From 7ab48580547f336236f9ad19e23950197c44a91f Mon Sep 17 00:00:00 2001 From: Kevin Dewald Date: Mon, 21 Oct 2024 00:17:02 -0700 Subject: [PATCH] Rebased on main for new Message class --- simpledbus/include/simpledbus/base/Message.h | 1 + simpledbus/src/base/Connection.cpp | 2 +- simpledbus/src/base/Message.cpp | 39 ++++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/simpledbus/include/simpledbus/base/Message.h b/simpledbus/include/simpledbus/base/Message.h index 0442e10e..87f0c9f7 100644 --- a/simpledbus/include/simpledbus/base/Message.h +++ b/simpledbus/include/simpledbus/base/Message.h @@ -49,6 +49,7 @@ class Message { bool is_signal(const std::string& interface, const std::string& signal_name) const; bool is_method_call(const std::string& interface, const std::string& method) const; + static Message from_retained(DBusMessage* msg); static Message from_acquired(DBusMessage* msg); static Message create_method_call(const std::string& bus_name, const std::string& path, const std::string& interface, const std::string& method); diff --git a/simpledbus/src/base/Connection.cpp b/simpledbus/src/base/Connection.cpp index a69a8516..1033262f 100644 --- a/simpledbus/src/base/Connection.cpp +++ b/simpledbus/src/base/Connection.cpp @@ -208,7 +208,7 @@ bool Connection::unregister_object_path(const std::string& path) { DBusHandlerResult Connection::static_message_handler(DBusConnection* connection, DBusMessage* message, void* user_data) { Connection* conn = static_cast(user_data); - Message msg(message); + Message msg = Message::from_retained(message); std::string path = msg.get_path(); std::lock_guard lock(conn->_mutex); diff --git a/simpledbus/src/base/Message.cpp b/simpledbus/src/base/Message.cpp index 8d73a2c5..a51dabc8 100644 --- a/simpledbus/src/base/Message.cpp +++ b/simpledbus/src/base/Message.cpp @@ -558,6 +558,16 @@ Holder Message::_extract_generic(DBusMessageIter* iter) { return Holder(); } +Message Message::from_retained(DBusMessage* msg) { + Message message; + if (msg) { + dbus_message_ref(msg); + message._msg = msg; + message._unique_id = _creation_counter++; + } + return message; +} + Message Message::from_acquired(DBusMessage* msg) { Message message; if (msg) { @@ -583,9 +593,30 @@ Message Message::create_error(const Message& msg, const std::string& error_name, return Message::from_acquired(msg_error); } -Message Message::create_signal(std::string path, std::string interface, std::string signal) { +Message Message::create_signal(const std::string& path, const std::string& interface, const std::string& signal) { DBusMessage* msg_signal = dbus_message_new_signal(path.c_str(), interface.c_str(), signal.c_str()); - Message message(msg_signal); - dbus_message_unref(msg_signal); - return message; + return Message::from_acquired(msg_signal); +} + +void Message::_invalidate() { + _unique_id = INVALID_UNIQUE_ID; + _msg = nullptr; + _iter_initialized = false; + _is_extracted = false; + _extracted = Holder(); + +#ifdef DBUS_MESSAGE_ITER_INIT_CLOSED + _iter = DBUS_MESSAGE_ITER_INIT_CLOSED; +#else + // For older versions of DBus, DBUS_MESSAGE_ITER_INIT_CLOSED is not defined. + _iter = DBusMessageIter(); +#endif + _arguments.clear(); } + +void Message::_safe_delete() { + if (is_valid()) { + dbus_message_unref(this->_msg); + _invalidate(); + } +} \ No newline at end of file