Class: DBus::PacketMarshaller
- Inherits:
-
Object
- Object
- DBus::PacketMarshaller
- Defined in:
- lib/dbus/marshall.rb
Overview
D-Bus packet marshaller class
Class that handles the conversion (unmarshalling) of Ruby objects to (binary) payload data.
Instance Attribute Summary collapse
-
#packet ⇒ Object
readonly
The current or result packet.
Instance Method Summary collapse
-
#align(a) ⇒ Object
Align the buffer with NULL (0) bytes on a byte length of a.
-
#append(type, val) ⇒ Object
Append a value val to the packet based on its type.
-
#append_signature(str) ⇒ Object
Append the the signature signature itself to the packet.
-
#append_simple_string(s) ⇒ Object
Append a string of bytes without type.
-
#append_string(str) ⇒ Object
Append the the string str itself to the packet.
-
#array(type) ⇒ Object
Append the array type type to the packet and allow for appending the child elements.
-
#initialize ⇒ PacketMarshaller
constructor
Create a new marshaller, setting the current packet to the empty packet.
-
#struct ⇒ Object
Align and allow for appending struct fields.
Constructor Details
#initialize ⇒ PacketMarshaller
Create a new marshaller, setting the current packet to the empty packet.
252 253 254 |
# File 'lib/dbus/marshall.rb', line 252 def initialize @packet = "" end |
Instance Attribute Details
#packet ⇒ Object (readonly)
The current or result packet. FIXME: allow access only when marshalling is finished
248 249 250 |
# File 'lib/dbus/marshall.rb', line 248 def packet @packet end |
Instance Method Details
#align(a) ⇒ Object
Align the buffer with NULL (0) bytes on a byte length of a.
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/dbus/marshall.rb', line 257 def align(a) case a when 1 when 2, 4, 8 bits = a - 1 @packet = @packet.ljust(@packet.length + bits & ~bits, 0.chr) else raise "Unsupported alignment" end end |
#append(type, val) ⇒ Object
Append a value val to the packet based on its type.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/dbus/marshall.rb', line 306 def append(type, val) type = type.chr if type.kind_of?(Fixnum) type = Type::Parser.new(type).parse[0] if type.kind_of?(String) case type.sigtype when Type::BYTE @packet += val.chr when Type::UINT32 align(4) @packet += [val].pack("L") when Type::UINT64 align(8) @packet += [val].pack("Q") when Type::INT64 align(8) @packet += [val].pack("q") when Type::INT32 align(4) @packet += [val].pack("l") when Type::UINT16 align(2) @packet += [val].pack("S") when Type::INT16 align(2) @packet += [val].pack("s") when Type::DOUBLE align(8) @packet += [val].pack("d") when Type::BOOLEAN align(4) if val @packet += [1].pack("L") else @packet += [0].pack("L") end when Type::OBJECT_PATH append_string(val) when Type::STRING append_string(val) when Type::SIGNATURE append_signature(val) when Type::VARIANT if not val.kind_of?(Array) raise TypeException end vartype, vardata = val vartype = Type::Parser.new(vartype).parse[0] if vartype.kind_of?(String) append_signature(vartype.to_s) align(vartype.alignment) sub = PacketMarshaller.new sub.append(vartype, vardata) @packet += sub.packet when Type::ARRAY if val.kind_of?(Hash) raise TypeException if type.child.sigtype != Type::DICT_ENTRY # Damn ruby rocks here val = val.to_a end if not val.kind_of?(Array) raise TypeException end array(type.child) do val.each do |elem| append(type.child, elem) end end when Type::STRUCT, Type::DICT_ENTRY raise TypeException if not val.kind_of?(Array) if type.sigtype == Type::DICT_ENTRY and val.size != 2 raise TypeException end struct do idx = 0 while val[idx] != nil type.members.each do |subtype| raise TypeException if val[idx] == nil append(subtype, val[idx]) idx += 1 end end end else raise NotImplementedError end end |
#append_signature(str) ⇒ Object
Append the the signature signature itself to the packet.
275 276 277 |
# File 'lib/dbus/marshall.rb', line 275 def append_signature(str) @packet += str.length.chr + str + "\0" end |
#append_simple_string(s) ⇒ Object
Append a string of bytes without type.
301 302 303 |
# File 'lib/dbus/marshall.rb', line 301 def append_simple_string(s) @packet += s + "\0" end |
#append_string(str) ⇒ Object
Append the the string str itself to the packet.
269 270 271 272 |
# File 'lib/dbus/marshall.rb', line 269 def append_string(str) align(4) @packet += [str.length].pack("L") + str + "\0" end |
#array(type) ⇒ Object
Append the array type type to the packet and allow for appending the child elements.
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/dbus/marshall.rb', line 281 def array(type) # Thanks to Peter Rullmann for this line align(4) sizeidx = @packet.size @packet += "ABCD" align(type.alignment) contentidx = @packet.size yield sz = @packet.size - contentidx raise InvalidPacketException if sz > 67108864 @packet[sizeidx...sizeidx + 4] = [sz].pack("L") end |
#struct ⇒ Object
Align and allow for appending struct fields.
295 296 297 298 |
# File 'lib/dbus/marshall.rb', line 295 def struct align(8) yield end |