Print info packet when sending it

This commit is contained in:
ChillerDragon 2022-10-30 11:18:15 +01:00
parent 1ef6abf377
commit f75eaebcee
2 changed files with 51 additions and 16 deletions

View file

@ -2,18 +2,23 @@ require 'huffman_tw'
# Class holding the parsed packet data
class Packet
attr_reader :flags
attr_reader :payload
attr_reader :flags, :payload
def initialize(data)
def initialize(data, prefix = '')
# @data and @payload
# are strings representing the raw bytes
@data = data
#
# @prefix is a String that will be displayed
# when printing the packet
# use '>' and '<' for example to indicate
# network direction (client/server)
@prefix = prefix
@huffman = Huffman.new
@flags = {}
flags_byte = data[0].unpack("B*")
@data = data
flags_byte = @data[0].unpack("B*")
parse_flags(flags_byte.first[2..5])
@payload = data[PACKET_HEADER_SIZE..]
@payload = @data[PACKET_HEADER_SIZE..]
if flags_compressed
@payload = @huffman.decompress(@payload.unpack("C*"))
@payload = @payload.pack("C*")
@ -24,23 +29,23 @@ class Packet
header = bytes[0..2].join(' ').yellow
token = bytes[3..6].join(' ').green
payload = bytes[7..].join(' ')
puts " data: #{[header, token, payload].join(' ')}"
print " "
puts @prefix + " data: #{[header, token, payload].join(' ')}"
print @prefix + " "
print "header".ljust(3 * 3, ' ').yellow
print "token".ljust(4 * 3, ' ').green
puts "data"
end
def to_s()
puts "Packet"
puts " flags: #{@flags}"
puts @prefix + "Packet"
puts @prefix + " flags: #{@flags}"
bytes = str_hex(@data).split(' ')
# todo: check terminal size?
max_width = 14
rows = bytes.groups_of(max_width)
annotate_first_row(rows.first)
rows[1..].each do |row|
print " "
print @prefix + " "
puts row.join(' ')
end
puts ""

View file

@ -10,6 +10,35 @@ require_relative 'lib/packet'
require_relative 'lib/chunk'
require_relative 'lib/server_info'
class NetBase
attr_accessor :client_token, :server_token
def initialize
@ip = nil
@port = nil
@s = nil
end
def connect(socket, ip, port)
@s = socket
@ip = ip
@port = port
end
##
# Sends a packing setting the proper header for you
#
# @param payload [Array] The Integer list representing the data after the header
def send_packet(payload)
header = [0x00, 0x00, 0x01] + str_bytes(@server_token)
data = (header + payload).pack('C*')
@s.send(data, 0, @ip, @port)
p = Packet.new(data, '>')
puts p
end
end
class TwClient
attr_reader :state
@ -22,13 +51,12 @@ class TwClient
@port = 8303
@packet_flags = {}
@ticks = 0
@netbase = NetBase.new
@netbase.client_token = @client_token
end
def send_msg(data)
# size and flags
header = [0x00, 0x00, 0x01] + str_bytes(@token)
msg = header + data
@s.send(msg.pack('C*'), 0, @ip, @port)
@netbase.send_packet(data)
end
# does not help because server
@ -94,6 +122,7 @@ class TwClient
def on_msg_token(data)
@token = bytes_to_str(data)
@netbase.server_token = @token
puts "Got token #{@token}"
send_msg_connect()
end
@ -136,6 +165,7 @@ class TwClient
@port = port
puts "connecting to #{@ip}:#{@port} .."
@s.connect(ip, port)
@netbase.connect(@s, @ip, @port)
send_ctrl_with_token
loop do
tick
@ -197,7 +227,7 @@ class TwClient
data = pck.first
packet = Packet.new(data)
packet = Packet.new(data, '<')
puts packet.to_s
# process connless packets data