Class: Packet
- Inherits:
-
Object
- Object
- Packet
- Defined in:
- lib/packet.rb
Overview
Class holding the parsed packet data
Instance Attribute Summary collapse
-
#addr ⇒ Object
readonly
Returns the value of attribute addr.
-
#client ⇒ Object
Returns the value of attribute client.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
- #annotate_first_row(bytes) ⇒ Object
- #flags_compressed ⇒ Object
- #flags_connless ⇒ Object
- #flags_control ⇒ Object
-
#initialize(data, prefix = '') ⇒ Packet
constructor
A new instance of Packet.
- #to_s ⇒ Object
Constructor Details
#initialize(data, prefix = '') ⇒ Packet
Returns a new instance of Packet.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/packet.rb', line 13 def initialize(data, prefix = '') # @data and @payload # are strings representing the raw bytes # # @prefix is a String that will be displayed # when printing the packet # use '>' and '<' for example to indicate # network direction (client/server) @prefix = prefix @addr = NetAddr.new(nil, nil) @huffman = Huffman.new @client_id = nil @client = nil @data = data flags_byte = @data[0].unpack('B*') @flags = PacketFlags.new(flags_byte.first[2..5]).hash @payload = @data[PACKET_HEADER_SIZE..] return unless flags_compressed @payload = @huffman.decompress(@payload.unpack('C*')) @payload = @payload.pack('C*') end |
Instance Attribute Details
#addr ⇒ Object (readonly)
Returns the value of attribute addr.
10 11 12 |
# File 'lib/packet.rb', line 10 def addr @addr end |
#client ⇒ Object
Returns the value of attribute client.
11 12 13 |
# File 'lib/packet.rb', line 11 def client @client end |
#client_id ⇒ Object
Returns the value of attribute client_id.
11 12 13 |
# File 'lib/packet.rb', line 11 def client_id @client_id end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
10 11 12 |
# File 'lib/packet.rb', line 10 def flags @flags end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
10 11 12 |
# File 'lib/packet.rb', line 10 def payload @payload end |
Instance Method Details
#annotate_first_row(bytes) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/packet.rb', line 36 def annotate_first_row(bytes) header = bytes[0..2].join(' ').yellow token = bytes[3..6].join(' ').green payload = bytes[7..].join(' ') puts @prefix + " data: #{[header, token, payload].join(' ')}" print "#{@prefix} " print 'header'.ljust(3 * 3, ' ').yellow print 'token'.ljust(4 * 3, ' ').green puts 'data' end |
#flags_compressed ⇒ Object
62 63 64 |
# File 'lib/packet.rb', line 62 def flags_compressed @flags[:compressed] end |
#flags_connless ⇒ Object
66 67 68 |
# File 'lib/packet.rb', line 66 def flags_connless @flags[:connection] == false end |
#flags_control ⇒ Object
70 71 72 |
# File 'lib/packet.rb', line 70 def flags_control @flags[:control] end |
#to_s ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/packet.rb', line 47 def to_s 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 "#{@prefix} " puts row.join(' ') end puts '' end |