metaserver-client and metaserver-server connections added.
This commit is contained in:
parent
08c27ba610
commit
ea5b39ebb7
|
@ -1,15 +1,31 @@
|
||||||
local socket = assert( require 'socket' )
|
local socket = assert( require 'socket' )
|
||||||
|
local ms = assert( require 'shared.metaserver' )
|
||||||
|
|
||||||
local udp = {}
|
local udp = {}
|
||||||
local packet = assert( require 'shared.packet' )
|
local packet = assert( require 'shared.packet' )
|
||||||
|
|
||||||
local cxn = assert( socket.udp() )
|
local cxn = assert( socket.udp() )
|
||||||
|
local mscxn = assert( socket.udp() )
|
||||||
cxn:settimeout( 0 )
|
cxn:settimeout( 0 )
|
||||||
|
mscxn:settimeout( 0 )
|
||||||
|
assert(mscxn:setpeername( ms.ip, ms.port ))
|
||||||
|
|
||||||
function udp.receive()
|
function udp.receive()
|
||||||
return cxn:receive()
|
return cxn:receive()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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.isValid( ip, port )
|
function udp.isValid( ip, port )
|
||||||
local s, e = socket.udp()
|
local s, e = socket.udp()
|
||||||
if s then s, e = s:setpeername( ip, port ) end
|
if s then s, e = s:setpeername( ip, port ) end
|
||||||
|
|
|
@ -6,9 +6,10 @@ local packet = assert( require 'shared.packet' )
|
||||||
local menu = assert( require 'client.ui.menu' )
|
local menu = assert( require 'client.ui.menu' )
|
||||||
local strings = assert( require 'client.strings' )
|
local strings = assert( require 'client.strings' )
|
||||||
local fonts = assert( require 'client.ui.fonts' )
|
local fonts = assert( require 'client.ui.fonts' )
|
||||||
|
local metaserver = assert ( require 'client.udp' )
|
||||||
local utf8 = assert( require 'utf8' )
|
local utf8 = assert( require 'utf8' )
|
||||||
|
|
||||||
local browser = {}
|
local browser = { latest = 0, }
|
||||||
|
|
||||||
local test = assert( require 'client.test.browser' )
|
local test = assert( require 'client.test.browser' )
|
||||||
|
|
||||||
|
@ -62,6 +63,7 @@ serverList.selected = false
|
||||||
serverList.x = 25
|
serverList.x = 25
|
||||||
serverList.y = 0
|
serverList.y = 0
|
||||||
serverList.h = 36
|
serverList.h = 36
|
||||||
|
serverList.ips = {}
|
||||||
local serverButtons = serverList.buttons
|
local serverButtons = serverList.buttons
|
||||||
local color = { 1, 0.6, 0.6, 0.1 }
|
local color = { 1, 0.6, 0.6, 0.1 }
|
||||||
local ti = textInput.new{
|
local ti = textInput.new{
|
||||||
|
@ -77,7 +79,7 @@ function ti:callback() return self:enterText( browser.joinIPString ) end
|
||||||
local headerButtons = {
|
local headerButtons = {
|
||||||
|
|
||||||
button{
|
button{
|
||||||
callback = function() return serverList.refresh( test.getTestServers() ) end,
|
callback = function() return serverList.requestServers() end,
|
||||||
text = lg.newText( fonts.midFont, strings.refresh_button ) ,
|
text = lg.newText( fonts.midFont, strings.refresh_button ) ,
|
||||||
color = color,
|
color = color,
|
||||||
x = cw * 32,
|
x = cw * 32,
|
||||||
|
@ -110,49 +112,42 @@ for j, headerButton in ipairs( headerButtons ) do
|
||||||
serverButtons[j] = headerButton
|
serverButtons[j] = headerButton
|
||||||
end
|
end
|
||||||
|
|
||||||
function serverList.refresh( serverInfo )
|
function serverList.requestServers()
|
||||||
local n = #headerButtons + 1
|
for i = 1, 5 do metaserver.requestServerList() end
|
||||||
|
|
||||||
for i, server in ipairs( serverInfo ) do
|
|
||||||
local b = serverButtons[n] or button{}
|
|
||||||
b.space = 0
|
|
||||||
b.x = cw
|
|
||||||
b.w = lg.getWidth()
|
|
||||||
b.y = 27 * i + 145
|
|
||||||
b.h = 24
|
|
||||||
b.color = { 0.3 + 0.1 * (n % 2), 0.3 + 0.1 * (n % 2), 0.8, 0.3 }
|
|
||||||
b.callback = joinServerCallback
|
|
||||||
b.serverInfo = server
|
|
||||||
b.ip = tostring( server.ip )
|
|
||||||
b.port = server.port
|
|
||||||
b.text = serverInfoToText( server )
|
|
||||||
b.active = ( b.y < lg.getHeight() )
|
|
||||||
serverButtons[n] = b
|
|
||||||
n = n + 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = #headerButtons + #serverInfo + 1, #headerButtons + #serverButtons do serverButtons[i] = nil end
|
function serverList.clear( )
|
||||||
|
for i = #headerButtons + 1, #serverButtons do serverButtons[i] = nil end
|
||||||
|
for ip in pairs( serverList.ips ) do serverList.ips[ip] = nil end
|
||||||
|
end
|
||||||
|
|
||||||
|
function serverList.add( serverInfo )
|
||||||
|
local y = 27 * ( #serverButtons - #headerButtons ) + 180
|
||||||
|
local ip = tostring( serverInfo.ip )
|
||||||
|
serverButtons[ #serverButtons + 1] = button{
|
||||||
|
space = 0,
|
||||||
|
x = cw,
|
||||||
|
w = lg.getWidth(),
|
||||||
|
y = y,
|
||||||
|
h = 24,
|
||||||
|
color = { 0.3 + 0.1 * (#serverButtons % 2), 0.3 + 0.1 * (#serverButtons % 2), 0.8, 0.3 },
|
||||||
|
callback = joinServerCallback,
|
||||||
|
serverInfo = serverInfo,
|
||||||
|
ip = ip,
|
||||||
|
port = serverInfo.port,
|
||||||
|
text = serverInfoToText( serverInfo ),
|
||||||
|
active = ( y < lg.getHeight() )}
|
||||||
|
serverList.ips[ ip ] = true
|
||||||
return serverList:paint()
|
return serverList:paint()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local metaServerHandlers = setmetatable(
|
||||||
do
|
{
|
||||||
local rs = serverList.resize
|
default = function() end,
|
||||||
serverList.resize = function( x, y )
|
heartbeat = serverList.clear,
|
||||||
rs( x, y )
|
serverInfo = serverList.add,
|
||||||
for i, button in ipairs( serverButtons ) do
|
},
|
||||||
button.w = x
|
{__index = function( t ) return t.default end })
|
||||||
end
|
|
||||||
return serverList:paint()
|
|
||||||
end
|
|
||||||
|
|
||||||
local ol = serverList.onLoad
|
|
||||||
function serverList:onLoad()
|
|
||||||
serverList.refresh( test.getTestServers() )
|
|
||||||
return ol( serverList )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function serverList.scroll( up )
|
function serverList.scroll( up )
|
||||||
local minY = 170
|
local minY = 170
|
||||||
|
@ -177,7 +172,11 @@ function browser.draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
function browser.update( dt )
|
function browser.update( dt )
|
||||||
|
local p = metaserver.receiveMeta()
|
||||||
|
if not p then return end
|
||||||
|
print( "Receiving server list:", p )
|
||||||
|
local msgs, types = packet.deserialise( p )
|
||||||
|
for i = 1, #msgs do metaServerHandlers[types[i]]( msgs[i] ) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function browser.onLoad( )
|
function browser.onLoad( )
|
||||||
|
@ -195,7 +194,12 @@ function browser.wheelmoved( x, y )
|
||||||
end
|
end
|
||||||
|
|
||||||
function browser.resize( x, y )
|
function browser.resize( x, y )
|
||||||
return serverList.resize( x, y )
|
serverList.resize( x, y )
|
||||||
|
|
||||||
|
for i, button in ipairs( serverButtons ) do
|
||||||
|
button.w = x
|
||||||
|
end
|
||||||
|
return serverList:paint()
|
||||||
end
|
end
|
||||||
|
|
||||||
function browser.mousepressed(x, y, button, istouch, pressed)
|
function browser.mousepressed(x, y, button, istouch, pressed)
|
||||||
|
|
|
@ -5,16 +5,20 @@ local shared = assert( require '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() )
|
||||||
|
udp:settimeout(0)
|
||||||
|
assert( udp:setsockname( shared.metaserver.ip, 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" )
|
||||||
--Clients ask the metaserver for this list ( maybe with some filter? )
|
--Clients ask the metaserver for this list ( maybe with some filter? )
|
||||||
local servers = {}
|
local servers = {}
|
||||||
local clients = {}
|
local clients = {}
|
||||||
|
local tick = 0
|
||||||
|
|
||||||
local handlers = setmetatable({
|
local handlers = setmetatable({
|
||||||
|
|
||||||
serverInfo = function( svInfo, ip, port )
|
serverInfo = function( svInfo, ip, port )
|
||||||
|
print( "Received server info from:", ip, port )
|
||||||
if ip ~= tostring( svInfo.ip ) then return print("Server IP mismatch:", ip, svInfo.ip) end
|
if ip ~= tostring( svInfo.ip ) then return print("Server IP mismatch:", ip, svInfo.ip) end
|
||||||
local t = socket.gettime()
|
local t = socket.gettime()
|
||||||
if not servers[ip] then servers[ip] = { ip = ip, port = port, info = svInfo } end
|
if not servers[ip] then servers[ip] = { ip = ip, port = port, info = svInfo } end
|
||||||
|
@ -34,16 +38,19 @@ local handlers = setmetatable({
|
||||||
end,
|
end,
|
||||||
|
|
||||||
clientInfo = function( clientInfo, ip, port )
|
clientInfo = function( clientInfo, ip, port )
|
||||||
if ip ~= tostring( clientInfo.ip ) then
|
--[[if ip ~= tostring( clientInfo.ip ) then
|
||||||
return print( ip, port, "Client IP mismatch:", clientInfo.ip )
|
return print( ip, port, "Client IP mismatch:", clientInfo.ip )
|
||||||
end
|
end]]
|
||||||
|
|
||||||
if clients[ip] then print( ip, port, "Client request timeout." ) end
|
print( "Client requested server list: ", ip, port )
|
||||||
|
|
||||||
|
clients[ip] = clients[ip] or {}
|
||||||
|
|
||||||
local t = socket.gettime()
|
local t = socket.gettime()
|
||||||
clients[ip].time = t
|
clients[ip].time = t
|
||||||
|
|
||||||
for i, server in ipairs( servers ) do
|
packet.heartbeat{ tick = tick }
|
||||||
|
for ip, server in pairs( servers ) do
|
||||||
packet.serverInfo( server.info )
|
packet.serverInfo( server.info )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,9 +77,9 @@ local function read(msg, ip, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function prune( t )
|
local function prune( t )
|
||||||
print( "Pruning", socket.gettime() )
|
|
||||||
for ip, server in pairs(servers) do
|
for ip, server in pairs(servers) do
|
||||||
if server.time < t - SERVERTIMEOUT then
|
if server.time < t - SERVERTIMEOUT then
|
||||||
|
print( "Pruning server IP:", ip )
|
||||||
servers[ip] = nil
|
servers[ip] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -83,4 +90,5 @@ repeat
|
||||||
read( udp:receivefrom() )
|
read( udp:receivefrom() )
|
||||||
prune( socket.gettime() )
|
prune( socket.gettime() )
|
||||||
io.flush()
|
io.flush()
|
||||||
|
tick = tick + 1
|
||||||
until socket.sleep( 2.0 )
|
until socket.sleep( 2.0 )
|
|
@ -1,6 +1,7 @@
|
||||||
local shared = assert( require 'shared' )
|
local shared = assert( require 'shared' )
|
||||||
local packet = shared.packet
|
local packet = shared.packet
|
||||||
local socket = assert( require 'socket' )
|
local socket = assert( require 'socket' )
|
||||||
|
local ms = shared.metaserver
|
||||||
local udp
|
local udp
|
||||||
local io = assert( io )
|
local io = assert( io )
|
||||||
|
|
||||||
|
@ -17,8 +18,6 @@ local server = {
|
||||||
tick = 0,
|
tick = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
local msIP, msPort = socket.dns.toip(socket.dns.gethostname()), 42069
|
|
||||||
|
|
||||||
local clients = {}
|
local clients = {}
|
||||||
|
|
||||||
--Developer convenience function: start the local client program.
|
--Developer convenience function: start the local client program.
|
||||||
|
@ -30,7 +29,7 @@ function server.Advertise()
|
||||||
print( "Advertise." )
|
print( "Advertise." )
|
||||||
packet.metaServer()
|
packet.metaServer()
|
||||||
packet.serverInfo( svInfo )
|
packet.serverInfo( svInfo )
|
||||||
udp:sendto( packet.get() , msIP, msPort )
|
udp:sendto( packet.get() , ms.ip, ms.port )
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.serverInfo( serverInfo )
|
function server.serverInfo( serverInfo )
|
||||||
|
|
|
@ -5,6 +5,7 @@ local shared = {}
|
||||||
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' )
|
||||||
|
shared.metaserver = assert( require 'shared.metaserver' )
|
||||||
|
|
||||||
--Turn on logging?
|
--Turn on logging?
|
||||||
print = shared.print
|
print = shared.print
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
local socket = assert( require 'socket' )
|
||||||
|
return { ip = socket.dns.toip(socket.dns.gethostname()), port = 42069 }
|
Loading…
Reference in New Issue