Finished address book, but broke networking. Needs debug

This commit is contained in:
j4nk 2023-06-09 09:42:19 -04:00
parent 1678fa286b
commit 27e4c19117
3 changed files with 43 additions and 7 deletions

4
main.c
View File

@ -223,6 +223,8 @@ int initialize_everything(char* lua_file) {
deinitialize_everything();
return 0;
}
// initialize the address book
oct_network_ab_init();
}
if (config.needs_termbox) {
@ -236,6 +238,8 @@ int deinitialize_everything() {
tb_shutdown();
if (L) lua_close(L);
oct_tb_sprite_list_deinitialize();
oct_network_ab_deinit();
//oct_network_node_deinit();
oct_log_deinit();
return 1;
}

View File

@ -141,6 +141,7 @@ int oct_network_recv_msg() {
// 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_LOG_INFO("Received message!");
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) {
@ -156,14 +157,29 @@ int oct_network_recv_msg() {
int oct_network_send_msg() {
if (oct_network_node.needs_send) {
int sfd = oct_network_init_socket(oct_network_node.send_addr, oct_network_node.send_port);
struct oct_network_ab_entry* e;
e = oct_network_ab_find(oct_network_node.send_addr, oct_network_node.send_port);
if (!e) {
OCT_LOG_INFO("Not in address book: %s:%s", oct_network_node.send_addr, oct_network_node.send_port);
e = oct_network_ab_insert(oct_network_node.send_addr, oct_network_node.send_port);
if (!e) {
// Give up...
oct_network_node.needs_send = 0;
return 0;
}
}
else {
OCT_LOG_INFO("Found in address book: %s:%s", oct_network_node.send_addr, oct_network_node.send_port);
}
int sfd = e->sfd;
ssize_t length = strlen(oct_network_node.send_buffer);
OCT_LOG_INFO("Sending message");
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;
close(sfd);
}
return 1;
}
@ -198,7 +214,7 @@ void oct_network_ab_init() {
oct_network_ab.last = NULL;
}
int oct_network_ab_insert(char* addr, char* port) {
struct oct_network_ab_entry* oct_network_ab_insert(char* addr, char* port) {
// First create the entry
struct oct_network_ab_entry* e = (struct oct_network_ab_entry*)malloc(sizeof(struct oct_network_ab_entry));
if (!e) {
@ -208,8 +224,9 @@ int oct_network_ab_insert(char* addr, char* port) {
// Then, create the socket
if ((e->sfd = oct_network_init_socket(addr, port)) < 0) {
free(e);
return 0;
return NULL;
}
OCT_LOG_INFO("CREATED NEW SOCKET");
e->next = NULL;
e->prev = oct_network_ab.last;
strncpy(e->addr, addr, NI_MAXHOST);
@ -223,15 +240,17 @@ int oct_network_ab_insert(char* addr, char* port) {
oct_network_ab.last = e;
oct_network_ab.size++;
return 1;
return e;
}
int oct_network_ab_remove(struct oct_network_ab_entry* e) {
// First, remove node from linked list and fix
if (e == oct_network_ab.first) {
oct_network_ab.first = e->next;
if (e->next) {
e->next->prev = NULL;
}
}
else if (e == oct_network_ab.last) {
oct_network_ab.last = e->prev;
e->prev->next = NULL;
@ -248,6 +267,18 @@ int oct_network_ab_remove(struct oct_network_ab_entry* e) {
return 1;
}
struct oct_network_ab_entry* oct_network_ab_find(char* addr, char* port) {
struct oct_network_ab_entry* crawler = oct_network_ab.first;
while (crawler != NULL) {
if (strncmp(crawler->addr, addr, NI_MAXHOST) == 0 &&
strncmp(crawler->port, port, NI_MAXSERV) == 0) {
return crawler;
}
crawler = crawler->next;
}
return NULL;
}
void oct_network_ab_deinit() {
while (oct_network_ab.first) {
oct_network_ab_remove(oct_network_ab.first);

View File

@ -30,8 +30,9 @@ int oct_network_recv_msg_lua(lua_State* L);
int oct_network_send_msg_lua(lua_State* L);
void oct_network_ab_init();
int oct_network_ab_insert(char* addr, char* port);
struct oct_network_ab_entry* oct_network_ab_insert(char* addr, char* port);
int oct_network_ab_remove(struct oct_network_ab_entry* e);
struct oct_network_ab_entry* oct_network_ab_find(char* addr, char* port);
void oct_network_ab_deinit();