Module: BinHelper

Included in:
MiniMqtt::Packet, MiniMqtt::PacketHandler
Defined in:
lib/mini_mqtt/bin_helper.rb

Instance Method Summary collapse

Instance Method Details

#flag_byte(flags) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/mini_mqtt/bin_helper.rb', line 10

def flag_byte flags
  raise "flags must have 8 elements" unless flags.size == 8
  byte = 0
  flags.reverse.each_with_index do |flag, index|
    byte |= 1 << index if flag && flag != 0
  end
  byte
end

#mqtt_utf8_encode(string) ⇒ Object



19
20
21
# File 'lib/mini_mqtt/bin_helper.rb', line 19

def mqtt_utf8_encode string
  ushort(string.length) + string
end

#read_mqtt_encoded_string(stream) ⇒ Object



23
24
25
26
# File 'lib/mini_mqtt/bin_helper.rb', line 23

def read_mqtt_encoded_string stream
  length = read_ushort stream
  stream.read length
end

#read_ushort(stream) ⇒ Object



28
29
30
# File 'lib/mini_mqtt/bin_helper.rb', line 28

def read_ushort stream
  stream.read(2).unpack('n').first
end

#uchar(number) ⇒ Object



2
3
4
# File 'lib/mini_mqtt/bin_helper.rb', line 2

def uchar number
  [number].pack 'C'
end

#ushort(number) ⇒ Object



6
7
8
# File 'lib/mini_mqtt/bin_helper.rb', line 6

def ushort number
  [number].pack 'n'
end