44Connection::Private::Private(DBusConnection *c, Server::Private *s)
45 : conn(c) , dispatcher(NULL), server(s)
50Connection::Private::Private(DBusBusType
type)
51 : dispatcher(NULL), server(NULL)
55 conn = dbus_bus_get_private(
type, e);
57 if (e)
throw Error(e);
62Connection::Private::~Private()
64 debug_log(
"terminating connection 0x%08x", conn);
68 if (dbus_connection_get_is_connected(conn))
70 std::vector<std::string>::iterator i = names.begin();
72 while (i != names.end())
74 debug_log(
"%s: releasing bus name %s", dbus_bus_get_unique_name(conn), i->c_str());
75 dbus_bus_release_name(conn, i->c_str(), NULL);
78 dbus_connection_close(conn);
80 dbus_connection_unref(conn);
83void Connection::Private::init()
85 dbus_connection_ref(conn);
86 dbus_connection_ref(conn);
89 this, &Connection::Private::disconn_filter_function
92 dbus_connection_add_filter(conn, message_filter_stub, &disconn_filter, NULL);
94 dbus_connection_set_dispatch_status_function(conn, dispatch_status_stub,
this, 0);
95 dbus_connection_set_exit_on_disconnect(conn,
false);
98void Connection::Private::detach_server()
119bool Connection::Private::do_dispatch()
123 if (!dbus_connection_get_is_connected(conn))
132 return dbus_connection_dispatch(conn) != DBUS_DISPATCH_DATA_REMAINS;
135void Connection::Private::dispatch_status_stub(DBusConnection *dc, DBusDispatchStatus status,
void *data)
137 Private *p =
static_cast<Private *
>(data);
141 case DBUS_DISPATCH_DATA_REMAINS:
142 debug_log(
"some dispatching to do on %p", dc);
143 p->dispatcher->queue_connection(p);
146 case DBUS_DISPATCH_COMPLETE:
147 debug_log(
"all dispatching done on %p", dc);
150 case DBUS_DISPATCH_NEED_MEMORY:
151 debug_log(
"connection %p needs memory", dc);
156DBusHandlerResult Connection::Private::message_filter_stub(DBusConnection *conn, DBusMessage *dmsg,
void *data)
162 return slot && !slot->
empty() && slot->
call(msg)
163 ? DBUS_HANDLER_RESULT_HANDLED
164 : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
167bool Connection::Private::disconn_filter_function(
const Message &msg)
169 if (msg.
is_signal(DBUS_INTERFACE_LOCAL,
"Disconnected"))
171 debug_log(
"%p disconnected by local bus", conn);
172 dbus_connection_close(conn);
179DBusDispatchStatus Connection::Private::dispatch_status()
181 return dbus_connection_get_dispatch_status(conn);
184bool Connection::Private::has_something_to_dispatch()
186 return dispatch_status() == DBUS_DISPATCH_DATA_REMAINS;
209 DBusConnection *
conn = priv
210 ? dbus_connection_open_private(address, e)
211 : dbus_connection_open(address, e);
213 if (e)
throw Error(e);
223 : _pvt(p), _timeout(-1)
229 : _pvt(c._pvt), _timeout(c._timeout)
231 dbus_connection_ref(
_pvt->conn);
236 dbus_connection_unref(
_pvt->conn);
241 debug_log(
"registering stubs for connection %p",
_pvt->conn);
253 dbus_connection_set_watch_functions(
255 Dispatcher::Private::on_add_watch,
256 Dispatcher::Private::on_rem_watch,
257 Dispatcher::Private::on_toggle_watch,
262 dbus_connection_set_timeout_functions(
264 Dispatcher::Private::on_add_timeout,
265 Dispatcher::Private::on_rem_timeout,
266 Dispatcher::Private::on_toggle_timeout,
283 bool r = dbus_bus_register(
_pvt->conn, e);
292 return dbus_connection_get_is_connected(
_pvt->conn);
298 dbus_connection_close(
_pvt->conn);
303 dbus_connection_set_exit_on_disconnect(
_pvt->conn, exit);
308 return dbus_bus_set_unique_name(
_pvt->conn, n);
313 return dbus_bus_get_unique_name(
_pvt->conn);
318 dbus_connection_flush(
_pvt->conn);
325 dbus_bus_add_match(
_pvt->conn, rule, e);
329 if (e)
throw Error(e);
337 dbus_bus_remove_match(
_pvt->conn, rule, e);
346 debug_log(
"DBus::Connection::remove_match: %s (%s).",
347 static_cast<DBusError *
>(e)->message,
348 static_cast<DBusError *
>(e)->name);
355 return dbus_connection_add_filter(
_pvt->conn, Private::message_filter_stub, &s, NULL);
361 dbus_connection_remove_filter(
_pvt->conn, Private::message_filter_stub, &s);
366 return dbus_connection_send(
_pvt->conn, msg.
_pvt->msg, serial);
376 reply = dbus_connection_send_with_reply_and_block(
_pvt->conn, msg.
_pvt->msg, this->_timeout, e);
380 reply = dbus_connection_send_with_reply_and_block(
_pvt->conn, msg.
_pvt->msg, timeout, e);
383 if (e)
throw Error(e);
390 DBusPendingCall *pending;
392 if (!dbus_connection_send_with_reply(
_pvt->conn, msg.
_pvt->msg, &pending, timeout))
410 int ret = dbus_bus_request_name(
_pvt->conn, name, flags, e);
414 if (e)
throw Error(e);
421 _pvt->names.push_back(name);
422 std::string match =
"destination='" +
_pvt->names.back() +
"'";
431 unsigned long ul = dbus_bus_get_unix_user(
_pvt->conn, sender, e);
433 if (e)
throw Error(e);
442 bool b = dbus_bus_name_has_owner(
_pvt->conn, name, e);
444 if (e)
throw Error(e);
458 bool b = dbus_bus_start_service_by_name(
_pvt->conn, name, flags, NULL, e);
460 if (e)
throw Error(e);
DXXAPI Dispatcher * default_dispatcher
DXXAPI LogFunction debug_log
bool connected() const
Gets whether the connection is currently open.
void exit_on_disconnect(bool exit)
Set whether _exit() should be called when the connection receives a disconnect signal.
void remove_match(const char *rule, bool throw_on_error)
Removes a previously-added match rule "by value" (the most recently-added identical rule gets removed...
bool has_name(const char *name)
Asks the bus whether a certain name has an owner.
Private(DBusConnection *, Server::Private *=NULL)
void flush()
Blocks until the outgoing message queue is empty.
void request_name(const char *name, int flags=0)
Message send_blocking(Message &msg, int timeout=-1)
Sends a message and blocks a certain time period while waiting for a reply.
bool add_filter(MessageSlot &s)
Adds a message filter.
Dispatcher * setup(Dispatcher *)
bool start_service(const char *name, unsigned long flags)
Starts a service that will request ownership of the given name.
void remove_filter(MessageSlot &s)
Removes a previously-added message filter.
void add_match(const char *rule)
Adds a match rule to match messages going through the message bus.
static Connection ActivationBus()
bool send(const Message &msg, unsigned int *serial=NULL)
Adds a message to the outgoing message queue.
unsigned long sender_unix_uid(const char *sender)
PendingCall send_async(Message &msg, int timeout=-1)
Queues a message to send, as with send(), but also returns a DBusPendingCall used to receive a reply ...
static Connection SessionBus()
bool register_bus()
Registers a connection with the bus.
void set_timeout(int timeout)
bool operator==(const Connection &) const
void disconnect()
Closes a private connection, so no further data can be sent or received.
std::vector< std::string > names
const char * unique_name() const
Gets the unique name of the connection as assigned by the message bus.
void queue_connection(Connection::Private *)
bool is_signal(const char *interface, const char *member) const