116 lines
2.6 KiB
Lua
116 lines
2.6 KiB
Lua
package.cpath = "..\\?.dll;..\\clib\\?.dll;"..package.cpath
|
|
package.path = "..\\?.lua;..\\lualib\\?.lua;"..package.path
|
|
local shared = assert( require 'shared.shared' )
|
|
local packet = shared.packet
|
|
local socket = assert( require 'socket' )
|
|
local mscxn = assert( socket.udp() )
|
|
mscxn:settimeout( 0 )
|
|
assert( mscxn:setpeername( shared.metaserver.ip, shared.metaserver.port ), "Could not connect to metaserver!" )
|
|
local udp
|
|
local io = assert( io )
|
|
|
|
local svInfo = packet.serverInfo{ version = 13,
|
|
players = 0,
|
|
capacity = 255,
|
|
ip = shared.ip.fromString( socket.dns.toip(socket.dns.gethostname()) ),
|
|
port = 51312,
|
|
svname = "New Server",
|
|
map = "Test Map"
|
|
}
|
|
|
|
local server = {
|
|
tick = 0,
|
|
|
|
advertised = function()
|
|
print( "Advertised." )
|
|
end,
|
|
}
|
|
|
|
local clients = {}
|
|
|
|
--Developer convenience function: start the local client program.
|
|
function server.StartLocalClient()
|
|
os.execute( "start vision.bat" )
|
|
end
|
|
|
|
function server.Advertise()
|
|
print( "Advertise." )
|
|
packet.get()
|
|
packet.metaServer()
|
|
packet.serverInfo( svInfo )
|
|
mscxn:send( packet.get() )
|
|
end
|
|
|
|
function server.serverInfo( serverInfo )
|
|
print( packet.getString( serverInfo.svname ) )
|
|
end
|
|
|
|
function server.metaServer( ms )
|
|
server.advertising = true
|
|
end
|
|
|
|
--Incoming packet.
|
|
function server.Parse( msg, ip, port )
|
|
if not msg then return end
|
|
if not clients[ip] then
|
|
print( "New IP:", ip, port )
|
|
clients[ip] = { ip = ip, port = port }
|
|
end
|
|
|
|
local msgs, types = packet.deserialise( msg )
|
|
if msgs then for i = 1, #msgs do
|
|
print( "Received: ", types[i], ip, port );
|
|
( server[ types[i] ] or print )( msgs[i], ip, port )
|
|
end end
|
|
return server.Parse( udp:receivefrom() ) -- Process other packets.
|
|
end
|
|
|
|
function server.SetIP( ipString, port )
|
|
svInfo.ip = shared.ip.fromString( ipString )
|
|
svInfo.port = port
|
|
if udp:setsockname( ipString, port ) then
|
|
print( ipString, port )
|
|
--Find another port.
|
|
elseif port < 65536 then
|
|
print( "Trying IP:", ipString, port )
|
|
return server.SetIP( ipString, port + 1 )
|
|
else
|
|
print( "Could not use IP: "..ipString )
|
|
return error( "Connection failed." )
|
|
end
|
|
end
|
|
|
|
|
|
function server.Start()
|
|
udp = assert( socket.udp() )
|
|
udp:settimeout(0)
|
|
server.SetIP( socket.dns.toip(socket.dns.gethostname()), 51312 )
|
|
print( "Server started:", udp:getsockname() )
|
|
end
|
|
|
|
function server.Advance()
|
|
server.tick = server.tick + 1
|
|
for id, client in pairs( clients ) do
|
|
--
|
|
end
|
|
end
|
|
|
|
function server.NewGame()
|
|
server.tick = 0
|
|
end
|
|
|
|
function server.Quit()
|
|
|
|
end
|
|
|
|
server.Start()
|
|
|
|
function love.update( dt )
|
|
server.Parse( udp:receivefrom() )
|
|
server.Advance()
|
|
if server.tick % 250 == 0 then
|
|
server.Advertise()
|
|
end
|
|
end
|
|
|
|
function love.draw() end |