init commit
This commit is contained in:
87
uv_server.lua
Normal file
87
uv_server.lua
Normal file
@@ -0,0 +1,87 @@
|
||||
UVADDR = 0x03004508
|
||||
|
||||
server = nil
|
||||
ST_sockets = {}
|
||||
nextID = 1
|
||||
local port = 64222
|
||||
|
||||
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*$")))
|
||||
num = tonumber(p) or 0
|
||||
num = math.min(num,255)
|
||||
emu.memory.iwram:write8(UVADDR,num)
|
||||
|
||||
else
|
||||
if err ~= socket.ERRORS.AGAIN then
|
||||
console:error(ST_format(id, err, true))
|
||||
ST_stop(id)
|
||||
end
|
||||
return
|
||||
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
|
||||
|
||||
|
||||
Server = nil
|
||||
while not server do
|
||||
server, err = socket.bind(nil, port)
|
||||
if err then
|
||||
if err == socket.ERRORS.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
|
||||
|
||||
Reference in New Issue
Block a user