Print info packet when sending it
This commit is contained in:
parent
1ef6abf377
commit
f75eaebcee
|
@ -2,18 +2,23 @@ require 'huffman_tw'
|
||||||
|
|
||||||
# Class holding the parsed packet data
|
# Class holding the parsed packet data
|
||||||
class Packet
|
class Packet
|
||||||
attr_reader :flags
|
attr_reader :flags, :payload
|
||||||
attr_reader :payload
|
|
||||||
|
|
||||||
def initialize(data)
|
def initialize(data, prefix = '')
|
||||||
# @data and @payload
|
# @data and @payload
|
||||||
# are strings representing the raw bytes
|
# 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
|
@huffman = Huffman.new
|
||||||
@flags = {}
|
@flags = {}
|
||||||
flags_byte = data[0].unpack("B*")
|
@data = data
|
||||||
|
flags_byte = @data[0].unpack("B*")
|
||||||
parse_flags(flags_byte.first[2..5])
|
parse_flags(flags_byte.first[2..5])
|
||||||
@payload = data[PACKET_HEADER_SIZE..]
|
@payload = @data[PACKET_HEADER_SIZE..]
|
||||||
if flags_compressed
|
if flags_compressed
|
||||||
@payload = @huffman.decompress(@payload.unpack("C*"))
|
@payload = @huffman.decompress(@payload.unpack("C*"))
|
||||||
@payload = @payload.pack("C*")
|
@payload = @payload.pack("C*")
|
||||||
|
@ -24,23 +29,23 @@ class Packet
|
||||||
header = bytes[0..2].join(' ').yellow
|
header = bytes[0..2].join(' ').yellow
|
||||||
token = bytes[3..6].join(' ').green
|
token = bytes[3..6].join(' ').green
|
||||||
payload = bytes[7..].join(' ')
|
payload = bytes[7..].join(' ')
|
||||||
puts " data: #{[header, token, payload].join(' ')}"
|
puts @prefix + " data: #{[header, token, payload].join(' ')}"
|
||||||
print " "
|
print @prefix + " "
|
||||||
print "header".ljust(3 * 3, ' ').yellow
|
print "header".ljust(3 * 3, ' ').yellow
|
||||||
print "token".ljust(4 * 3, ' ').green
|
print "token".ljust(4 * 3, ' ').green
|
||||||
puts "data"
|
puts "data"
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s()
|
def to_s()
|
||||||
puts "Packet"
|
puts @prefix + "Packet"
|
||||||
puts " flags: #{@flags}"
|
puts @prefix + " flags: #{@flags}"
|
||||||
bytes = str_hex(@data).split(' ')
|
bytes = str_hex(@data).split(' ')
|
||||||
# todo: check terminal size?
|
# todo: check terminal size?
|
||||||
max_width = 14
|
max_width = 14
|
||||||
rows = bytes.groups_of(max_width)
|
rows = bytes.groups_of(max_width)
|
||||||
annotate_first_row(rows.first)
|
annotate_first_row(rows.first)
|
||||||
rows[1..].each do |row|
|
rows[1..].each do |row|
|
||||||
print " "
|
print @prefix + " "
|
||||||
puts row.join(' ')
|
puts row.join(' ')
|
||||||
end
|
end
|
||||||
puts ""
|
puts ""
|
||||||
|
|
40
teeworlds.rb
40
teeworlds.rb
|
@ -10,6 +10,35 @@ require_relative 'lib/packet'
|
||||||
require_relative 'lib/chunk'
|
require_relative 'lib/chunk'
|
||||||
require_relative 'lib/server_info'
|
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
|
class TwClient
|
||||||
attr_reader :state
|
attr_reader :state
|
||||||
|
|
||||||
|
@ -22,13 +51,12 @@ class TwClient
|
||||||
@port = 8303
|
@port = 8303
|
||||||
@packet_flags = {}
|
@packet_flags = {}
|
||||||
@ticks = 0
|
@ticks = 0
|
||||||
|
@netbase = NetBase.new
|
||||||
|
@netbase.client_token = @client_token
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_msg(data)
|
def send_msg(data)
|
||||||
# size and flags
|
@netbase.send_packet(data)
|
||||||
header = [0x00, 0x00, 0x01] + str_bytes(@token)
|
|
||||||
msg = header + data
|
|
||||||
@s.send(msg.pack('C*'), 0, @ip, @port)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# does not help because server
|
# does not help because server
|
||||||
|
@ -94,6 +122,7 @@ class TwClient
|
||||||
|
|
||||||
def on_msg_token(data)
|
def on_msg_token(data)
|
||||||
@token = bytes_to_str(data)
|
@token = bytes_to_str(data)
|
||||||
|
@netbase.server_token = @token
|
||||||
puts "Got token #{@token}"
|
puts "Got token #{@token}"
|
||||||
send_msg_connect()
|
send_msg_connect()
|
||||||
end
|
end
|
||||||
|
@ -136,6 +165,7 @@ class TwClient
|
||||||
@port = port
|
@port = port
|
||||||
puts "connecting to #{@ip}:#{@port} .."
|
puts "connecting to #{@ip}:#{@port} .."
|
||||||
@s.connect(ip, port)
|
@s.connect(ip, port)
|
||||||
|
@netbase.connect(@s, @ip, @port)
|
||||||
send_ctrl_with_token
|
send_ctrl_with_token
|
||||||
loop do
|
loop do
|
||||||
tick
|
tick
|
||||||
|
@ -197,7 +227,7 @@ class TwClient
|
||||||
|
|
||||||
data = pck.first
|
data = pck.first
|
||||||
|
|
||||||
packet = Packet.new(data)
|
packet = Packet.new(data, '<')
|
||||||
puts packet.to_s
|
puts packet.to_s
|
||||||
|
|
||||||
# process connless packets data
|
# process connless packets data
|
||||||
|
|
Loading…
Reference in a new issue