Change chunk header api

This commit is contained in:
ChillerDragon 2022-11-12 10:13:29 +01:00
parent 4813652b82
commit 83c38c4639
4 changed files with 24 additions and 21 deletions

View file

@ -34,7 +34,7 @@ class NetChunk
" data: #{str_hex(data)}"
end
def self.create_non_vital_header(data = { size: 0 })
def self._create_non_vital_header(data = { size: 0 })
flag_bits = '00'
unused_bits = '00'
@ -58,15 +58,18 @@ class NetChunk
#
# It will create a 3 byte chunk header
# represented as an Array of 3 integers
def self.create_vital_header(flags, size, seq = nil)
def self.create_header(opts = { resend: false, vital: false, size: nil, seq: nil })
raise 'Chunk.create_header :size option can not be nil' if opts[:size].nil?
return _create_non_vital_header(opts) unless opts[:vital]
@@sent_vital_chunks += 1
seq = @@sent_vital_chunks if seq.nil?
seq = @@sent_vital_chunks if opts[:seq].nil?
flag_bits = '00'.dup
flag_bits[0] = flags[:resend] ? '1' : '0'
flag_bits[1] = flags[:vital] ? '1' : '0'
flag_bits[0] = opts[:resend] ? '1' : '0'
flag_bits[1] = opts[:vital] ? '1' : '0'
size_bits = size.to_s(2).rjust(12, '0')
size_bits = opts[:size].to_s(2).rjust(12, '0')
# size_bits[0..5]
# size_bits[6..]

View file

@ -82,7 +82,7 @@ class TeeworldsClient
def send_chat(str)
@netbase.send_packet(
NetChunk.create_vital_header({ vital: true }, 4 + str.length) +
NetChunk.create_header(vital: true, size: 4 + str.length) +
[
pack_msg_id(NETMSGTYPE_CL_SAY),
CHAT_ALL,
@ -169,7 +169,7 @@ class TeeworldsClient
data += Packer.pack_str(GAME_NETVERSION)
data += Packer.pack_str('password')
data += Packer.pack_int(CLIENT_VERSION)
msg = NetChunk.create_vital_header({ vital: true }, data.size + 1) +
msg = NetChunk.create_header(vital: true, size: data.size + 1) +
[pack_msg_id(NETMSG_INFO, system: true)] +
data
@ -197,7 +197,7 @@ class TeeworldsClient
data += Packer.pack_str(password)
data += Packer.pack_int(1)
end
msg = NetChunk.create_vital_header({ vital: true }, data.size + 1) +
msg = NetChunk.create_header(vital: true, size: data.size + 1) +
[pack_msg_id(NETMSG_RCON_AUTH, system: true)] +
data
@netbase.send_packet(msg, 1)
@ -206,7 +206,7 @@ class TeeworldsClient
def rcon(command)
data = []
data += Packer.pack_str(command)
msg = NetChunk.create_vital_header({ vital: true }, data.size + 1) +
msg = NetChunk.create_header(vital: true, size: data.size + 1) +
[pack_msg_id(NETMSG_RCON_CMD, system: true)] +
data
@netbase.send_packet(msg, 1)
@ -227,7 +227,7 @@ class TeeworldsClient
end
@netbase.send_packet(
NetChunk.create_vital_header({ vital: true }, data.size + 1) +
NetChunk.create_header(vital: true, size: data.size + 1) +
[pack_msg_id(NETMSGTYPE_CL_STARTINFO, system: false)] +
data
)
@ -235,14 +235,14 @@ class TeeworldsClient
def send_msg_ready
@netbase.send_packet(
NetChunk.create_vital_header({ vital: true }, 1) +
NetChunk.create_header(vital: true, size: 1) +
[pack_msg_id(NETMSG_READY, system: true)]
)
end
def send_enter_game
@netbase.send_packet(
NetChunk.create_vital_header({ vital: true }, 1) +
NetChunk.create_header(vital: true, size: 1) +
[pack_msg_id(NETMSG_ENTERGAME, system: true)]
)
end
@ -275,7 +275,7 @@ class TeeworldsClient
data += Packer.pack_int(inp[:wanted_weapon])
data += Packer.pack_int(inp[:next_weapon])
data += Packer.pack_int(inp[:prev_weapon])
msg = NetChunk.create_non_vital_header(size: data.size + 1) +
msg = NetChunk.createl_header(vital: false, size: data.size + 1) +
[pack_msg_id(NETMSG_INPUT, system: true)] +
data
@netbase.send_packet(msg, 1)

View file

@ -133,33 +133,33 @@ class TeeworldsServer
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_vital_header({ vital: true }, data.size + 1) +
msg = NetChunk.create_header(vital: true, size: data.size + 1) +
[pack_msg_id(NETMSG_MAP_CHANGE, system: true)] +
data
@netbase.send_packet(msg, 1, addr:)
end
def send_ready(addr)
msg = NetChunk.create_vital_header({ vital: true }, 1) +
msg = NetChunk.create_header(vital: true, size: 1) +
[pack_msg_id(NETMSG_CON_READY, system: true)]
@netbase.send_packet(msg, 1, addr:)
end
def send_ready_to_enter(addr)
msg = NetChunk.create_vital_header({ vital: true }, 1) +
msg = NetChunk.create_header(vital: true, size: 1) +
[pack_msg_id(NETMSGTYPE_SV_READYTOENTER, system: false)]
@netbase.send_packet(msg, 1, addr:)
end
def send_server_info(addr, server_info)
msg = NetChunk.create_vital_header({ vital: true }, 1 + server_info.size) +
msg = NetChunk.create_header(vital: true, size: 1 + server_info.size) +
[pack_msg_id(NETMSG_SERVERINFO, system: true)] +
server_info
@netbase.send_packet(msg, 1, addr:)
end
def send_game_info(addr, data)
msg = NetChunk.create_vital_header({ vital: true }, 1 + data.size) +
msg = NetChunk.create_header(vital: true, size: 1 + data.size) +
[pack_msg_id(NETMSGTYPE_SV_GAMEINFO, system: false)] +
data
@netbase.send_packet(msg, 1, addr:)
@ -223,7 +223,7 @@ class TeeworldsServer
data = []
data += Packer.pack_int(@current_game_tick)
data += Packer.pack_int(@current_game_tick - delta_tick)
msg_snap_empty = NetChunk.create_non_vital_header(size: data.size + 1) +
msg_snap_empty = NetChunk.create_header(vital: false, size: data.size + 1) +
[pack_msg_id(NETMSG_SNAPEMPTY, system: true)] +
data
@clients.each do |_id, client|

View file

@ -5,7 +5,7 @@ require_relative '../lib/chunk'
describe 'NetChunk', :net_chunk do
context 'Create vital header' do
it 'Should set the vital flag' do
expect(NetChunk.create_vital_header({ vital: true }, 20, 5)).to eq([64, 20, 5])
expect(NetChunk.create_header(vital: true, size: 20, seq: 5)).to eq([64, 20, 5])
end
end
end