85 lines
2.0 KiB
Lua
85 lines
2.0 KiB
Lua
local shared = assert( require 'shared' )
|
|
local packet = shared.packet
|
|
local socket = assert( require 'socket' )
|
|
local udp
|
|
local io = assert( io )
|
|
|
|
local svInfo = { 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 }
|
|
|
|
local msIP, msPort
|
|
|
|
local clients = {}
|
|
|
|
--[[do
|
|
local _print = print
|
|
function server.Print(...)
|
|
_print( ... )
|
|
server.logFile:write( table.concat({os.date("!%Y-%m-%d %X"), ...}, "\t"), "\n" )
|
|
server.logFile:flush()
|
|
end
|
|
end
|
|
local print = server.Print]]
|
|
|
|
|
|
--Developer convenience function: start the local client program.
|
|
function server.StartLocalClient()
|
|
os.execute( "start vision.bat" )
|
|
end
|
|
|
|
function server.Advertise()
|
|
udp:sendto( packet.get( packet.serverInfo( svInfo ) ) , msIP, msPort )
|
|
end
|
|
|
|
--Incoming packet.
|
|
function server.Parse( msg, ip, port )
|
|
if not msg then return end
|
|
if not clients[ip] then
|
|
clients[ip] = { ip = ip, port = port }
|
|
end
|
|
|
|
local msgs = packet.deserialise( msg )
|
|
print( "in: ", ip, port, msg, server.tick )
|
|
return server.Parse( udp:receivefrom() ) -- Process other packets.
|
|
end
|
|
|
|
function server.SetIP( ipString, port )
|
|
svInfo.ip = shared.ip.fromString( ipString )
|
|
svInfo.port = port
|
|
end
|
|
|
|
function server.Start()
|
|
udp = assert( socket.udp() )
|
|
udp:settimeout(0)
|
|
server.SetIP( socket.dns.toip(socket.dns.gethostname()), 51312 )
|
|
assert( udp:setsockname( tostring( svInfo.ip ), svInfo.port ))
|
|
server.StartLocalClient()
|
|
repeat
|
|
server.Parse( udp:receivefrom() )
|
|
server.Advance()
|
|
until socket.sleep( 0.1 - (socket.gettime() % 0.1) )
|
|
end
|
|
|
|
function server.Advance()
|
|
server.tick = server.tick + 1
|
|
for id, client in pairs( clients ) do
|
|
udp:sendto( string.format("server: %d client: %d", server.tick, client.tick), client.ip, client.port)
|
|
end
|
|
end
|
|
|
|
function server.NewGame()
|
|
server.tick = 0
|
|
end
|
|
|
|
function server.Quit()
|
|
|
|
end
|
|
|
|
return server.Start() |