Class: TeeworldsServer
- Inherits:
-
Object
- Object
- TeeworldsServer
- Defined in:
- lib/teeworlds_server.rb
Instance Attribute Summary collapse
-
#clients ⇒ Object
Returns the value of attribute clients.
Instance Method Summary collapse
- #do_snapshot ⇒ Object
- #drop_client(client, reason = nil) ⇒ Object
- #get_next_client_id ⇒ Object
- #get_player_by_id(id) ⇒ Object
-
#initialize(options = {}) ⇒ TeeworldsServer
constructor
A new instance of TeeworldsServer.
- #on_client_packet(packet) ⇒ Object
- #on_ctrl_close(packet) ⇒ Object
- #on_ctrl_connect(packet) ⇒ Object
- #on_ctrl_keep_alive(packet) ⇒ Object
- #on_ctrl_message(packet) ⇒ Object
- #on_ctrl_token(packet) ⇒ Object
- #on_message(chunk, packet) ⇒ Object
- #on_packet(packet) ⇒ Object
- #process_chunk(chunk, packet) ⇒ Object
- #run(ip, port) ⇒ Object
- #send_ctrl_close(client, reason) ⇒ Object
- #send_ctrl_with_token(addr, token) ⇒ Object
- #send_game_info(client, data) ⇒ Object
- #send_map(client) ⇒ Object
- #send_ready(client) ⇒ Object
- #send_ready_to_enter(client) ⇒ Object
- #send_server_info(client, server_info) ⇒ Object
- #send_server_settings(client, server_settings) ⇒ Object
- #tick ⇒ Object
- #tick_start_time(tick) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ TeeworldsServer
Returns a new instance of TeeworldsServer.
63 64 65 66 67 68 69 70 71 |
# File 'lib/teeworlds_server.rb', line 63 def initialize( = {}) @verbose = [:verbose] || false @ip = '127.0.0.1' @port = 8303 @game_server = GameServer.new(self) @clients = {} @current_game_tick = 0 @last_snap_time = Time.now end |
Instance Attribute Details
#clients ⇒ Object
Returns the value of attribute clients.
61 62 63 |
# File 'lib/teeworlds_server.rb', line 61 def clients @clients end |
Instance Method Details
#do_snapshot ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/teeworlds_server.rb', line 303 def do_snapshot delta_tick = -1 # DeltaTick = m_aClients[i].m_LastAckedSnapshot; data = [] data += Packer.pack_int(@current_game_tick) data += Packer.pack_int(@current_game_tick - delta_tick) msg_snap_empty = NetChunk.create_header(vital: false, size: data.size + 1) + [pack_msg_id(NETMSG_SNAPEMPTY, system: true)] + data # data = [] # data += Packer.pack_int(@current_game_tick) # data += Packer.pack_int(@current_game_tick - delta_tick) # msg_snap_single = NetChunk.create_header(vital: false, size: data.size + 1) + # [pack_msg_id(NETMSG_SNAPSINGLE, system: true)] + # data @clients.each do |_id, client| next unless client.in_game? @netbase.send_packet(msg_snap_empty, chunks: 1, client:) end end |
#drop_client(client, reason = nil) ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/teeworlds_server.rb', line 259 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 |
#get_next_client_id ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/teeworlds_server.rb', line 289 def get_next_client_id (0..MAX_CLIENTS).each do |i| next if @clients[i] return i end -1 end |
#get_player_by_id(id) ⇒ Object
325 326 327 |
# File 'lib/teeworlds_server.rb', line 325 def get_player_by_id(id) @clients[id]&.player end |
#on_client_packet(packet) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/teeworlds_server.rb', line 137 def on_client_packet(packet) client = packet.client if client.nil? # TODO: turn this into a silent return # otherwise bad actors can easily trigger this # with handcrafted packets puts 'Error: got client packet from unknown client' exit 1 end chunks = BigChungusTheChunkGetter.get_chunks(packet.payload) chunks.each do |chunk| if chunk.flags_vital && !chunk.flags_resend packet.client.bump_ack puts "got ack: #{packet.client.ack}" if @verbose end process_chunk(chunk, packet) end end |
#on_ctrl_close(packet) ⇒ Object
250 251 252 253 254 255 256 257 |
# File 'lib/teeworlds_server.rb', line 250 def on_ctrl_close(packet) reason = nil if packet.payload[2] u = Unpacker.new(packet.payload[1..]) reason = u.get_string end drop_client(packet.client, reason) end |
#on_ctrl_connect(packet) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/teeworlds_server.rb', line 267 def on_ctrl_connect(packet) id = get_next_client_id if id == -1 puts 'server full drop packet. TODO: tell the client' return end token = bytes_to_str(packet.payload[1..4]) puts "got connection, sending accept (client token: #{token})" client = Client.new(id:, addr: packet.addr, token:) @clients[id] = client @netbase.send_packet([NET_CTRLMSG_ACCEPT], chunks: 0, control: true, client:) end |
#on_ctrl_keep_alive(packet) ⇒ Object
246 247 248 |
# File 'lib/teeworlds_server.rb', line 246 def on_ctrl_keep_alive(packet) puts "Got keep alive from #{packet.addr}" if @verbose end |
#on_ctrl_message(packet) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/teeworlds_server.rb', line 156 def (packet) u = Unpacker.new(packet.payload) msg = u.get_int puts "got ctrl msg: #{msg}" case msg when NET_CTRLMSG_TOKEN then on_ctrl_token(packet) when NET_CTRLMSG_CONNECT then on_ctrl_connect(packet) when NET_CTRLMSG_KEEPALIVE then on_ctrl_keep_alive(packet) when NET_CTRLMSG_CLOSE then on_ctrl_close(packet) else puts "Uknown control message #{msg}" exit(1) end end |
#on_ctrl_token(packet) ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/teeworlds_server.rb', line 238 def on_ctrl_token(packet) u = Unpacker.new(packet.payload[1..]) token = u.get_raw(4) token_str = token.map { |b| b.to_s(16).rjust(2, '0') }.join puts "got token #{token_str}" send_ctrl_with_token(packet.addr, token_str) end |
#on_message(chunk, packet) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/teeworlds_server.rb', line 102 def (chunk, packet) puts "got game chunk: #{chunk}" case chunk.msg when NETMSGTYPE_CL_STARTINFO then @game_server.on_start_info(chunk, packet) when NETMSGTYPE_CL_SAY then @game_server.on_say(chunk, packet) when NETMSGTYPE_CL_EMOTICON then @game_server.on_emoticon(chunk, packet) else puts "Unsupported game msg: #{chunk.msg}" exit(1) end end |
#on_packet(packet) ⇒ Object
280 281 282 283 284 285 286 287 |
# File 'lib/teeworlds_server.rb', line 280 def on_packet(packet) # process connless packets data if packet.flags_control (packet) else # process non-connless packets on_client_packet(packet) end end |
#process_chunk(chunk, packet) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/teeworlds_server.rb', line 114 def process_chunk(chunk, packet) unless chunk.sys (chunk, packet) return end puts "proccess chunk with msg: #{chunk.msg}" if @verbose case chunk.msg when NETMSG_INFO @game_server.on_info(chunk, packet) when NETMSG_READY @game_server.on_ready(chunk, packet) when NETMSG_ENTERGAME @game_server.on_enter_game(chunk, packet) when NETMSG_INPUT @game_server.on_input(chunk, packet) when NETMSG_RCON_CMD @game_server.on_rcon_cmd(chunk, packet) else puts "Unsupported system msg: #{chunk.msg}" exit(1) end end |
#run(ip, port) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/teeworlds_server.rb', line 73 def run(ip, port) @server_token = (1..4).to_a.map { |_| rand(0..255) } @server_token = @server_token.map { |b| b.to_s(16).rjust(2, '0') }.join puts "server token #{@server_token}" @netbase = NetBase.new(verbose: @verbose) NetChunk.reset @ip = ip @port = port puts "listening on #{@ip}:#{@port} .." @s = UDPSocket.new @s.bind(@ip, @port) @netbase.bind(@s) loop do tick # TODO: proper tick speed sleep # replace by blocking network read # m_NetServer # .Wait( # clamp( # int(( # TickStartTime( # m_CurrentGameTick+1)-time_get() # )*1000/time_freq()), # 1, # 1000/SERVER_TICK_SPEED/2)); sleep 0.001 end end |
#send_ctrl_close(client, reason) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/teeworlds_server.rb', line 171 def send_ctrl_close(client, reason) # when clients disconnect # during the connection process # we do not care return if client.nil? msg = [NET_CTRLMSG_CLOSE] msg += Packer.pack_str(reason) unless reason.nil? @netbase.set_peer_token(client.token) @netbase.send_packet(msg, chunks: 0, control: true, client:) # @netbase.set_peer_token(@server_token) end |
#send_ctrl_with_token(addr, token) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/teeworlds_server.rb', line 184 def send_ctrl_with_token(addr, token) msg = [NET_CTRLMSG_TOKEN] + str_bytes(@server_token) @netbase.set_peer_token(token) @netbase.send_packet(msg, chunks: 0, control: true, addr:) # @netbase.set_peer_token(@server_token) end |
#send_game_info(client, data) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/teeworlds_server.rb', line 231 def send_game_info(client, data) msg = NetChunk.create_header(vital: true, size: 1 + data.size, client:) + [pack_msg_id(NETMSGTYPE_SV_GAMEINFO, system: false)] + data @netbase.send_packet(msg, chunks: 1, client:) end |
#send_map(client) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/teeworlds_server.rb', line 191 def send_map(client) data = [] data += Packer.pack_str(@game_server.map.name) data += @game_server.map.crc_arr # poor mans pack_raw() data += Packer.pack_int(@game_server.map.size) data += Packer.pack_int(8) # chunk num? data += Packer.pack_int(MAP_CHUNK_SIZE) data += @game_server.map.sha256_arr # poor mans pack_raw() msg = NetChunk.create_header(vital: true, size: data.size + 1, client:) + [pack_msg_id(NETMSG_MAP_CHANGE, system: true)] + data @netbase.send_packet(msg, chunks: 1, client:) end |
#send_ready(client) ⇒ Object
205 206 207 208 209 |
# File 'lib/teeworlds_server.rb', line 205 def send_ready(client) msg = NetChunk.create_header(vital: true, size: 1, client:) + [pack_msg_id(NETMSG_CON_READY, system: true)] @netbase.send_packet(msg, chunks: 1, client:) end |
#send_ready_to_enter(client) ⇒ Object
211 212 213 214 215 |
# File 'lib/teeworlds_server.rb', line 211 def send_ready_to_enter(client) msg = NetChunk.create_header(vital: true, size: 1, client:) + [pack_msg_id(NETMSGTYPE_SV_READYTOENTER, system: false)] @netbase.send_packet(msg, chunks: 1, client:) end |
#send_server_info(client, server_info) ⇒ Object
224 225 226 227 228 229 |
# File 'lib/teeworlds_server.rb', line 224 def send_server_info(client, server_info) msg = NetChunk.create_header(vital: true, size: 1 + server_info.size, client:) + [pack_msg_id(NETMSG_SERVERINFO, system: true)] + server_info @netbase.send_packet(msg, chunks: 1, client:) end |
#send_server_settings(client, server_settings) ⇒ Object
217 218 219 220 221 222 |
# File 'lib/teeworlds_server.rb', line 217 def send_server_settings(client, server_settings) msg = NetChunk.create_header(vital: true, size: 1 + server_settings.size, client:) + [pack_msg_id(NETMSGTYPE_SV_SERVERSETTINGS, system: false)] + server_settings @netbase.send_packet(msg, chunks: 1, client:) end |
#tick ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/teeworlds_server.rb', line 329 def tick unless @clients.empty? now = Time.now diff = now - @last_snap_time # TODO: replace snaps every second by something more correct if diff > 1 @current_game_tick += 1 do_snapshot end @game_server.on_tick end begin data, sender_inet_addr = @s.recvfrom_nonblock(1400) rescue IO::EAGAINWaitReadable data = nil sender_inet_addr = nil end return unless data packet = Packet.new(data, '<') packet.addr.ip = sender_inet_addr[2] # or 3 idk bot 127.0.0.1 in my local test case packet.addr.port = sender_inet_addr[1] @clients.each do |id, client| next unless packet.addr.eq(client.addr) client.last_recv_time = Time.now packet.client_id = id packet.client = client end puts packet.to_s if @verbose on_packet(packet) end |
#tick_start_time(tick) ⇒ Object
298 299 300 301 |
# File 'lib/teeworlds_server.rb', line 298 def tick_start_time(tick) # TODO: implement this C++ code # m_GameStartTime + (time_freq()*Tick)/SERVER_TICK_SPEED; end |