module BinHelper

Public Instance Methods

flag_byte(flags) click to toggle source
# 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) click to toggle source
# File lib/mini_mqtt/bin_helper.rb, line 19
def mqtt_utf8_encode string
  ushort(string.length) + string
end
read_mqtt_encoded_string(stream) click to toggle source
# 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) click to toggle source
# File lib/mini_mqtt/bin_helper.rb, line 28
def read_ushort stream
  stream.read(2).unpack('n').first
end
uchar(number) click to toggle source
# File lib/mini_mqtt/bin_helper.rb, line 2
def uchar number
  [number].pack 'C'
end
ushort(number) click to toggle source
# File lib/mini_mqtt/bin_helper.rb, line 6
def ushort number
  [number].pack 'n'
end