diff --git a/Makefile b/Makefile index 16f0c26..0ca3fcd 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ DEBUG=-g CFLAGS=-D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE open_card_table: main.o oct_networking.o oct_termbox_sprite.o oct_log.o - $(CC) $(CFLAGS) $(INC) $(CLIB) -o $(BIN) $^ + $(CC) $(DEBUG) $(CFLAGS) $(INC) $(CLIB) -o $(BIN) $^ %.o: %.c - $(CC) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $< + $(CC) $(DEBUG) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $< test: $(CC) $(CFLAGS) $(INC) $(CLIB) test.c -o test $(DEBUG) diff --git a/main.c b/main.c index f2fa4a8..00d4f92 100644 --- a/main.c +++ b/main.c @@ -100,6 +100,7 @@ int main(int argc, char* argv[]) { if (ev.key == TB_KEY_ESC) { finish = 1; } + oct_network_recv_msg(); lua_getglobal(L, "oct_loop"); lua_pushinteger(L, ev.key); lua_pushinteger(L, ev.ch); @@ -112,6 +113,7 @@ int main(int argc, char* argv[]) { } else { while (!finish) { + oct_network_recv_msg(); lua_getglobal(L, "oct_loop"); lua_pushinteger(L, 0); lua_pushinteger(L, 0); @@ -240,7 +242,7 @@ int initialize_everything(char* lua_file) { if (config.type == OCT_TYPE_SERVER) { OCT_LOG_INFO("Lua script is server type"); - if (!oct_network_node_init(config.port)) { + 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; } diff --git a/oct_networking.c b/oct_networking.c index 14b03df..fe5fa7e 100644 --- a/oct_networking.c +++ b/oct_networking.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -11,14 +13,16 @@ struct { int needs_recv; - int needs_send; char recv_buffer[BUFFER_SIZE]; + //char recv_from[NI_MAXHOST]; + + int needs_send; char send_buffer[BUFFER_SIZE]; int sfd; } oct_network_node; -int oct_network_node_init(char* port) { - oct_network_node.needs_recv = 0; +int oct_network_node_init(char* port, lua_State* L) { + oct_network_node.needs_recv = 1; oct_network_node.needs_send = 0; struct addrinfo hints; @@ -61,14 +65,46 @@ int oct_network_node_init(char* port) { if (!rp) { return 0; } + + lua_pushcfunction(L, oct_network_recv_msg_lua); + lua_setglobal(L, "oct_recv"); + lua_pushcfunction(L, oct_network_send_msg_lua); + lua_setglobal(L, "oct_send"); return 1; } + +// This runs every loop, filling the receive buffer if needed int oct_network_recv_msg() { + if (oct_network_node.needs_recv) { + struct sockaddr_storage peer_addr; + socklen_t peer_addrlen = 0; // init is needed to make valgrind happy + + // recvfrom returns -1 if nothing was received + // Need MSG_DONTWAIT flag for nonblocking + if (recvfrom(oct_network_node.sfd, oct_network_node.recv_buffer, BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen) > 0) { + oct_network_node.needs_recv = 0; + } + } return 0; } + int oct_network_send_msg() { return 0; } + +int oct_network_recv_msg_lua(lua_State* L) { + if (!oct_network_node.needs_recv) { + lua_pushstring(L, oct_network_node.recv_buffer); + oct_network_node.needs_recv = 1; + } + else { + lua_pushstring(L, ""); + } + return 1; +} +int oct_network_send_msg_lua(lua_State* L) { + return 0; +} diff --git a/oct_networking.h b/oct_networking.h index f6adb6e..7292148 100644 --- a/oct_networking.h +++ b/oct_networking.h @@ -2,6 +2,7 @@ #define OCT_NETWORKING_H #include +#include #define BUFFER_SIZE 1024 @@ -15,9 +16,12 @@ struct oct_network_client; struct oct_network_server; -int oct_network_node_init(char* port); +int oct_network_node_init(char* port, lua_State* L); int oct_network_node_deinit(); int oct_network_recv_msg(); int oct_network_send_msg(); +int oct_network_recv_msg_lua(lua_State* L); +int oct_network_send_msg_lua(lua_State* L); + #endif diff --git a/test_server.lua b/test_server.lua index d73ca27..3669b0b 100644 --- a/test_server.lua +++ b/test_server.lua @@ -14,5 +14,8 @@ function oct_init() end function oct_loop(key) - + str = oct_recv(); + if (str ~= "" or str == nil) then + OCT_LOG_INFO(str); + end end