Network troubleshooting.
This commit is contained in:
parent
42aa39423f
commit
76beae271c
|
@ -5,7 +5,7 @@ local button = assert( require 'ui.button' )
|
||||||
local strings = assert( require 'strings' )
|
local strings = assert( require 'strings' )
|
||||||
local connecting = {}
|
local connecting = {}
|
||||||
|
|
||||||
local time, ip, port, attempts = 0, 0, 0, 0
|
local time, ip, port, attempts = 0, 0, 0, 1
|
||||||
|
|
||||||
local cancelButton = button{
|
local cancelButton = button{
|
||||||
x = lg.getWidth() / 4,
|
x = lg.getWidth() / 4,
|
||||||
|
@ -43,7 +43,10 @@ end
|
||||||
function connecting.update(dt)
|
function connecting.update(dt)
|
||||||
time = time + dt
|
time = time + dt
|
||||||
|
|
||||||
if time > 5 then
|
if time > 2 then
|
||||||
|
time = 0
|
||||||
|
attempts = attempts + 1
|
||||||
|
server.connect( ip, port )
|
||||||
--return scene.loadScene( scene.game )
|
--return scene.loadScene( scene.game )
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
local socket = assert( require 'socket' )
|
local socket = assert( require 'socket' )
|
||||||
local ms = assert( require 'shared.metaserver' )
|
local ms = assert( require 'shared.metaserver' )
|
||||||
|
local config = assert( require 'config' )
|
||||||
|
|
||||||
local udp = {}
|
local udp = {}
|
||||||
local packet = assert( require 'shared.packet' )
|
local packet = assert( require 'shared.packet' )
|
||||||
|
@ -35,7 +36,7 @@ end
|
||||||
|
|
||||||
function udp.connect( ip, port )
|
function udp.connect( ip, port )
|
||||||
assert( cxn:setpeername( ip, port ) )
|
assert( cxn:setpeername( ip, port ) )
|
||||||
return udp.send( packet.get( packet.heartbeat{ tick = 0, hash = 1234 } ) )
|
return udp.send( packet.get( packet.clientInfo{ username = config.plName } ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
function udp.disconnect( )
|
function udp.disconnect( )
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
assert( assert( require 'socket.http' ).request 'https://api.ipify.org' ) == '142.162.167.92'
|
assert( assert( require 'socket.http' ).request 'https://api.ipify.org' == '142.162.167.92')
|
|
@ -1,3 +1,4 @@
|
||||||
|
--Note, this won't work unless you set up port forwarding!
|
||||||
local SERVERTIMEOUT = 30
|
local SERVERTIMEOUT = 30
|
||||||
local CLIENTTIMEOUT = 1
|
local CLIENTTIMEOUT = 1
|
||||||
|
|
||||||
|
@ -5,8 +6,11 @@ local shared = assert( require '../shared.shared' )
|
||||||
local socket = assert( require 'socket' )
|
local socket = assert( require 'socket' )
|
||||||
local packet = assert( shared.packet )
|
local packet = assert( shared.packet )
|
||||||
local udp = assert( socket.udp() )
|
local udp = assert( socket.udp() )
|
||||||
|
local msip = assert( require 'getip' )
|
||||||
udp:settimeout(0)
|
udp:settimeout(0)
|
||||||
assert( udp:setsockname( socket.dns.toip(socket.dns.gethostname()), shared.metaserver.port ))
|
local localIP = socket.dns.toip(socket.dns.gethostname())
|
||||||
|
print( "Metaserver local IP:", localIP )
|
||||||
|
assert( udp:setsockname( localIP, shared.metaserver.port ))
|
||||||
|
|
||||||
--Servers broadcast their information here.
|
--Servers broadcast their information here.
|
||||||
--The metaserver builds a list of available servers. ( available meaning, "broadcasted in last ten heartbeats" )
|
--The metaserver builds a list of available servers. ( available meaning, "broadcasted in last ten heartbeats" )
|
||||||
|
|
|
@ -18,20 +18,46 @@ local svInfo = packet.serverInfo{ version = 13,
|
||||||
map = "Test Map"
|
map = "Test Map"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local clients = {}
|
||||||
|
local connecting = {}
|
||||||
|
|
||||||
local server = {
|
local server = {
|
||||||
tick = 0,
|
tick = 0,
|
||||||
|
|
||||||
advertised = function()
|
clientInfo = function( clientInfo, ip, port )
|
||||||
print( "Advertised." )
|
local key = ip..port
|
||||||
|
connecting[key] = connecting[key] or {}
|
||||||
|
local client = connecting[key]
|
||||||
|
local nonce = shared.hash.rand()
|
||||||
|
client.nonce = nonce
|
||||||
|
print( "Received connection request from:", ip, port )
|
||||||
|
print( "Sending authentication nonce:", nonce )
|
||||||
|
packet.svChimo{ nonce = nonce }
|
||||||
|
return udp:sendto( packet.get(), ip, port )
|
||||||
|
end,
|
||||||
|
|
||||||
|
clChimo = function( clChimo, ip, port )
|
||||||
|
if not connecting[ ip..port ] then
|
||||||
|
return print( "Old connection attempt from:", ip, port )
|
||||||
|
end
|
||||||
|
local remoteHash = clChimo.hash
|
||||||
|
local clNonce = clChimo.nonce
|
||||||
|
local svNonce = connecting[ip..port].nonce
|
||||||
|
local localHash = shared.hash.hash( clNonce, svNonce )
|
||||||
|
if localHash ~= remoteHash then
|
||||||
|
return print( "Hashes differ:", shared.hash.hex( clNonce ), shared.hash.hex( svNonce ) )
|
||||||
|
end
|
||||||
|
print( "Client connected:", ip, port, localHash )
|
||||||
|
clients[ localHash ] = { ip = ip, port = port }
|
||||||
|
packet.connected{ token = localHash }
|
||||||
|
return udp:sendto( packet.get(), ip, port )
|
||||||
|
end,
|
||||||
|
|
||||||
|
advertised = function( ack, ip, port )
|
||||||
|
print( "Advertised via:", ip, port )
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
local clients = {}
|
|
||||||
|
|
||||||
--Developer convenience function: start the local client program.
|
|
||||||
function server.StartLocalClient()
|
|
||||||
os.execute( "start vision.bat" )
|
|
||||||
end
|
|
||||||
|
|
||||||
function server.Advertise()
|
function server.Advertise()
|
||||||
print( "Advertise." )
|
print( "Advertise." )
|
||||||
|
@ -69,10 +95,9 @@ function server.SetIP( ipString, port )
|
||||||
svInfo.ip = shared.ip.fromString( ipString )
|
svInfo.ip = shared.ip.fromString( ipString )
|
||||||
svInfo.port = port
|
svInfo.port = port
|
||||||
if udp:setsockname( ipString, port ) then
|
if udp:setsockname( ipString, port ) then
|
||||||
print( ipString, port )
|
|
||||||
--Find another port.
|
--Find another port.
|
||||||
elseif port < 65536 then
|
elseif port < 65536 then
|
||||||
print( "Trying IP:", ipString, port )
|
print( "Trying port:", port )
|
||||||
return server.SetIP( ipString, port + 1 )
|
return server.SetIP( ipString, port + 1 )
|
||||||
else
|
else
|
||||||
print( "Could not use IP: "..ipString )
|
print( "Could not use IP: "..ipString )
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
local bit = assert( require 'bit' )
|
||||||
|
--hash of a pair of 32-bit numbers
|
||||||
|
return { hash = bit.bxor, rand = function() return 5 end, hex = bit.tohex }
|
|
@ -1 +1,3 @@
|
||||||
return { ip = '192.168.2.15', port = 42069 }
|
local ms = { ip = '192.168.2.15', port = 42069 }
|
||||||
|
print( "Metaserver public address:", ms.ip, ms.port )
|
||||||
|
return ms
|
|
@ -4,22 +4,25 @@ local ipString = assert( require 'shared.ipstring' )
|
||||||
local packet = {}
|
local packet = {}
|
||||||
local mt = { __index = { new = function( self ) return ffi.new( self.ct ) end } }
|
local mt = { __index = { new = function( self ) return ffi.new( self.ct ) end } }
|
||||||
|
|
||||||
|
local headerByte = 0x41 --Ensure printable characters at start of packets.
|
||||||
|
|
||||||
local function newStruct( t )
|
local function newStruct( t )
|
||||||
assert( not( packet[ t.name ] or packet[ t.netname ] ))
|
assert( not( packet[ t.name ] ))
|
||||||
packet[ t.name ] = t
|
packet[ t.name ] = t
|
||||||
packet[ t.netname ] = t
|
t.netname = headerByte
|
||||||
|
packet[ headerByte ] = t
|
||||||
|
headerByte = headerByte + 1
|
||||||
ffi.cdef(("typedef struct {uint8_t netname;\n%s;\n} %s;"):format( table.concat( t, ";\n" ), t.name ))
|
ffi.cdef(("typedef struct {uint8_t netname;\n%s;\n} %s;"):format( table.concat( t, ";\n" ), t.name ))
|
||||||
t.ct = ffi.typeof( ffi.new( t.name ) )
|
t.ct = ffi.typeof( ffi.new( t.name ) )
|
||||||
t.size = ffi.sizeof( t.ct )
|
t.size = ffi.sizeof( t.ct )
|
||||||
assert( t.size < 500, t.name )
|
assert( t.size < 500, t.name )
|
||||||
setmetatable( t, mt )
|
setmetatable( t, mt )
|
||||||
|
|
||||||
--print( "Packet:", t.name, "Members:", #t + 1, "Size:", t.size, "Alignment:", ffi.alignof( t.ct ) )
|
print( "Packet:", t.name, "Members:", #t + 1, "Size:", t.size, "Alignment:", ffi.alignof( t.ct ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "serverInfo",
|
name = "serverInfo",
|
||||||
netname = 42,
|
|
||||||
"uint8_t players",
|
"uint8_t players",
|
||||||
"uint8_t capacity",
|
"uint8_t capacity",
|
||||||
"ipAddress ip",
|
"ipAddress ip",
|
||||||
|
@ -31,26 +34,38 @@ newStruct{
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "clientInfo",
|
name = "clientInfo",
|
||||||
netname = 22,
|
|
||||||
"char username[31]",
|
"char username[31]",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newStruct{
|
||||||
|
name = "svChimo",
|
||||||
|
"uint32_t nonce",
|
||||||
|
}
|
||||||
|
|
||||||
|
newStruct{
|
||||||
|
name = "clChimo",
|
||||||
|
"uint32_t nonce",
|
||||||
|
"uint32_t hash",
|
||||||
|
}
|
||||||
|
|
||||||
|
newStruct{
|
||||||
|
name = "connected",
|
||||||
|
"uint32_t token",
|
||||||
|
}
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "metaServer",
|
name = "metaServer",
|
||||||
netname = 123,
|
|
||||||
"char padding[300]" --Just a bunch of padding to mitigate amplification.
|
"char padding[300]" --Just a bunch of padding to mitigate amplification.
|
||||||
}
|
}
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "heartbeat",
|
name = "heartbeat",
|
||||||
netname = 69,
|
|
||||||
"uint32_t tick",
|
"uint32_t tick",
|
||||||
"uint32_t hash",
|
"uint32_t hash",
|
||||||
}
|
}
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "insect",
|
name = "insect",
|
||||||
netname = 81,
|
|
||||||
"uint8_t id",
|
"uint8_t id",
|
||||||
"bool dead",
|
"bool dead",
|
||||||
"int8_t hp",
|
"int8_t hp",
|
||||||
|
@ -63,7 +78,6 @@ newStruct{
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "soleil",
|
name = "soleil",
|
||||||
netname = 27,
|
|
||||||
"uint16_t azimuth",
|
"uint16_t azimuth",
|
||||||
"uint16_t altitude",
|
"uint16_t altitude",
|
||||||
"int8_t vazi",
|
"int8_t vazi",
|
||||||
|
@ -72,7 +86,6 @@ newStruct{
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "playerChange",
|
name = "playerChange",
|
||||||
netname = 72,
|
|
||||||
"uint8_t id",
|
"uint8_t id",
|
||||||
"uint8_t role",
|
"uint8_t role",
|
||||||
"char username[31]",
|
"char username[31]",
|
||||||
|
@ -80,19 +93,16 @@ newStruct{
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "chatMessage",
|
name = "chatMessage",
|
||||||
netname = 54,
|
|
||||||
"char cmsg[127]",
|
"char cmsg[127]",
|
||||||
}
|
}
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "command",
|
name = "command",
|
||||||
netname = 32,
|
|
||||||
"char command",
|
"char command",
|
||||||
}
|
}
|
||||||
|
|
||||||
newStruct{
|
newStruct{
|
||||||
name = "advertised",
|
name = "advertised",
|
||||||
netname = 144,
|
|
||||||
"uint32_t time",
|
"uint32_t time",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ local shared = {}
|
||||||
return rq( ... )
|
return rq( ... )
|
||||||
end
|
end
|
||||||
end]]
|
end]]
|
||||||
|
shared.hash = assert( require 'shared.hash' )
|
||||||
shared.ip = assert( require 'shared.ipstring' )
|
shared.ip = assert( require 'shared.ipstring' )
|
||||||
shared.packet = assert( require 'shared.packet' )
|
shared.packet = assert( require 'shared.packet' )
|
||||||
shared.print = assert( require 'shared.print' )
|
shared.print = assert( require 'shared.print' )
|
||||||
|
|
Loading…
Reference in New Issue