Add shutdown rcon command

This commit is contained in:
ChillerDragon 2024-01-20 22:32:12 +08:00
parent b49116e190
commit e43218edbb
2 changed files with 17 additions and 4 deletions

View file

@ -106,7 +106,7 @@ class GameServer
puts "'#{packet.client.player.name}' joined the game" puts "'#{packet.client.player.name}' joined the game"
end end
def on_rcon_cmd(chunk, _packet) def on_rcon_cmd(chunk, packet)
u = Unpacker.new(chunk.data[1..]) u = Unpacker.new(chunk.data[1..])
command = u.get_string command = u.get_string
return if call_hook(:rcon_cmd, Context.new(nil, chunk:, packet:, command:)).nil? return if call_hook(:rcon_cmd, Context.new(nil, chunk:, packet:, command:)).nil?
@ -114,7 +114,7 @@ class GameServer
puts "[server] ClientID=#{packet.client.player.id} rcon='#{command}'" puts "[server] ClientID=#{packet.client.player.id} rcon='#{command}'"
if command == 'shutdown' if command == 'shutdown'
@server.shutdown! @server.shutdown!('Server shutdown')
else else
puts "[console] No such command: #{command}:" puts "[console] No such command: #{command}:"
end end
@ -128,6 +128,7 @@ class GameServer
# TODO: we accept any password lol # TODO: we accept any password lol
puts "[server] ClientID=#{packet.client.player.id} addr=#{packet.client.addr} authed (admin)" puts "[server] ClientID=#{packet.client.player.id} addr=#{packet.client.addr} authed (admin)"
packet.client.authed = true packet.client.authed = true
@server.send_rcon_auth_on(packet.client)
end end
def on_input(chunk, packet) def on_input(chunk, packet)
@ -147,6 +148,10 @@ class GameServer
def on_shutdown def on_shutdown
return if call_hook(:shutdown, Context.new(nil)).nil? return if call_hook(:shutdown, Context.new(nil)).nil?
puts '[gameserver] disconnecting all clients ...'
@server.clients.each do |id, client|
@server.send_ctrl_close(client, @server.shutdown_reason)
end
puts '[gameserver] shutting down ...' puts '[gameserver] shutting down ...'
end end

View file

@ -65,7 +65,7 @@ end
class TeeworldsServer class TeeworldsServer
attr_accessor :clients attr_accessor :clients
attr_reader :hooks attr_reader :hooks, :shutdown_reason
def initialize(options = {}) def initialize(options = {})
@verbose = options[:verbose] || false @verbose = options[:verbose] || false
@ -83,10 +83,12 @@ class TeeworldsServer
} }
@thread_running = false @thread_running = false
@is_shutting_down = false @is_shutting_down = false
@shutdown_reason = ''
end end
def shutdown! def shutdown!(reason)
@is_shutting_down = true @is_shutting_down = true
@shutdown_reason = reason
end end
def on_chat(&block) def on_chat(&block)
@ -260,6 +262,12 @@ class TeeworldsServer
@netbase.send_packet(msg, chunks: 1, client:) @netbase.send_packet(msg, chunks: 1, client:)
end end
def send_rcon_auth_on(client)
msg = NetChunk.create_header(vital: true, size: 1, client:) +
[pack_msg_id(NETMSG_RCON_AUTH_ON, system: true)]
@netbase.send_packet(msg, chunks: 1, client:)
end
def send_ready(client) def send_ready(client)
msg = NetChunk.create_header(vital: true, size: 1, client:) + msg = NetChunk.create_header(vital: true, size: 1, client:) +
[pack_msg_id(NETMSG_CON_READY, system: true)] [pack_msg_id(NETMSG_CON_READY, system: true)]