Class: Warchat::Network::BinaryReader

Inherits:
Object
  • Object
show all
Defined in:
lib/warchat/network/binary_reader.rb

Constant Summary collapse

TYPES =
[:hash,:array,:int_32,:string,:string,:boolean,:int_64]

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ BinaryReader

Returns a new instance of BinaryReader.



5
6
7
# File 'lib/warchat/network/binary_reader.rb', line 5

def initialize socket
  @socket = socket
end

Instance Method Details

#arrayObject



30
31
32
# File 'lib/warchat/network/binary_reader.rb', line 30

def array
  (1..(int_32)).map(&method(:parse_next))
end

#booleanObject



50
51
52
# File 'lib/warchat/network/binary_reader.rb', line 50

def boolean
  byte == "\001"
end

#byteObject



22
23
24
# File 'lib/warchat/network/binary_reader.rb', line 22

def byte
  substream(1)
end

#hashObject



34
35
36
37
38
# File 'lib/warchat/network/binary_reader.rb', line 34

def hash
  Hash[*(1..(int_32)).map do
    [string,parse_next] 
  end.flatten(1)]
end

#int_64Object



46
47
48
# File 'lib/warchat/network/binary_reader.rb', line 46

def int_64
  [substream(8).unpack('L_').first.to_s(16)].pack('H*').reverse.unpack('H*').first.hex
end

#parse_next(*args) ⇒ Object



10
11
12
# File 'lib/warchat/network/binary_reader.rb', line 10

def parse_next *args  
  send TYPES[byte.unpack('C').first-1]
end

#stringObject



26
27
28
# File 'lib/warchat/network/binary_reader.rb', line 26

def string
  substream(int_32)
end

#substream(l) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/warchat/network/binary_reader.rb', line 14

def substream l
  sub = @socket.read(l) 
  until sub.length >= l
    sub << @socket.read(l-sub.length)      
  end
  sub
end