From 522f64f68d69a0fc1e5e5d88ab41d3417af54efd Mon Sep 17 00:00:00 2001 From: j4nk Date: Mon, 22 May 2023 09:29:35 -0400 Subject: [PATCH] Allow specifying server port as cmd line arg --- main.c | 33 +++++++++++++++++++++++++++++++-- oct_networking.h | 4 ++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index c2628ca..30f5d49 100644 --- a/main.c +++ b/main.c @@ -13,8 +13,20 @@ #define OCT_LUA_FILE_NOT_FOUND 2 #define OCT_LUA_FILE_MISSING_OCT_INIT 3 +#define OCT_LUA_FILENAME_SIZE 128 + + +int oct_type; + 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 initialize_everything(char* lua_file); int deinitialize_everything(); @@ -57,6 +69,23 @@ int process_args(int argc, char* argv[]) { if (argc == 1) { // Didn't specify a 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 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); if (type == OCT_TYPE_SERVER) { // TODO make port a command line argument - if (!oct_network_server_init("1234")) { - printf("Could not establish a socket on port 1234\n"); + if (!oct_network_server_init(args.port)) { + printf("Could not establish a socket on port %s\n", args.port); return 0; } } diff --git a/oct_networking.h b/oct_networking.h index 3aebc19..64d7f5f 100644 --- a/oct_networking.h +++ b/oct_networking.h @@ -8,11 +8,15 @@ #define OCT_TYPE_SERVER 1 #define OCT_TYPE_CLIENT 2 +#define OCT_DEFAULT_PORT "20000" + +// Forward declarations struct oct_network_client; struct oct_network_server; int oct_network_client_init(); int oct_network_server_init(char* port); +int oct_network_server_deinit(); #endif