Class: RBus::Message::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbus/message/writer.rb

Overview

Writes messages to the Bus via a Transport

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Writer

Returns a new instance of Writer.



29
30
31
# File 'lib/rbus/message/writer.rb', line 29

def initialize(transport)
  @transport = transport
end

Instance Method Details

#send_message(message) ⇒ Object

Marshal and write message



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbus/message/writer.rb', line 34

def send_message(message)

  # The marshaling always uses native encoding, so
  # let's find it.
  endian = ([1].pack('I') == "\1\0\0\0") ? ?l : ?B
  
  body = ''
  args = message.arguments

  # Got arguments for the body? Marshal.
  unless args.nil? || args.empty?
    args.extend(MarshalMixin)
    body = args.dbus_marshal(message.signature)
  end
  
  hf = message.header_fields
  
  # Put together the header
  h_array = [endian, message.type, message.flags] + 
            [VERSION::PROTOCOL, body.length, message.serial, hf]
  
  # Marshal
  h_array.extend(MarshalMixin)        
  header = h_array.dbus_marshal('yyyyuua(yv)')
  
  # Pad
  header += "\0" * (-header.length & 7)
  
  Log.debug(header + body)

  # Send
  @transport.send(header + body)
end