2022-11-08 15:20:46 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
require_relative 'string'
|
|
|
|
require_relative 'array'
|
|
|
|
require_relative 'bytes'
|
|
|
|
require_relative 'network'
|
|
|
|
require_relative 'packet'
|
|
|
|
require_relative 'chunk'
|
|
|
|
require_relative 'net_base'
|
|
|
|
require_relative 'net_addr'
|
|
|
|
require_relative 'packer'
|
2022-11-09 15:46:04 +00:00
|
|
|
require_relative 'game_server'
|
2022-11-11 09:21:48 +00:00
|
|
|
require_relative 'message'
|
|
|
|
|
|
|
|
class Client
|
|
|
|
attr_accessor :id, :addr
|
|
|
|
|
|
|
|
def initialize(attr = {})
|
|
|
|
@id = attr[:id]
|
|
|
|
@addr = attr[:addr]
|
|
|
|
end
|
|
|
|
end
|
2022-11-08 15:20:46 +00:00
|
|
|
|
|
|
|
class TeeworldsServer
|
|
|
|
def initialize(options = {})
|
|
|
|
@verbose = options[:verbose] || false
|
|
|
|
@ip = '127.0.0.1'
|
|
|
|
@port = 8303
|
2022-11-09 15:46:04 +00:00
|
|
|
@game_server = GameServer.new(self)
|
2022-11-11 09:21:48 +00:00
|
|
|
@clients = {}
|
2022-11-08 15:20:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(ip, port)
|
|
|
|
@server_token = (1..4).to_a.map { |_| rand(0..255) }
|
|
|
|
@server_token = @server_token.map { |b| b.to_s(16) }.join
|
|
|
|
puts "server token #{@server_token}"
|
|
|
|
@netbase = NetBase.new
|
|
|
|
NetChunk.reset
|
|
|
|
@ip = ip
|
|
|
|
@port = port
|
|
|
|
puts "listening on #{@ip}:#{@port} .."
|
|
|
|
@s = UDPSocket.new
|
|
|
|
@s.bind(@ip, @port)
|
|
|
|
@netbase.bind(@s)
|
|
|
|
loop do
|
|
|
|
tick
|
2022-11-09 12:55:09 +00:00
|
|
|
# TODO: proper tick speed sleep
|
|
|
|
sleep 0.001
|
2022-11-08 15:20:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-09 15:46:04 +00:00
|
|
|
def on_system_chunk(chunk)
|
|
|
|
puts "got system chunk: #{chunk}"
|
|
|
|
end
|
|
|
|
|
2022-11-11 09:21:48 +00:00
|
|
|
def process_chunk(chunk, packet)
|
2022-11-09 15:46:04 +00:00
|
|
|
unless chunk.sys
|
|
|
|
on_system_chunk(chunk)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
puts "proccess chunk with msg: #{chunk.msg}"
|
|
|
|
case chunk.msg
|
|
|
|
when NETMSG_INFO
|
2022-11-11 09:21:48 +00:00
|
|
|
@game_server.on_info(chunk, packet)
|
2022-11-09 15:46:04 +00:00
|
|
|
else
|
|
|
|
puts "Unsupported system msg: #{chunk.msg}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_client_packet(packet)
|
|
|
|
chunks = BigChungusTheChunkGetter.get_chunks(packet.payload)
|
|
|
|
chunks.each do |chunk|
|
|
|
|
if chunk.flags_vital && !chunk.flags_resend
|
|
|
|
@netbase.ack = (@netbase.ack + 1) % NET_MAX_SEQUENCE
|
|
|
|
puts "got ack: #{@netbase.ack}" if @verbose
|
|
|
|
end
|
2022-11-11 09:21:48 +00:00
|
|
|
process_chunk(chunk, packet)
|
2022-11-09 15:46:04 +00:00
|
|
|
end
|
2022-11-08 15:20:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_ctrl_message(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)
|
2022-11-09 07:40:17 +00:00
|
|
|
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)
|
2022-11-08 15:20:46 +00:00
|
|
|
else
|
|
|
|
puts "Uknown control message #{msg}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_ctrl_with_token(addr, token)
|
|
|
|
msg = [NET_CTRLMSG_TOKEN] + token
|
|
|
|
@netbase.send_packet(msg, 0, control: true, addr:)
|
|
|
|
end
|
|
|
|
|
2022-11-11 09:21:48 +00:00
|
|
|
def send_map(addr)
|
|
|
|
data = []
|
|
|
|
data += Packer.pack_str(@game_server.map.name)
|
|
|
|
data += Packer.pack_int(@game_server.map.crc)
|
|
|
|
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_non_vital_header(size: data.size + 1) +
|
|
|
|
[pack_msg_id(NETMSG_MAP_CHANGE, system: true)] +
|
|
|
|
data
|
|
|
|
@netbase.send_packet(msg, 1, addr:)
|
|
|
|
end
|
|
|
|
|
2022-11-08 15:20:46 +00:00
|
|
|
def on_ctrl_token(packet)
|
|
|
|
u = Unpacker.new(packet.payload[1..])
|
|
|
|
token = u.get_raw(4)
|
|
|
|
# puts "got token #{token.map { |b| b.to_s(16) }.join('')}"
|
|
|
|
send_ctrl_with_token(packet.addr, token)
|
|
|
|
end
|
|
|
|
|
2022-11-09 07:40:17 +00:00
|
|
|
def on_ctrl_keep_alive(packet)
|
|
|
|
puts "Got keep alive from #{packet.addr}" if @verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_ctrl_close(packet)
|
|
|
|
puts "Client closed the connection #{packet.addr}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_ctrl_connect(packet)
|
2022-11-11 09:21:48 +00:00
|
|
|
puts 'got connection, sending accept'
|
|
|
|
|
|
|
|
id = get_next_client_id
|
|
|
|
if id == -1
|
|
|
|
puts 'server full drop packet. TODO: tell the client'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
client = Client.new(id:, addr: packet.addr)
|
|
|
|
@clients[id] = client
|
2022-11-09 15:46:04 +00:00
|
|
|
@netbase.send_packet([NET_CTRLMSG_ACCEPT], 0, control: true, addr: packet.addr)
|
2022-11-09 07:40:17 +00:00
|
|
|
end
|
|
|
|
|
2022-11-08 15:20:46 +00:00
|
|
|
def on_packet(packet)
|
|
|
|
# process connless packets data
|
|
|
|
if packet.flags_control
|
|
|
|
on_ctrl_message(packet)
|
|
|
|
else # process non-connless packets
|
|
|
|
on_client_packet(packet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 09:21:48 +00:00
|
|
|
def get_next_client_id
|
|
|
|
(0..MAX_CLIENTS).each do |i|
|
|
|
|
next if @clients[i]
|
|
|
|
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
|
2022-11-08 15:20:46 +00:00
|
|
|
def tick
|
|
|
|
begin
|
2022-11-11 09:21:48 +00:00
|
|
|
data, sender_inet_addr = @s.recvfrom_nonblock(1400)
|
2022-11-08 15:20:46 +00:00
|
|
|
rescue IO::EAGAINWaitReadable
|
|
|
|
data = nil
|
2022-11-11 09:21:48 +00:00
|
|
|
sender_inet_addr = nil
|
2022-11-08 15:20:46 +00:00
|
|
|
end
|
|
|
|
return unless data
|
|
|
|
|
|
|
|
packet = Packet.new(data, '<')
|
2022-11-11 09:21:48 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
packet.client_id = id
|
|
|
|
end
|
|
|
|
|
2022-11-08 15:20:46 +00:00
|
|
|
puts packet.to_s if @verbose
|
|
|
|
on_packet(packet)
|
|
|
|
end
|
|
|
|
end
|