From 0323c86399ac443cacb41e1a5ef009c772a1b262 Mon Sep 17 00:00:00 2001 From: j4nk Date: Mon, 11 Sep 2023 02:59:25 -0400 Subject: [PATCH] Started on lobby. Can register new client --- lobby.lua | 76 +++++++++++++++++++++++++++++++++++++++++++ oct_networking.c | 2 +- test_lobby_server.lua | 12 +++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 lobby.lua create mode 100644 test_lobby_server.lua diff --git a/lobby.lua b/lobby.lua new file mode 100644 index 0000000..7fa8f51 --- /dev/null +++ b/lobby.lua @@ -0,0 +1,76 @@ +require("oct_utils") +require("termbox_defs") +json = require "json" + +-- begin message type constants +OCT_LOBBY_MSG_CLIENTREG = 0; -- Client registration message +OCT_LOBBY_MSG_CLIENTUNREG = 1; -- Client unreg message +OCT_LOBBY_MSG_CLIENTLIST = 2; -- Client list broadcast message +OCT_LOBBY_MSG_START = 3; -- Start the game message +OCT_LOBBY_MSG_PING = 4; -- Querying connection +OCT_LOBBY_MSG_PONG = 5; -- Responding to connection query +-- end message type constants + +oct_lobby_clientlist = {}; +oct_lobby_timers = {}; +oct_lobby_timers["clientlist"] = 1000; -- Every 1000 calls to lobby_client broadcast client list + +function lobby_server(maxplayers) + msg,addr,port = oct_recv(); + if (msg ~= "") then + msg_dec = json.decode(msg); + if (msg_dec) then + -- OCT_LOG_INFO("RECEIVED from " .. addr .. " : msg_dec") + -- Message registering a new client + if (msg_dec["msgtype"] == OCT_LOBBY_MSG_CLIENTREG) then + -- first port var is the outgoing port of the client, basically useless + -- client sends the incoming port in the message + client_port = msg_dec["port"]; + if (client_port == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no port"); return; end + name = msg_dec["name"]; + if (name == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no name"); return; end + -- Now check if there is already someone in the server with the same name + if (oct_lobby_clientlist[name] ~= nil) then + OCT_LOG_ERROR(addr .. ":" .. port .. " tried to register a name already registered: " .. name); + return; + end + -- Now check if there is already someone in the server with the same ip/port combo + for k,v in pairs(oct_lobby_clientlist) do + if (v["addr"] == addr and v["port"] == client_port) then + OCT_LOG_ERROR(addr .. ":" .. port .. " tried to reregister"); + return; + end + end + -- All good, register them + oct_lobby_clientlist[name] = {addr=addr,port=client_port}; + OCT_LOG_INFO("Registered new client: " .. name .. " @ " .. addr .. ":" .. client_port); + end + else + OCT_LOG_WARNING("Could not decode received json from " .. addr); + end + end +end + +client_connected = false; +oct_started = false; + +function lobby_client(ip, port, name) + -- If not already connected, connect sending info + if (not client_connected) then + msg_to_server = json.encode({ {name = name} }) + oct_send(msg, ip, port) + end + + msg_from_server,servaddr,servport = oct_recv(); + msg_from_server_dec = json.decode(msg_from_server); + if (msg_from_server_dec) then + if (msg_from_server_dec["clientlist"] ~= nil) then + oct_lobby_clientlist = msg_from_server_dec["clientlist"]; + elseif (msg_from_server_dec["start"] ~= nil) then + oct_started = true; + end + else + OCT_LOG_WARNING("Could not decode received json from server"); + end +end + diff --git a/oct_networking.c b/oct_networking.c index b25a42b..4e8fad0 100644 --- a/oct_networking.c +++ b/oct_networking.c @@ -249,7 +249,7 @@ int oct_network_send_msgs() { } else { OCT_LOG_DEBUG("Found in address book: %s:%s", m.addr, m.port); - } + } int sfd = e->sfd; ssize_t length = strlen(m.buffer); diff --git a/test_lobby_server.lua b/test_lobby_server.lua new file mode 100644 index 0000000..bbed997 --- /dev/null +++ b/test_lobby_server.lua @@ -0,0 +1,12 @@ +require("oct_utils") +require("termbox_defs") +require("lobby") + +function oct_init() + OCT_LOG_INFO("Started test_lobby_server"); + return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX; +end + +function oct_loop(key) + lobby_server(10) +end