diff --git a/.gitignore b/.gitignore index 9ceda33..5429284 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ main *.o vgcore* -*.core \ No newline at end of file +*.core +open_card_table \ No newline at end of file diff --git a/Makefile b/Makefile index 20a759d..cdbff56 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,11 @@ INC=-I/usr/local/include BIN=main DEBUG=-g -all: - $(CC) $(INC) $(CLIB) main.c -o $(BIN) $(DEBUG) +open_card_table: main.o oct_networking.o + $(CC) $(INC) $(CLIB) -o open_card_table main.o oct_networking.o + +%.o: %.c + $(CC) $(INC) $(CLIB) -c -o $@ $< + test: $(CC) $(INC) $(CLIB) test.c -o test $(DEBUG) diff --git a/main.c b/main.c index 9a4147e..c2628ca 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include "termbox_render.h" #include "oct_termbox_sprite.h" +#include "oct_networking.h" #define TB_IMPL #include "termbox.h" @@ -73,7 +74,23 @@ int initialize_everything(char* lua_file) { oct_tb_initialize_lua(L); if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND; lua_getglobal(L, "oct_init"); - lua_call(L, 0, 0); + lua_call(L, 0, 1); + int type = lua_tointeger(L, -1); + //printf("TYPE: %d\n", type); + if (type == OCT_TYPE_SERVER) { + // TODO make port a command line argument + if (!oct_network_server_init("1234")) { + printf("Could not establish a socket on port 1234\n"); + return 0; + } + } + else if (type == OCT_TYPE_CLIENT) { + //oct_network_client_init(); + } + else { + // Do nothing, offline + } + tb_init(); return 0; } diff --git a/oct_networking.c b/oct_networking.c new file mode 100644 index 0000000..0bd33ac --- /dev/null +++ b/oct_networking.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include + + +#include "oct_networking.h" + +struct { + int needs_recv; + int needs_send; + char recv_buffer[BUFFER_SIZE]; + char send_buffer[BUFFER_SIZE]; + + int sock_fd; + +} oct_network_client; + +struct { + int needs_recv; + int needs_send; + char recv_buffer[BUFFER_SIZE]; + char send_buffer[BUFFER_SIZE]; + + int sfd; +} oct_network_server; + +int oct_network_server_init(char* port) { + oct_network_server.needs_recv = 0; + oct_network_server.needs_send = 0; + + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // ipv4 or ipv6 + hints.ai_socktype = SOCK_DGRAM; // datagram + hints.ai_flags = AI_PASSIVE; // any IP address + hints.ai_protocol = 0; // any protocol + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + + struct addrinfo* result; + int s = getaddrinfo(NULL, port, &hints, &result); + if (s != 0) { + //fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return 0; + } + + /* getaddrinfo() returns a list of address structures. + Try each address until we successfully bind(2). + If socket(2) (or bind(2)) fails, we (close the socket + and) try the next address. */ + + struct addrinfo *rp; + + for (rp = result; rp != NULL; rp = rp->ai_next) { + oct_network_server.sfd = socket(rp->ai_family, rp->ai_socktype, + rp->ai_protocol); + if (oct_network_server.sfd == -1) + continue; + + if (bind(oct_network_server.sfd, rp->ai_addr, rp->ai_addrlen) == 0) + break; /* Success */ + + close(oct_network_server.sfd); + } + + freeaddrinfo(result); + if (!rp) { + return 0; + } + + return 1; +} diff --git a/oct_networking.h b/oct_networking.h new file mode 100644 index 0000000..3aebc19 --- /dev/null +++ b/oct_networking.h @@ -0,0 +1,18 @@ +#ifndef OCT_NETWORKING_H +#define OCT_NETWORKING_H + +#include + +#define BUFFER_SIZE 1024 + +#define OCT_TYPE_SERVER 1 +#define OCT_TYPE_CLIENT 2 + +struct oct_network_client; +struct oct_network_server; + + +int oct_network_client_init(); +int oct_network_server_init(char* port); + +#endif diff --git a/oct_utils.lua b/oct_utils.lua index 61921ee..5c3ee7b 100644 --- a/oct_utils.lua +++ b/oct_utils.lua @@ -6,3 +6,6 @@ function load_termbox_sprite(file_name) io.close(file); return contents; end + +OCT_TYPE_SERVER = 1; +OCT_TYPE_CLIENT = 2; diff --git a/test_server.lua b/test_server.lua new file mode 100644 index 0000000..bf5d4bc --- /dev/null +++ b/test_server.lua @@ -0,0 +1,15 @@ +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; + return OCT_TYPE_SERVER; +end + +function oct_loop(key) + -- Do nothing, for now +end