Added ability to send messages

This commit is contained in:
j4nk 2023-05-27 17:28:19 -04:00
parent 53651462d6
commit 0cc7f69662
4 changed files with 100 additions and 9 deletions

2
main.c
View File

@ -109,6 +109,7 @@ int main(int argc, char* argv[]) {
oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]); oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]);
} }
tb_present(); tb_present();
oct_network_send_msg();
} }
} }
else { else {
@ -118,6 +119,7 @@ int main(int argc, char* argv[]) {
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
lua_call(L, 2, 0); lua_call(L, 2, 0);
oct_network_send_msg();
} }
} }
deinitialize_everything(); deinitialize_everything();

View File

@ -8,16 +8,19 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "oct_log.h"
#include "oct_networking.h" #include "oct_networking.h"
struct { struct {
int needs_recv; int needs_recv;
char recv_buffer[BUFFER_SIZE]; char recv_buffer[BUFFER_SIZE];
//char recv_from[NI_MAXHOST]; char recv_addr[NI_MAXHOST];
char recv_port[NI_MAXSERV];
int needs_send; int needs_send;
char send_buffer[BUFFER_SIZE]; char send_buffer[BUFFER_SIZE];
char send_addr[NI_MAXHOST];
char send_port[NI_MAXSERV];
int sfd; int sfd;
} oct_network_node; } oct_network_node;
@ -79,11 +82,17 @@ int oct_network_node_init(char* port, lua_State* L) {
int oct_network_recv_msg() { int oct_network_recv_msg() {
if (oct_network_node.needs_recv) { if (oct_network_node.needs_recv) {
struct sockaddr_storage peer_addr; 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 // recvfrom returns -1 if nothing was received
// Need MSG_DONTWAIT flag for nonblocking // 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; oct_network_node.needs_recv = 0;
} }
} }
@ -92,19 +101,75 @@ int oct_network_recv_msg() {
int oct_network_send_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) { int oct_network_recv_msg_lua(lua_State* L) {
if (!oct_network_node.needs_recv) { if (!oct_network_node.needs_recv) {
lua_pushstring(L, oct_network_node.recv_buffer); 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; oct_network_node.needs_recv = 1;
} }
else { else {
lua_pushstring(L, ""); lua_pushstring(L, "");
lua_pushstring(L, "");
lua_pushstring(L, "");
} }
return 1; return 3;
} }
int oct_network_send_msg_lua(lua_State* L) { 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; return 0;
} }

24
test_client.lua Normal file
View File

@ -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

View File

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