Allow specifying server port as cmd line arg
This commit is contained in:
		
							parent
							
								
									ecf6115dd1
								
							
						
					
					
						commit
						522f64f68d
					
				
							
								
								
									
										33
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								main.c
									
									
									
									
									
								
							@ -13,8 +13,20 @@
 | 
				
			|||||||
#define OCT_LUA_FILE_NOT_FOUND 2
 | 
					#define OCT_LUA_FILE_NOT_FOUND 2
 | 
				
			||||||
#define OCT_LUA_FILE_MISSING_OCT_INIT 3
 | 
					#define OCT_LUA_FILE_MISSING_OCT_INIT 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define OCT_LUA_FILENAME_SIZE 128
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int oct_type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lua_State *L;
 | 
					lua_State *L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct {
 | 
				
			||||||
 | 
						char port[6]; // max 65535 so 6 bytes needed
 | 
				
			||||||
 | 
						// Might come in handy later to keep filename
 | 
				
			||||||
 | 
						// e.g. if I ever want to allow downloading a script from peer
 | 
				
			||||||
 | 
						char lua_file[OCT_LUA_FILENAME_SIZE];
 | 
				
			||||||
 | 
					} args;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int process_args(int argc, char* argv[]);
 | 
					int process_args(int argc, char* argv[]);
 | 
				
			||||||
int initialize_everything(char* lua_file);
 | 
					int initialize_everything(char* lua_file);
 | 
				
			||||||
int deinitialize_everything();
 | 
					int deinitialize_everything();
 | 
				
			||||||
@ -57,6 +69,23 @@ int process_args(int argc, char* argv[]) {
 | 
				
			|||||||
	if (argc == 1) { // Didn't specify a file
 | 
						if (argc == 1) { // Didn't specify a file
 | 
				
			||||||
		return OCT_NO_LUA_FILE;
 | 
							return OCT_NO_LUA_FILE;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set args.port to default
 | 
				
			||||||
 | 
						strncpy(args.port, OCT_DEFAULT_PORT, 6);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (int i = 1; i < argc-1; i++) {
 | 
				
			||||||
 | 
							// argv's are guaranteed to be zero-terminated, so can use
 | 
				
			||||||
 | 
							// strcmp instead of strncmp
 | 
				
			||||||
 | 
							if (strcmp(argv[i], "-p") == 0) {
 | 
				
			||||||
 | 
								if (i != (argc-2)) {
 | 
				
			||||||
 | 
									strncpy(args.port, argv[i+1], 6);
 | 
				
			||||||
 | 
									i++;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						strncpy(args.lua_file, argv[argc-1], OCT_LUA_FILENAME_SIZE);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	// TODO define other cmd line args here
 | 
						// TODO define other cmd line args here
 | 
				
			||||||
	return initialize_everything(argv[argc-1]); // lua file should always be last argument
 | 
						return initialize_everything(argv[argc-1]); // lua file should always be last argument
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -79,8 +108,8 @@ int initialize_everything(char* lua_file) {
 | 
				
			|||||||
	//printf("TYPE: %d\n", type);
 | 
						//printf("TYPE: %d\n", type);
 | 
				
			||||||
	if (type == OCT_TYPE_SERVER) {
 | 
						if (type == OCT_TYPE_SERVER) {
 | 
				
			||||||
		// TODO make port a command line argument
 | 
							// TODO make port a command line argument
 | 
				
			||||||
		if (!oct_network_server_init("1234")) {
 | 
							if (!oct_network_server_init(args.port)) {
 | 
				
			||||||
			printf("Could not establish a socket on port 1234\n");
 | 
								printf("Could not establish a socket on port %s\n", args.port);
 | 
				
			||||||
			return 0;
 | 
								return 0;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,11 +8,15 @@
 | 
				
			|||||||
#define OCT_TYPE_SERVER 1
 | 
					#define OCT_TYPE_SERVER 1
 | 
				
			||||||
#define OCT_TYPE_CLIENT 2
 | 
					#define OCT_TYPE_CLIENT 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define OCT_DEFAULT_PORT "20000"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Forward declarations
 | 
				
			||||||
struct oct_network_client;
 | 
					struct oct_network_client;
 | 
				
			||||||
struct oct_network_server;
 | 
					struct oct_network_server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int oct_network_client_init();
 | 
					int oct_network_client_init();
 | 
				
			||||||
int oct_network_server_init(char* port);
 | 
					int oct_network_server_init(char* port);
 | 
				
			||||||
 | 
					int oct_network_server_deinit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif 
 | 
					#endif 
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user