From 0cc7f696624fed1b08619a7b1597c349471fe618 Mon Sep 17 00:00:00 2001 From: j4nk Date: Sat, 27 May 2023 17:28:19 -0400 Subject: [PATCH] Added ability to send messages --- main.c | 2 ++ oct_networking.c | 77 ++++++++++++++++++++++++++++++++++++++++++++---- test_client.lua | 24 +++++++++++++++ test_server.lua | 6 ++-- 4 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 test_client.lua diff --git a/main.c b/main.c index 00d4f92..d6271b8 100644 --- a/main.c +++ b/main.c @@ -109,6 +109,7 @@ int main(int argc, char* argv[]) { oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]); } tb_present(); + oct_network_send_msg(); } } else { @@ -118,6 +119,7 @@ int main(int argc, char* argv[]) { lua_pushinteger(L, 0); lua_pushinteger(L, 0); lua_call(L, 2, 0); + oct_network_send_msg(); } } deinitialize_everything(); diff --git a/oct_networking.c b/oct_networking.c index fe5fa7e..0af7379 100644 --- a/oct_networking.c +++ b/oct_networking.c @@ -8,16 +8,19 @@ #include #include - +#include "oct_log.h" #include "oct_networking.h" struct { int needs_recv; char recv_buffer[BUFFER_SIZE]; - //char recv_from[NI_MAXHOST]; + char recv_addr[NI_MAXHOST]; + char recv_port[NI_MAXSERV]; int needs_send; char send_buffer[BUFFER_SIZE]; + char send_addr[NI_MAXHOST]; + char send_port[NI_MAXSERV]; int sfd; } oct_network_node; @@ -79,11 +82,17 @@ int oct_network_node_init(char* port, lua_State* L) { 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 + socklen_t peer_addrlen = sizeof(peer_addr); // 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) { + if (recvfrom(oct_network_node.sfd, oct_network_node.recv_buffer, BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen) > 0) { + int s = getnameinfo((struct sockaddr *) &peer_addr, + peer_addrlen, oct_network_node.recv_addr, NI_MAXHOST, oct_network_node.recv_port, NI_MAXSERV, NI_NUMERICSERV); + if (s != 0) { + OCT_LOG_WARNING("Received message from unknown host: %s", oct_network_node.recv_buffer); + return -1; + } oct_network_node.needs_recv = 0; } } @@ -92,19 +101,75 @@ int oct_network_recv_msg() { int oct_network_send_msg() { - return 0; + if (oct_network_node.needs_send) { + int sfd; + int s; + struct addrinfo hints; + struct addrinfo* result; + struct addrinfo* rp; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ + hints.ai_flags = 0; + hints.ai_protocol = 0; /* Any protocol */ + s = getaddrinfo(oct_network_node.send_addr, oct_network_node.send_port, &hints, &result); + if (s != 0) { + OCT_LOG_WARNING("getaddrinfo failed when sending message"); + return -1; + } + + /* getaddrinfo() returns a list of address structures. + Try each address until we successfully connect(2). + If socket(2) (or connect(2)) fails, we (close the socket + 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) + continue; + + if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) + break; /* Success */ + + close(sfd); + } + freeaddrinfo(result); /* No longer needed */ + + if (rp == NULL) { /* No address succeeded */ + fprintf(stderr, "Could not connect\n"); + exit(EXIT_FAILURE); + } + ssize_t length = strlen(oct_network_node.send_buffer); + if (write(sfd, oct_network_node.send_buffer, length) != length) { + OCT_LOG_WARNING("Partial write when sending message"); + return -1; + } + oct_network_node.needs_send = 0; + } + return 1; } int oct_network_recv_msg_lua(lua_State* L) { if (!oct_network_node.needs_recv) { lua_pushstring(L, oct_network_node.recv_buffer); + lua_pushstring(L, oct_network_node.recv_addr); + lua_pushstring(L, oct_network_node.recv_port); oct_network_node.needs_recv = 1; } else { lua_pushstring(L, ""); + lua_pushstring(L, ""); + lua_pushstring(L, ""); } - return 1; + return 3; } int oct_network_send_msg_lua(lua_State* L) { + if (!oct_network_node.needs_send) { + strncpy(oct_network_node.send_buffer, luaL_checkstring(L, -3), BUFFER_SIZE); + strncpy(oct_network_node.send_addr, luaL_checkstring(L, -2), NI_MAXHOST); + strncpy(oct_network_node.send_port, luaL_checkstring(L, -1), NI_MAXSERV); + oct_network_node.needs_send = 1; + } return 0; } diff --git a/test_client.lua b/test_client.lua new file mode 100644 index 0000000..2c16de6 --- /dev/null +++ b/test_client.lua @@ -0,0 +1,24 @@ +-- Make sure to run open_card_table with -ll 3 to see output + +require("oct_utils") +require("termbox_defs") + +--text = oct_tb_sprite_new(); + +function oct_init() + -- text["shape"] = "THIS IS A TEST SERVER"; + -- text["x"] = 50; + -- text["y"] = 10; + OCT_LOG_INFO("THIS IS A TEST CLIENT"); + return OCT_TYPE_SERVER, OCT_NOT_NEEDS_TERMBOX; +end + +counter = 0; +function oct_loop(key) + if counter == 1000000 then + oct_send("Hello!", "127.0.0.1", "1234"); + counter = 0; + end + counter = counter + 1; + --msg,addr,port = oct_recv(); +end diff --git a/test_server.lua b/test_server.lua index 3669b0b..08097dd 100644 --- a/test_server.lua +++ b/test_server.lua @@ -14,8 +14,8 @@ function oct_init() end function oct_loop(key) - str = oct_recv(); - if (str ~= "" or str == nil) then - OCT_LOG_INFO(str); + msg,addr,port = oct_recv(); + if (msg ~= "") then + OCT_LOG_INFO(msg .. " " .. addr .. " " .. port); end end