vision/src/client/udp.lua

82 lines
1.9 KiB
Lua

local socket = assert( require 'socket' )
local ms = assert( require 'shared.metaserver' )
local config = assert( require 'config' )
local udp = { svInfo = {}, token = false, }
local packet = assert( require 'shared.packet' )
local hash = assert( require 'shared.hash' )
local cxn = assert( socket.udp() )
local mscxn = assert( socket.udp() )
cxn:settimeout( 0 )
mscxn:settimeout( 0 )
assert(mscxn:setpeername( ms.ip, ms.port ))
function udp.receive()
return cxn:receive()
end
udp.deserialise = packet.deserialise
function udp.receiveMeta()
return mscxn:receive()
end
function udp.requestServerList()
print( "Requesting server list." )
packet.get()
packet.metaServer()
packet.clientInfo()
return mscxn:send( packet.get() )
end
function udp.newPacket( tick )
if udp.token then packet.connected{ token = udp.token, tick = tick or 0 } end
end
function udp.setToken( token )
print( "Setting server token:", token )
udp.token = token
end
function udp.setInfo( svInfo )
udp.svInfo = svInfo
end
function udp.answerChallenge( svNonce )
local clNonce = hash.rand()
packet.get()
packet.clChimo{ nonce = clNonce, hash = hash.hash( clNonce, svNonce ) }
print( "Received authentication nonce. Reply:", clNonce, svNonce )
return udp.send()
end
function udp.isValid( ip, port )
local s, e = socket.udp()
if s then s, e = s:setpeername( ip, port ) end
if s then return true
else return nil, e end --temporary socket, gc it, we just want the error
end
function udp.connect( ip, port )
assert( cxn:setpeername( ip, port ) )
print( "Connection request to:", ip, port )
return udp.send( packet.clientInfo{ username = config.plName })
end
function udp.disconnect( tick )
for i = 1, 10 do
udp.newPacket( tick )
packet.disconnect()
udp.send()
end
udp.setToken()
cxn = assert( socket.udp() )
cxn:settimeout( 0 )
end
function udp.send()
return cxn:send( packet.get() )
end
return udp