From 880f5199aa8b6d6e864d25099e04bad410b76ace Mon Sep 17 00:00:00 2001 From: Aleksandar Kanchev Date: Mon, 9 Jul 2012 18:09:04 +0200 Subject: DBusMessage: add support for custom marshaling Add functions to support querying and manipulating the message body and signature. This is useful for code generators, which can generate custom marshaling functions based on a given IDL. Those functions tend to be optimized and faster than the generic iterator based marshaling. --- dbus/dbus-message.c | 143 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 44 deletions(-) (limited to 'dbus/dbus-message.c') diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index 01c2367c..5908180c 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -411,50 +411,6 @@ set_or_delete_string_field (DBusMessage *message, &value); } -#if 0 -/* Probably we don't need to use this */ -/** - * Sets the signature of the message, i.e. the arguments in the - * message payload. The signature includes only "in" arguments for - * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for - * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from - * what you might expect (it does not include the signature of the - * entire C++-style method). - * - * The signature is a string made up of type codes such as - * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also - * the value of #DBUS_TYPE_INVALID). The macros such as - * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you - * may find it useful to use the string forms, such as - * #DBUS_TYPE_INT32_AS_STRING. - * - * An "unset" or #NULL signature is considered the same as an empty - * signature. In fact dbus_message_get_signature() will never return - * #NULL. - * - * @param message the message - * @param signature the type signature or #NULL to unset - * @returns #FALSE if no memory - */ -static dbus_bool_t -_dbus_message_set_signature (DBusMessage *message, - const char *signature) -{ - _dbus_return_val_if_fail (message != NULL, FALSE); - _dbus_return_val_if_fail (!message->locked, FALSE); - _dbus_return_val_if_fail (signature == NULL || - _dbus_check_is_valid_signature (signature)); - /* can't delete the signature if you have a message body */ - _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 || - signature != NULL); - - return set_or_delete_string_field (message, - DBUS_HEADER_FIELD_SIGNATURE, - DBUS_TYPE_SIGNATURE, - signature); -} -#endif - /* Message Cache * * We cache some DBusMessage to reduce the overhead of allocating @@ -3521,6 +3477,47 @@ dbus_message_get_sender (DBusMessage *message) return v; } +/** + * Sets the signature of the message, i.e. the arguments in the + * message payload. The signature includes only "in" arguments for + * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for + * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from + * what you might expect (it does not include the signature of the + * entire C++-style method). + * + * The signature is a string made up of type codes such as + * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also + * the value of #DBUS_TYPE_INVALID). The macros such as + * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you + * may find it useful to use the string forms, such as + * #DBUS_TYPE_INT32_AS_STRING. + * + * An "unset" or #NULL signature is considered the same as an empty + * signature. In fact dbus_message_get_signature() will never return + * #NULL. + * + * @param message the message + * @param signature the type signature or #NULL to unset + * @returns #FALSE if no memory + */ +dbus_bool_t +dbus_message_set_signature (DBusMessage *message, + const char *signature) +{ + _dbus_return_val_if_fail (message != NULL, FALSE); + _dbus_return_val_if_fail (!message->locked, FALSE); + _dbus_return_val_if_fail (signature == NULL || + _dbus_check_is_valid_signature (signature), FALSE); + /* can't delete the signature if you have a message body */ + _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 || + signature != NULL, FALSE); + + return set_or_delete_string_field (message, + DBUS_HEADER_FIELD_SIGNATURE, + DBUS_TYPE_SIGNATURE, + signature); +} + /** * Gets the type signature of the message, i.e. the arguments in the * message payload. The signature includes only "in" arguments for @@ -4705,6 +4702,64 @@ dbus_message_type_to_string (int type) } } +/** + * Returns pointer to the buffer used to store the message body. + * + * @param message the message + * @return pointer to the message body memory + */ +char* +dbus_message_get_body (DBusMessage *message) { + _dbus_return_val_if_fail (message != NULL, NULL); + + return _dbus_string_get_data(&(message->body)); +} + +/** + * Adjust the length of the message body buffer. The memory will be reallocated + * if the new length is bigger than the already allocated size. + * + * @see dbus_message_get_body_allocated + * @param message the message + * @param length the new length of the body + * @return #TRUE if successful + */ +dbus_bool_t +dbus_message_set_body_length (DBusMessage *message, + int length) { + _dbus_return_val_if_fail (message != NULL, FALSE); + _dbus_return_val_if_fail (length >= 0, FALSE); + + return _dbus_string_set_length(&(message->body), length); +} + +/** + * Gets the length of the message body buffer. + * + * @param message the message + * @param length the new length of the body + * @return the length of the body buffer + */ +int +dbus_message_get_body_length (DBusMessage *message) { + _dbus_return_val_if_fail (message != NULL, 0); + + return _dbus_string_get_length(&(message->body)); +} + +/** + * Gets the allocated memory size used to hold the message body. + * + * @param message the message + * @return size of the allocated message body memory + */ +int +dbus_message_get_body_allocated (DBusMessage *message) { + _dbus_return_val_if_fail (message != NULL, 0); + + return _dbus_string_get_allocated(&(message->body)); +} + /** * Turn a DBusMessage into the marshalled form as described in the D-Bus * specification. -- cgit v1.2.1