Started on server. Can listen on socket, nothing else

This commit is contained in:
j4nk 2023-05-21 18:47:35 -04:00
parent 19562a51d0
commit ecf6115dd1
7 changed files with 139 additions and 4 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
main
*.o
vgcore*
*.core
*.core
open_card_table

View File

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

19
main.c
View File

@ -3,6 +3,7 @@
#include <lua5.3/lauxlib.h>
#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;
}

77
oct_networking.c Normal file
View File

@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#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;
}

18
oct_networking.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef OCT_NETWORKING_H
#define OCT_NETWORKING_H
#include <sys/socket.h>
#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

View File

@ -6,3 +6,6 @@ function load_termbox_sprite(file_name)
io.close(file);
return contents;
end
OCT_TYPE_SERVER = 1;
OCT_TYPE_CLIENT = 2;

15
test_server.lua Normal file
View File

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