open-card-table/oct_networking.h

68 lines
1.9 KiB
C

#ifndef OCT_NETWORKING_H
#define OCT_NETWORKING_H
#include <sys/socket.h>
#include <lua5.3/lualib.h>
#include <netdb.h>
#define BUFFER_SIZE 1024
#define NUM_BUFFERS 10;
#define OCT_DEFAULT_PORT "20000"
// Allowed to receive up to 10 messages per iteration of the main loop
#define OCT_NETWORK_MAX_RECVS 10
// Queue stuff
// Send and recv queues are identical except in the functions that act on them, so we can use the same structure
// for both
struct oct_network_q_message {
char addr[NI_MAXHOST];
char port[NI_MAXSERV];
char buffer[BUFFER_SIZE];
};
struct oct_network_q_entry {
struct oct_network_q_message message;
struct oct_network_q_entry* next;
struct oct_network_q_entry* prev;
};
struct oct_network_q {
size_t size;
struct oct_network_q_entry* first;
struct oct_network_q_entry* last;
};
int oct_network_q_enqueue(struct oct_network_q* q, char* addr, char* port, char* msg);
struct oct_network_q_message oct_network_q_dequeue(struct oct_network_q* q);
void oct_network_q_init(struct oct_network_q* q);
void oct_network_q_deinit(struct oct_network_q* q);
struct oct_network_ab_entry {
int sfd;
char addr[NI_MAXHOST];
char port[NI_MAXSERV];
struct oct_network_ab_entry* next;
struct oct_network_ab_entry* prev;
};
int oct_network_node_init(char* port, lua_State* L);
void oct_network_node_deinit();
// These are heavily based off of man 3 getaddrinfo, e.g. at
// https://www.man7.org/linux/man-pages/man3/getaddrinfo.3.html
int oct_network_recv_msgs();
int oct_network_send_msgs();
int oct_network_recv_msg_lua(lua_State* L);
int oct_network_send_msg_lua(lua_State* L);
int oct_network_broadcast_msg_lua(lua_State* L);
void oct_network_ab_init();
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();
#endif