Redoing lobby, client registration implemented
This commit is contained in:
parent
7cf075d1a6
commit
97124b1e64
102
lobby.lua
102
lobby.lua
|
@ -8,7 +8,8 @@ OCT_LOBBY_MSG_CLIENTUNREG = 1; -- Client unreg message
|
||||||
OCT_LOBBY_MSG_CLIENTLIST = 2; -- Client list broadcast message
|
OCT_LOBBY_MSG_CLIENTLIST = 2; -- Client list broadcast message
|
||||||
OCT_LOBBY_MSG_START = 3; -- Start the game message
|
OCT_LOBBY_MSG_START = 3; -- Start the game message
|
||||||
OCT_LOBBY_MSG_PING = 4; -- Querying connection
|
OCT_LOBBY_MSG_PING = 4; -- Querying connection
|
||||||
OCT_LOBBY_MSG_PONG = 5; -- Responding to connection query
|
OCT_LOBBY_MSG_ACK = 5; -- "yes", used to respond to ping and other stuff
|
||||||
|
OCT_LOBBY_MSG_NAK = 6; -- "no"
|
||||||
-- end message type constants
|
-- end message type constants
|
||||||
|
|
||||||
oct_lobby_clientlist = {};
|
oct_lobby_clientlist = {};
|
||||||
|
@ -18,59 +19,76 @@ oct_lobby_timers["clientlist"] = 1000; -- Every 1000 calls to lobby_client broad
|
||||||
function lobby_server(maxplayers)
|
function lobby_server(maxplayers)
|
||||||
msg,addr,port = oct_recv();
|
msg,addr,port = oct_recv();
|
||||||
if (msg ~= "") then
|
if (msg ~= "") then
|
||||||
msg_dec = json.decode(msg);
|
msg_obj = json.decode(msg)[1] -- dumps to 1-elem array, get first elem
|
||||||
if (msg_dec) then
|
|
||||||
-- OCT_LOG_INFO("RECEIVED from " .. addr .. " : msg_dec")
|
if (msg_obj == nil) then
|
||||||
-- Message registering a new client
|
OCT_LOG_WARNING("Malformed json received")
|
||||||
if (msg_dec["msgtype"] == OCT_LOBBY_MSG_CLIENTREG) then
|
end
|
||||||
-- first port var is the outgoing port of the client, basically useless
|
|
||||||
-- client sends the incoming port in the message
|
-- The main state machine
|
||||||
client_port = msg_dec["port"];
|
if (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTREG) then
|
||||||
if (client_port == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no port"); return; end
|
-- Client registration
|
||||||
name = msg_dec["name"];
|
req_name = msg_obj["name"]
|
||||||
if (name == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no name"); return; end
|
req_port = msg_obj["port"]
|
||||||
-- Now check if there is already someone in the server with the same name
|
req_addr = convert_known_host_to_ip(addr)
|
||||||
if (oct_lobby_clientlist[name] ~= nil) then
|
|
||||||
OCT_LOG_ERROR(addr .. ":" .. port .. " tried to register a name already registered: " .. name);
|
-- First, check if clientlist contains a conflicting username
|
||||||
return;
|
if (oct_lobby_clientlist[req_name] ~= nil) then
|
||||||
end
|
response = json.encode({
|
||||||
-- Now check if there is already someone in the server with the same ip/port combo
|
{ msg_type = OCT_LOBBY_MSG_NAK }
|
||||||
for k,v in pairs(oct_lobby_clientlist) do
|
})
|
||||||
if (v["addr"] == addr and v["port"] == client_port) then
|
OCT_LOG_WARNING("Rejected attempt to register from client: " .. req_name .. " @ " .. req_addr .. ":" .. req_port)
|
||||||
OCT_LOG_ERROR(addr .. ":" .. port .. " tried to reregister");
|
|
||||||
return;
|
oct_send(response, req_addr, req_port)
|
||||||
end
|
else
|
||||||
end
|
response = json.encode({
|
||||||
-- All good, register them
|
{ msg_type = OCT_LOBBY_MSG_ACK }
|
||||||
oct_lobby_clientlist[name] = {addr=addr,port=client_port};
|
})
|
||||||
OCT_LOG_INFO("Registered new client: " .. name .. " @ " .. addr .. ":" .. client_port);
|
|
||||||
|
oct_lobby_clientlist[req_name] = {}
|
||||||
|
oct_lobby_clientlist[req_name]["addr"] = req_addr
|
||||||
|
oct_lobby_clientlist[req_name]["port"] = req_port
|
||||||
|
OCT_LOG_INFO("Registered new client: " .. req_name .. " @ " .. req_addr .. ":" .. req_port)
|
||||||
|
|
||||||
|
oct_send(response, req_addr, req_port)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
OCT_LOG_WARNING("Could not decode received json from " .. addr);
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
client_connected = false;
|
client_connected = false;
|
||||||
oct_started = false;
|
oct_started = false;
|
||||||
|
client_wait_for_reg_response = false;
|
||||||
|
|
||||||
function lobby_client(ip, port, name)
|
function lobby_client(ip, port, name, my_port)
|
||||||
-- If not already connected, connect sending info
|
-- If not already connected, connect sending info
|
||||||
if (not client_connected) then
|
if (not client_connected and client_wait_for_reg_response == false) then
|
||||||
msg_to_server = json.encode({ {name = name} })
|
msg_to_server = json.encode({
|
||||||
oct_send(msg, ip, port)
|
{ name = name, msg_type=OCT_LOBBY_MSG_CLIENTREG, port=my_port }
|
||||||
|
})
|
||||||
|
oct_send(msg_to_server, ip, port)
|
||||||
|
client_wait_for_reg_response = true -- prevent this from being run again
|
||||||
end
|
end
|
||||||
|
|
||||||
msg_from_server,servaddr,servport = oct_recv();
|
msg,addr,port = oct_recv()
|
||||||
msg_from_server_dec = json.decode(msg_from_server);
|
if (msg ~= "") then
|
||||||
if (msg_from_server_dec) then
|
msg_obj = json.decode(msg)[1]
|
||||||
if (msg_from_server_dec["clientlist"] ~= nil) then
|
|
||||||
oct_lobby_clientlist = msg_from_server_dec["clientlist"];
|
-- State machine code here
|
||||||
elseif (msg_from_server_dec["start"] ~= nil) then
|
-- Registration was successful
|
||||||
oct_started = true;
|
if (client_wait_for_reg_response == true and
|
||||||
|
msg_obj["msg_type"] == OCT_LOBBY_MSG_ACK) then
|
||||||
|
OCT_LOG_INFO("Server accepted registration request!")
|
||||||
|
client_connected = 1
|
||||||
|
client_wait_for_reg_response = false
|
||||||
|
|
||||||
|
-- Registration was unsuccessful
|
||||||
|
elseif (client_wait_for_reg_response == true and
|
||||||
|
msg_obj["msg_type"] == OCT_LOBBY_MSG_NAK) then
|
||||||
|
OCT_LOG_ERROR("Server rejected registration request, try using a different name")
|
||||||
|
client_connected = 0
|
||||||
|
client_wait_for_reg_response = false
|
||||||
end
|
end
|
||||||
else
|
|
||||||
OCT_LOG_WARNING("Could not decode received json from server");
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,27 @@ function load_termbox_sprite(file_name)
|
||||||
return contents;
|
return contents;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- 127.0.0.1 will come back as "localhost.localdomain"
|
||||||
|
-- I don't think my program can handle this, so just change it back
|
||||||
|
-- Keep this in a function in case we run into others
|
||||||
|
function convert_known_host_to_ip(host)
|
||||||
|
if (host == "localhost.localdomain" or host == "localhost") then
|
||||||
|
return "127.0.0.1"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- taken from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
|
||||||
|
function table_to_string(o)
|
||||||
|
if type(o) == 'table' then
|
||||||
|
local s = '{ '
|
||||||
|
for k,v in pairs(o) do
|
||||||
|
if type(k) ~= 'number' then k = '"'..k..'"' end
|
||||||
|
s = s .. '['..k..'] = ' .. table_to_string(v) .. ','
|
||||||
|
end
|
||||||
|
return s .. '} '
|
||||||
|
else
|
||||||
|
return tostring(o)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ end
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
function oct_loop(key)
|
function oct_loop(key)
|
||||||
if counter == 10000000 then
|
if counter == 1000000 then
|
||||||
oct_send("First message!\nnewline\nnewline\nnewline", "127.0.0.1", "1234");
|
oct_send("First message!\nnewline\nnewline\nnewline", "127.0.0.1", "1234");
|
||||||
oct_send("Second message!\nhey!", "127.0.0.1", "1234");
|
oct_send("Second message!\nhey!", "127.0.0.1", "1234");
|
||||||
counter = 0;
|
counter = 0;
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
require("oct_utils")
|
||||||
|
require("termbox_defs")
|
||||||
|
require("lobby")
|
||||||
|
|
||||||
|
my_ip = ""
|
||||||
|
my_port = ""
|
||||||
|
|
||||||
|
function oct_init(arg, port)
|
||||||
|
OCT_LOG_INFO("Starting test_lobby_client")
|
||||||
|
|
||||||
|
my_port = port
|
||||||
|
|
||||||
|
return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX;
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
first = 1
|
||||||
|
|
||||||
|
function oct_loop(key)
|
||||||
|
lobby_client("127.0.0.1", "2048", "TEST_LOBBY", my_port)
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue