Proper client drop support on the server side

This commit is contained in:
ChillerDragon 2022-11-12 16:21:43 +01:00
parent c770695f22
commit 7422ee9d94
2 changed files with 28 additions and 7 deletions

View file

@ -63,7 +63,7 @@ class GameServer
@server.send_game_info(packet.client, GameInfo.new.to_a)
end
def on_rcon_cmd(chunk, packet)
def on_rcon_cmd(chunk, _packet)
u = Unpacker.new(chunk.data[1..])
cmd = u.get_string
puts "got rcon_cmd=#{cmd}"
@ -78,8 +78,9 @@ class GameServer
# TODO: do something
end
def disconnect_client(client, reason = nil)
@server.send_ctrl_close(client, reason)
def on_client_drop(client, reason = nil)
reason = reason.nil? ? '' : " (#{reason})"
puts "'#{client.player.name}' left the game#{reason}"
end
def on_tick
@ -91,8 +92,7 @@ class GameServer
end
timeout_ids.each do |id|
disconnect_client(@server.clients[id], 'Timeout')
@server.clients.delete(id)
@server.drop_client(@server.clients[id], 'Timeout')
end
end
end

View file

@ -16,13 +16,21 @@ require_relative 'message'
require_relative 'token'
class Client
attr_accessor :id, :addr, :vital_sent, :last_recv_time, :token
attr_accessor :id, :addr, :vital_sent, :last_recv_time, :token, :player
def initialize(attr = {})
@id = attr[:id]
@addr = attr[:addr]
@vital_sent = 0
@last_recv_time = Time.now
@player = Player.new(
id: @id,
local: 0,
team: 0,
name: '(connecting)',
clan: '',
country: -1
)
@token = attr[:token]
SecurityToken.validate(@token)
end
@ -208,7 +216,20 @@ class TeeworldsServer
end
def on_ctrl_close(packet)
puts "Client closed the connection #{packet.addr}"
reason = nil
if packet.payload[2]
u = Unpacker.new(packet.payload[1..])
reason = u.get_string
end
drop_client(packet.client, reason)
end
def drop_client(client, reason = nil)
send_ctrl_close(client, reason)
return if client.nil?
@game_server.on_client_drop(client, reason)
@clients.delete(client.id)
end
def on_ctrl_connect(packet)