Can receive messages

This commit is contained in:
j4nk 2023-05-26 23:52:57 -04:00
parent bb510e068e
commit 53651462d6
5 changed files with 53 additions and 8 deletions

View File

@ -6,10 +6,10 @@ DEBUG=-g
CFLAGS=-D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE CFLAGS=-D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE
open_card_table: main.o oct_networking.o oct_termbox_sprite.o oct_log.o 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 %.o: %.c
$(CC) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $< $(CC) $(DEBUG) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $<
test: test:
$(CC) $(CFLAGS) $(INC) $(CLIB) test.c -o test $(DEBUG) $(CC) $(CFLAGS) $(INC) $(CLIB) test.c -o test $(DEBUG)

4
main.c
View File

@ -100,6 +100,7 @@ int main(int argc, char* argv[]) {
if (ev.key == TB_KEY_ESC) { if (ev.key == TB_KEY_ESC) {
finish = 1; finish = 1;
} }
oct_network_recv_msg();
lua_getglobal(L, "oct_loop"); lua_getglobal(L, "oct_loop");
lua_pushinteger(L, ev.key); lua_pushinteger(L, ev.key);
lua_pushinteger(L, ev.ch); lua_pushinteger(L, ev.ch);
@ -112,6 +113,7 @@ int main(int argc, char* argv[]) {
} }
else { else {
while (!finish) { while (!finish) {
oct_network_recv_msg();
lua_getglobal(L, "oct_loop"); lua_getglobal(L, "oct_loop");
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
@ -240,7 +242,7 @@ int initialize_everything(char* lua_file) {
if (config.type == OCT_TYPE_SERVER) { if (config.type == OCT_TYPE_SERVER) {
OCT_LOG_INFO("Lua script is server type"); 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); OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port);
return OCT_NETWORK_ERROR; return OCT_NETWORK_ERROR;
} }

View File

@ -3,6 +3,8 @@
#include <netdb.h> #include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -11,14 +13,16 @@
struct { struct {
int needs_recv; int needs_recv;
int needs_send;
char recv_buffer[BUFFER_SIZE]; char recv_buffer[BUFFER_SIZE];
//char recv_from[NI_MAXHOST];
int needs_send;
char send_buffer[BUFFER_SIZE]; char send_buffer[BUFFER_SIZE];
int sfd; int sfd;
} oct_network_node; } oct_network_node;
int oct_network_node_init(char* port) { int oct_network_node_init(char* port, lua_State* L) {
oct_network_node.needs_recv = 0; oct_network_node.needs_recv = 1;
oct_network_node.needs_send = 0; oct_network_node.needs_send = 0;
struct addrinfo hints; struct addrinfo hints;
@ -62,13 +66,45 @@ int oct_network_node_init(char* port) {
return 0; 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; return 1;
} }
// This runs every loop, filling the receive buffer if needed
int oct_network_recv_msg() { 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; return 0;
} }
int oct_network_send_msg() { int oct_network_send_msg() {
return 0; 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;
}

View File

@ -2,6 +2,7 @@
#define OCT_NETWORKING_H #define OCT_NETWORKING_H
#include <sys/socket.h> #include <sys/socket.h>
#include <lua5.3/lualib.h>
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
@ -15,9 +16,12 @@ struct oct_network_client;
struct oct_network_server; 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_node_deinit();
int oct_network_recv_msg(); int oct_network_recv_msg();
int oct_network_send_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 #endif

View File

@ -14,5 +14,8 @@ function oct_init()
end end
function oct_loop(key) function oct_loop(key)
str = oct_recv();
if (str ~= "" or str == nil) then
OCT_LOG_INFO(str);
end
end end