From 32f4ee7b8f070ef688a31f2af0cacf9285d4a61c Mon Sep 17 00:00:00 2001 From: j4nk Date: Sun, 28 May 2023 23:42:23 -0400 Subject: [PATCH] Signal to need networking, no diff between client/server --- demo2.lua | 2 +- main.c | 16 ++++++---------- oct_networking.c | 1 + oct_networking.h | 3 --- oct_utils.lua | 4 ++-- pong.lua | 2 +- test_client.lua | 2 +- test_server.lua | 2 +- 8 files changed, 13 insertions(+), 19 deletions(-) diff --git a/demo2.lua b/demo2.lua index abbd8b3..95ff08a 100644 --- a/demo2.lua +++ b/demo2.lua @@ -12,7 +12,7 @@ function oct_init() card1["y"] = 0; card2["x"] = 50; card2["y"] = 50; - return OCT_TYPE_CLIENT, OCT_NEEDS_TERMBOX; + return OCT_NOT_NEEDS_NETWORKING, OCT_NEEDS_TERMBOX; end function oct_loop(key) diff --git a/main.c b/main.c index d6271b8..16404f3 100644 --- a/main.c +++ b/main.c @@ -37,7 +37,7 @@ struct { // e.g. if I ever want to allow downloading a script from peer char lua_file[OCT_MAX_FILENAME_SIZE]; int needs_termbox; - int type; + int needs_networking; } config; void usage(); @@ -232,7 +232,7 @@ int initialize_everything(char* lua_file) { OCT_LOG_INFO("Begin running oct_init()"); lua_getglobal(L, "oct_init"); lua_call(L, 0, 2); - config.type = lua_tointeger(L, -2); + config.needs_networking = lua_tointeger(L, -2); config.needs_termbox = lua_tointeger(L, -1); OCT_LOG_INFO("Finish running oct_init()"); @@ -241,20 +241,16 @@ int initialize_everything(char* lua_file) { if (!config.needs_termbox) { oct_tb_change_oct_tb_sprite_new(L); } + + OCT_LOG_INFO("Lua script %s networking", config.needs_networking ? "requires" : "does not require"); - if (config.type == OCT_TYPE_SERVER) { - OCT_LOG_INFO("Lua script is server type"); + if (config.needs_networking) { if (!oct_network_node_init(config.port, L)) { OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port); return OCT_NETWORK_ERROR; } } - else if (config.type == OCT_TYPE_CLIENT) { - //oct_network_client_init(); - } - else { - // Do nothing, offline - } + if (config.needs_termbox) { tb_init(); } diff --git a/oct_networking.c b/oct_networking.c index 0af7379..5c89569 100644 --- a/oct_networking.c +++ b/oct_networking.c @@ -124,6 +124,7 @@ int oct_network_send_msg() { and) try the next address. */ for (rp = result; rp != NULL; rp = rp->ai_next) { + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) diff --git a/oct_networking.h b/oct_networking.h index 7292148..3d4b337 100644 --- a/oct_networking.h +++ b/oct_networking.h @@ -6,9 +6,6 @@ #define BUFFER_SIZE 1024 -#define OCT_TYPE_SERVER 1 -#define OCT_TYPE_CLIENT 2 - #define OCT_DEFAULT_PORT "20000" // Forward declarations diff --git a/oct_utils.lua b/oct_utils.lua index 743c278..5cb3c41 100644 --- a/oct_utils.lua +++ b/oct_utils.lua @@ -7,8 +7,8 @@ function load_termbox_sprite(file_name) return contents; end -OCT_TYPE_SERVER = 1; -OCT_TYPE_CLIENT = 2; +OCT_NEEDS_NETWORKING = 1; +OCT_NOT_NEEDS_NETWORKING = 0; OCT_NEEDS_TERMBOX = 1; OCT_NOT_NEEDS_TERMBOX = 0; diff --git a/pong.lua b/pong.lua index f618cf6..8b15770 100644 --- a/pong.lua +++ b/pong.lua @@ -85,7 +85,7 @@ function oct_init() score_p2_sprite["shape"] = "Player 2: " .. tostring(score_p2); init_text["shape"] = "Press SPACE to begin"; pause(); - return OCT_TYPE_CLIENT, OCT_NEEDS_TERMBOX; + return OCT_NOT_NEEDS_NETWORKING, OCT_NEEDS_TERMBOX; end function oct_loop(key, ch) diff --git a/test_client.lua b/test_client.lua index 2c16de6..d146cb4 100644 --- a/test_client.lua +++ b/test_client.lua @@ -10,7 +10,7 @@ function oct_init() -- text["x"] = 50; -- text["y"] = 10; OCT_LOG_INFO("THIS IS A TEST CLIENT"); - return OCT_TYPE_SERVER, OCT_NOT_NEEDS_TERMBOX; + return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX; end counter = 0; diff --git a/test_server.lua b/test_server.lua index 08097dd..aa2954c 100644 --- a/test_server.lua +++ b/test_server.lua @@ -10,7 +10,7 @@ function oct_init() -- text["x"] = 50; -- text["y"] = 10; OCT_LOG_INFO("THIS IS A TEST SERVER"); - return OCT_TYPE_SERVER, OCT_NOT_NEEDS_TERMBOX; + return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX; end function oct_loop(key)