Send broken close message to clients after 1 second
Aka starting to work on timeouting clients
This commit is contained in:
parent
15139ceb68
commit
e914ec3fa2
|
@ -71,4 +71,22 @@ class GameServer
|
||||||
# we do nothing for now
|
# we do nothing for now
|
||||||
# TODO: do something
|
# TODO: do something
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disconnect_client(client, reason = nil)
|
||||||
|
@server.send_ctrl_close(client, reason)
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_tick
|
||||||
|
now = Time.now
|
||||||
|
timeout_ids = []
|
||||||
|
@server.clients.each do |id, client|
|
||||||
|
diff = now - client.last_recv_time
|
||||||
|
timeout_ids.push(id) if diff > 1
|
||||||
|
end
|
||||||
|
|
||||||
|
timeout_ids.each do |id|
|
||||||
|
disconnect_client(@server.clients[id], 'Timeout')
|
||||||
|
@server.clients.delete(id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,12 +15,20 @@ require_relative 'game_server'
|
||||||
require_relative 'message'
|
require_relative 'message'
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
attr_accessor :id, :addr, :vital_sent
|
attr_accessor :id, :addr, :vital_sent, :last_recv_time, :token
|
||||||
|
|
||||||
def initialize(attr = {})
|
def initialize(attr = {})
|
||||||
@id = attr[:id]
|
@id = attr[:id]
|
||||||
@addr = attr[:addr]
|
@addr = attr[:addr]
|
||||||
@vital_sent = 0
|
@vital_sent = 0
|
||||||
|
@token = attr[:token]
|
||||||
|
unless @token.size == 4
|
||||||
|
raise "Invalid client token size\n" \
|
||||||
|
"got=#{@token.size} expected=4\n" \
|
||||||
|
"#{str_hex(@token)}"
|
||||||
|
end
|
||||||
|
|
||||||
|
@last_recv_time = Time.now
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: use or remove
|
# TODO: use or remove
|
||||||
|
@ -36,6 +44,8 @@ class Client
|
||||||
end
|
end
|
||||||
|
|
||||||
class TeeworldsServer
|
class TeeworldsServer
|
||||||
|
attr_accessor :clients
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@verbose = options[:verbose] || false
|
@verbose = options[:verbose] || false
|
||||||
@ip = '127.0.0.1'
|
@ip = '127.0.0.1'
|
||||||
|
@ -132,6 +142,14 @@ class TeeworldsServer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_ctrl_close(client, reason)
|
||||||
|
msg = [NET_CTRLMSG_CLOSE]
|
||||||
|
msg += Packer.pack_str(reason) unless reason.nil?
|
||||||
|
@netbase.peer_token = client.token
|
||||||
|
@netbase.send_packet(msg, 0, control: true, addr: client.addr)
|
||||||
|
# @netbase.peer_token = @server_token
|
||||||
|
end
|
||||||
|
|
||||||
def send_ctrl_with_token(addr, token)
|
def send_ctrl_with_token(addr, token)
|
||||||
msg = [NET_CTRLMSG_TOKEN] + str_bytes(@server_token)
|
msg = [NET_CTRLMSG_TOKEN] + str_bytes(@server_token)
|
||||||
@netbase.peer_token = token
|
@netbase.peer_token = token
|
||||||
|
@ -203,7 +221,7 @@ class TeeworldsServer
|
||||||
puts 'server full drop packet. TODO: tell the client'
|
puts 'server full drop packet. TODO: tell the client'
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
client = Client.new(id:, addr: packet.addr)
|
client = Client.new(id:, addr: packet.addr, token: packet.payload[...4])
|
||||||
@clients[id] = client
|
@clients[id] = client
|
||||||
@netbase.send_packet([NET_CTRLMSG_ACCEPT], 0, control: true, addr: packet.addr)
|
@netbase.send_packet([NET_CTRLMSG_ACCEPT], 0, control: true, addr: packet.addr)
|
||||||
end
|
end
|
||||||
|
@ -254,6 +272,7 @@ class TeeworldsServer
|
||||||
@current_game_tick += 1
|
@current_game_tick += 1
|
||||||
do_snapshot
|
do_snapshot
|
||||||
end
|
end
|
||||||
|
@game_server.on_tick
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
Loading…
Reference in a new issue