poppy-server/main.lua

44 lines
1005 B
Lua
Raw Permalink Normal View History

2021-12-31 18:04:12 +00:00
print("Startup!")
2021-12-31 21:19:26 +00:00
local constants = require("constants")
local rpc = require("rpc")
local socket = require("socket")
2020-08-13 19:01:44 +00:00
2021-12-31 21:19:26 +00:00
local v1_message = {}
require("clientid")(v1_message)
require("world")(v1_message)
2020-08-18 03:35:57 +00:00
2021-12-31 21:19:26 +00:00
local server = assert(socket.udp())
local ip, port = nil, nil
2020-08-13 19:01:44 +00:00
2021-12-31 18:04:12 +00:00
server:settimeout(nil)
2021-12-31 19:13:10 +00:00
assert(server:setsockname("*", 42069))
2021-12-31 21:19:26 +00:00
if not server or not socket then error("failed to listen on socket") end
2020-08-13 19:01:44 +00:00
while true do
2021-12-31 18:04:12 +00:00
::nw_next::
print("iteration start")
2020-08-18 03:35:57 +00:00
local data
data, ip, port = server:receivefrom()
2021-12-31 18:04:12 +00:00
print("received data from " .. ip)
if not data then
local err_code = ip
print(err_code)
goto nw_next
end
2020-08-18 03:35:57 +00:00
2021-12-31 21:19:26 +00:00
for _, req in ipairs(rpc.parse_multiple(data)) do
local response = rpc.eval(v1_message, req)
2021-12-31 19:13:10 +00:00
if response then
2021-12-31 21:19:26 +00:00
local payload = "poppyV001" .. constants.RS .. response
print("sending response to " .. ip .. ":" .. port .. " " .. payload)
server:sendto(payload, ip, port)
2021-12-31 19:13:10 +00:00
else
2021-12-31 21:19:26 +00:00
print(req .. " had no return value!")
2021-12-31 19:13:10 +00:00
end
2020-08-18 03:35:57 +00:00
end
2021-12-31 21:19:26 +00:00
print("iteration over")
2020-08-13 19:01:44 +00:00
end
::nw_break::