Fix crash on server messages and add tests for it

This commit is contained in:
ChillerDragon 2022-11-20 10:13:07 +01:00
parent ec92b073e8
commit 1e829c0eaf
4 changed files with 53 additions and 1 deletions

View file

@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../../lib/teeworlds_client'
client = TeeworldsClient.new
connected = false
client.on_connected do |_|
client.rcon_auth(password: 'rcon')
connected = true
end
# this should nicley print the server message
client.on_chat do |_, msg|
puts "[chat] #{msg}"
end
# connect and block main thread
client.connect('localhost', 8377, detach: true)
sleep 0.5 until connected
puts '[test] sending server hello'
client.rcon('say "hello"')
sleep 1
client.rcon('shutdown')
sleep 0.2
# this is also testing a disconnect
# if the server shutdown already
# should never crash
client.disconnect

View file

@ -160,6 +160,12 @@ then
then
fail "Error: server still running rcon shutdown failed"
fi
elif [ "$testname" == "client/srv_say.rb" ]
then
if ! grep -q '^\[chat\].*hello' "$logdir/ruby_client.txt"
then
fail "Error: missing 'hello' chat message in client log"
fi
elif [ "$testname" == "client/multiple_blocks.rb" ]
then
sleep 1

View file

@ -26,6 +26,14 @@ class ChatMesage
end
def to_s
"#{@author.name}: #{@message}"
# server message
return "*** #{@message}" if @client_id == -1
# player message
# should never be from an invalid id
# but lets not crash if servers send weird stuff
name = ''
name = @author.name if @author
"#{name}: #{@message}"
end
end

View file

@ -446,6 +446,9 @@ class TeeworldsClient
# puts "tick"
begin
pck = @s.recvfrom_nonblock(1400)
rescue Errno::ECONNREFUSED
puts 'connection problems ...'
pck = nil
rescue IO::EAGAINWaitReadable
pck = nil
end