diff --git a/res/scripts/socketserver.lua b/res/scripts/socketserver.lua new file mode 100644 index 000000000..518887fa6 --- /dev/null +++ b/res/scripts/socketserver.lua @@ -0,0 +1,103 @@ +lastkeys = nil +server = nil +ST_sockets = {} +nextID = 1 + +local KEY_NAMES = { "A", "B", "s", "S", "<", ">", "^", "v", "R", "L" } + +function ST_stop(id) + local sock = ST_sockets[id] + ST_sockets[id] = nil + sock:close() +end + +function ST_format(id, msg, isError) + local prefix = "Socket " .. id + if isError then + prefix = prefix .. " Error: " + else + prefix = prefix .. " Received: " + end + return prefix .. msg +end + +function ST_error(id, err) + console:error(ST_format(id, err, true)) + ST_stop(id) +end + +function ST_received(id) + local sock = ST_sockets[id] + if not sock then return end + while true do + local p, err = sock:receive(1024) + if p then + console:log(ST_format(id, p:match("^(.-)%s*$"))) + else + if err ~= socket.ERRORS.AGAIN then + console:error(ST_format(id, err, true)) + ST_stop(id) + end + return + end + end +end + +function ST_scankeys() + local keys = emu:getKeys() + if keys ~= lastkeys then + lastkeys = keys + local msg = "[" + for i, k in ipairs(KEY_NAMES) do + if (keys & (1 << (i - 1))) == 0 then + msg = msg .. " " + else + msg = msg .. k; + end + end + msg = msg .. "]\n" + for id, sock in pairs(ST_sockets) do + if sock then sock:send(msg) end + end + end +end + +function ST_accept() + local sock, err = server:accept() + if err then + console:error(ST_format("Accept", err, true)) + return + end + local id = nextID + nextID = id + 1 + ST_sockets[id] = sock + sock:add("received", function() ST_received(id) end) + sock:add("error", function() ST_error(id) end) + console:log(ST_format(id, "Connected")) +end + +callbacks:add("keysRead", ST_scankeys) + +local port = 8888 +server = nil +while not server do + server, err = socket.bind(nil, port) + if err then + if err == "address in use" then + port = port + 1 + else + console:error(ST_format("Bind", err, true)) + break + end + else + local ok + ok, err = server:listen() + if err then + server:close() + console:error(ST_format("Listen", err, true)) + else + console:log("Socket Server Test: Listening on port " .. port) + server:add("received", ST_accept) + end + end +end diff --git a/res/scripts/sockettest.lua b/res/scripts/sockettest.lua new file mode 100644 index 000000000..c5a674576 --- /dev/null +++ b/res/scripts/sockettest.lua @@ -0,0 +1,69 @@ +sockettest = nil +lastkeys = nil + +local KEY_NAMES = { "A", "B", "s", "S", "<", ">", "^", "v", "R", "L" } + +function ST_stop() + if not sockettest then return end + console:log("Socket Test: Shutting down") + sockettest:close() + sockettest = nil +end + +function ST_start() + ST_stop() + console:log("Socket Test: Connecting to 127.0.0.1:8888...") + sockettest = socket.tcp() + sockettest:add("received", ST_received) + sockettest:add("error", ST_error) + if sockettest:connect("127.0.0.1", 8888) then + console:log("Socket Test: Connected") + lastkeys = nil + else + console:log("Socket Test: Failed to connect") + ST_stop() + end +end + +function ST_error(err) + console:error("Socket Test Error: " .. err) + ST_stop() +end + +function ST_received() + while true do + local p, err = sockettest:receive(1024) + if p then + console:log("Socket Test Received: " .. p:match("^(.-)%s*$")) + else + if err ~= socket.ERRORS.AGAIN then + console:error("Socket Test Error: " .. err) + ST_stop() + end + return + end + end +end + +function ST_scankeys() + if not sockettest then return end + local keys = emu:getKeys() + if keys ~= lastkeys then + lastkeys = keys + local msg = "[" + for i, k in ipairs(KEY_NAMES) do + if (keys & (1 << (i - 1))) == 0 then + msg = msg .. " " + else + msg = msg .. k; + end + end + sockettest:send(msg .. "]\n") + end +end + +callbacks:add("start", ST_start) +callbacks:add("stop", ST_stop) +callbacks:add("crashed", ST_stop) +callbacks:add("reset", ST_start) +callbacks:add("keysRead", ST_scankeys)