Class: Protocol::MQTT::Codec::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/mqtt/codec.rb

Overview

Incremental writer producing a BINARY String.

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



86
87
88
# File 'lib/protocol/mqtt/codec.rb', line 86

def initialize
  @buf = +"".b
end

Instance Method Details

#bytesObject



91
92
93
# File 'lib/protocol/mqtt/codec.rb', line 91

def bytes
  @buf.freeze
end

#bytesizeObject



96
97
98
# File 'lib/protocol/mqtt/codec.rb', line 96

def bytesize
  @buf.bytesize
end

#write(str) ⇒ Object



101
102
103
104
# File 'lib/protocol/mqtt/codec.rb', line 101

def write(str)
  @buf << str.b
  self
end

#write_binary(s) ⇒ Object

Raises:



139
140
141
142
143
144
# File 'lib/protocol/mqtt/codec.rb', line 139

def write_binary(s)
  bytes = s.to_s.b
  raise ProtocolError, "binary data too long: #{bytes.bytesize}" if bytes.bytesize > 0xFFFF
  @buf << [bytes.bytesize].pack("n") << bytes
  self
end

#write_string_pair(k, v) ⇒ Object



147
148
149
150
151
# File 'lib/protocol/mqtt/codec.rb', line 147

def write_string_pair(k, v)
  write_utf8(k)
  write_utf8(v)
  self
end

#write_u16(v) ⇒ Object



113
114
115
116
# File 'lib/protocol/mqtt/codec.rb', line 113

def write_u16(v)
  @buf << [v].pack("n")
  self
end

#write_u32(v) ⇒ Object



119
120
121
122
# File 'lib/protocol/mqtt/codec.rb', line 119

def write_u32(v)
  @buf << [v].pack("N")
  self
end

#write_u8(v) ⇒ Object



107
108
109
110
# File 'lib/protocol/mqtt/codec.rb', line 107

def write_u8(v)
  @buf << [v].pack("C")
  self
end

#write_utf8(s) ⇒ Object

Raises:



131
132
133
134
135
136
# File 'lib/protocol/mqtt/codec.rb', line 131

def write_utf8(s)
  bytes = s.to_s.b
  raise ProtocolError, "UTF-8 string too long: #{bytes.bytesize}" if bytes.bytesize > 0xFFFF
  @buf << [bytes.bytesize].pack("n") << bytes
  self
end

#write_vbi(v) ⇒ Object



125
126
127
128
# File 'lib/protocol/mqtt/codec.rb', line 125

def write_vbi(v)
  @buf << VBI.encode(v)
  self
end