def __init__(self, error_name, reply_serial, destination=None, signature=None, body=None, sender=None): """ @param error_name: C{str} DBus error name @param reply_serial: C{int} serial number this message is a reply to @param destination: C{str} DBus bus name for message destination or None @param signature: C{str} DBus signature string for encoding C{self.body} @param body: C{list} of python objects to encode. Objects must match the C{self.signature} @param sender: C{str} name of the originating Bus connection """ if destination: marshal.validateBusName(destination) marshal.validateInterfaceName(error_name) self.error_name = error_name self.reply_serial = marshal.UInt32(reply_serial) self.destination = destination self.signature = signature self.body = body self.sender = sender self._marshal()
def __init__(self, reply_serial, body=None, destination=None, signature=None): """ @param reply_serial: C{int} serial number this message is a reply to @param destination: C{str} DBus bus name for message destination or None @param signature: C{str} DBus signature string for encoding C{self.body} @param body: C{list} of python objects to encode. Objects must match the C{self.signature} """ if destination: marshal.validateBusName(destination) self.reply_serial = marshal.UInt32(reply_serial) self.destination = destination self.signature = signature self.body = body self._marshal()
def _marshal(self, newSerial=True, oobFDs=None): """ Encodes the message into binary format. The resulting binary message is stored in C{self.rawMessage} """ flags = 0 if not self.expectReply: flags |= 0x1 if not self.autoStart: flags |= 0x2 # may be overriden below, depending on oobFDs _headerAttrs = self._headerAttrs # marshal body before headers to know if the 'unix_fd' header is needed if self.signature: binBody = b''.join( marshal.marshal( self.signature, self.body, oobFDs=oobFDs )[1] ) if oobFDs: # copy class based _headerAttrs to add a unix_fds header this # time _headerAttrs = list(self._headerAttrs) _headerAttrs.append(('unix_fds', 9, False)) self.unix_fds = len(oobFDs) else: binBody = b'' self.headers = [] for attr_name, code, is_required in _headerAttrs: hval = getattr(self, attr_name, None) if hval is not None: if attr_name == 'path': hval = marshal.ObjectPath(hval) elif attr_name == 'signature': hval = marshal.Signature(hval) elif attr_name == 'unix_fds': hval = marshal.UInt32(hval) self.headers.append([code, hval]) self.bodyLength = len(binBody) if newSerial: self.serial = DBusMessage._nextSerial DBusMessage._nextSerial += 1 binHeader = b''.join(marshal.marshal( _headerFormat, [ self.endian, self._messageType, flags, self._protocolVersion, self.bodyLength, self.serial, self.headers ], lendian=self.endian == ord('l') )[1]) headerPadding = marshal.pad['header'](len(binHeader)) self.rawHeader = binHeader self.rawPadding = headerPadding self.rawBody = binBody self.rawMessage = b''.join([binHeader, headerPadding, binBody]) if len(self.rawMessage) > self._maxMsgLen: raise error.MarshallingError( 'Marshalled message exceeds maximum message size of %d' % (self._maxMsgLen,), )