Class: Packet::BinParser

Inherits:
Object
  • Object
show all
Defined in:
lib/packet/packet_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBinParser

Returns a new instance of BinParser.



5
6
7
8
9
10
11
12
13
14
# File 'lib/packet/packet_parser.rb', line 5

def initialize
  @size = 0
  @data = []
  @remaining = ""
  # 0 => reading length
  # 1 => reading actual data
  @parser_state = 0
  @length_string = ""
  @numeric_length = 0
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/packet/packet_parser.rb', line 3

def data
  @data
end

#length_stringObject

Returns the value of attribute length_string.



3
4
5
# File 'lib/packet/packet_parser.rb', line 3

def length_string
  @length_string
end

#numeric_lengthObject

Returns the value of attribute numeric_length.



3
4
5
# File 'lib/packet/packet_parser.rb', line 3

def numeric_length
  @numeric_length
end

#parser_stateObject

Returns the value of attribute parser_state.



4
5
6
# File 'lib/packet/packet_parser.rb', line 4

def parser_state
  @parser_state
end

#remainingObject

Returns the value of attribute remaining.



3
4
5
# File 'lib/packet/packet_parser.rb', line 3

def remaining
  @remaining
end

Instance Method Details

#extract(new_data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/packet/packet_parser.rb', line 23

def extract new_data
  remaining = new_data

  loop do
    if @parser_state == 0
      length_to_read =  9 - @length_string.length
      len_str,remaining = remaining.unpack("a#{length_to_read}a*")
      break if len_str !~ /^\d+$/
      if len_str.length < length_to_read
        @length_string << len_str
        break
      else
        @length_string << len_str
        @numeric_length = @length_string.to_i
        @parser_state = 1
        if remaining.length < @numeric_length
          @data << remaining
          @numeric_length = @numeric_length - remaining.length
          break
        elsif remaining.length == @numeric_length
          @data << remaining
          yield(@data.join)
          reset
          break
        else
          pack_data,remaining = remaining.unpack("a#{@numeric_length}a*")
          @data << pack_data
          yield(@data.join)
          reset
        end
      end
    elsif @parser_state == 1
      pack_data,remaining = remaining.unpack("a#{@numeric_length}a*")
      if pack_data.length < @numeric_length
        @data << pack_data
        @numeric_length = @numeric_length - pack_data.length
        break
      elsif pack_data.length == @numeric_length
        @data << pack_data
        yield(@data.join)
        reset
        break
      else
        @data << pack_data
        yield(@data.join)
        reset
      end
    end # end of beginning if condition
  end # end of loop do
end

#resetObject



16
17
18
19
20
21
# File 'lib/packet/packet_parser.rb', line 16

def reset
  @data = []
  @parser_state = 0
  @length_string = ""
  @numeric_length = 0
end