dbus-cxx
connection.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later OR BSD-3-Clause
2/***************************************************************************
3 * Copyright (C) 2009,2010 by Rick L. Vinyard, Jr. *
4 * rvinyard@cs.nmsu.edu *
5 * Copyright (C) 2019 by Robert Middleton *
6 * robert.middleton@rm5248.com *
7 * *
8 * This file is part of the dbus-cxx library. *
9 ***************************************************************************/
10#include <stdint.h>
11#include <dbus-cxx/signal.h>
15#include <dbus-cxx/dbus-cxx-config.h>
16#include <deque>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21#include "enums.h"
22#include <sigc++/sigc++.h>
23#include <future>
24#include <queue>
25
26#ifndef DBUSCXX_CONNECTION_H
27#define DBUSCXX_CONNECTION_H
28
30#define DBUSCXX_NAME_FLAG_ALLOW_REPLACEMENT 0x01
32#define DBUSCXX_NAME_FLAG_REPLACE_EXISTING 0x02
34#define DBUSCXX_NAME_FLAG_DO_NOT_QUEUE 0x04
35
36#define DBUSCXX_INTERFACE_INTROSPECTABLE "org.freedesktop.DBus.Introspectable"
38#define DBUSCXX_INTROSPECT_1_0_XML_NAMESPACE "http://www.freedesktop.org/standards/dbus"
40#define DBUSCXX_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
42#define DBUSCXX_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"
44#define DBUSCXX_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<!DOCTYPE node PUBLIC \""\
45 DBUSCXX_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER "\"\n\"" DBUSCXX_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER "\">\n"
46
47
48namespace DBus {
49class Message;
50class Object;
51class ObjectPathHandler;
52class ObjectProxy;
53class PendingCall;
54class ReturnMessage;
55class SignalMessage;
56class Timeout;
57class Watch;
58class ThreadDispatcher;
59class ErrorMessage;
60class DBusDaemonProxy;
61
62namespace priv {
63class Transport;
64}
65
73class Connection : public std::enable_shared_from_this<Connection> {
74
75private:
77
78 Connection( std::string address );
79
80public:
88 static std::shared_ptr<Connection> create( BusType type );
89
100 static std::shared_ptr<Connection> create( std::string address );
101
102 ~Connection();
103
105 operator bool() const;
106
108 bool is_valid() const;
109
111 bool is_registered() const;
112
117 bool bus_register();
118
120 std::string unique_name() const;
121
129 RequestNameResponse request_name( const std::string& name, unsigned int flags = 0 );
130
137 ReleaseNameResponse release_name( const std::string& name );
138
154 bool name_has_owner( const std::string& name ) const;
155
162 StartReply start_service( const std::string& name, uint32_t flags = 0 ) const;
163
164 bool add_match( const std::string& rule );
165
166 void add_match_nonblocking( const std::string& rule );
167
168 bool remove_match( const std::string& rule );
169
170 bool is_connected() const;
171
172 bool is_authenticated() const;
173
174 bool is_anonymous() const;
175
176 const char* server_id() const;
177
184 uint32_t send( const std::shared_ptr<const Message> message );
185
190 Connection& operator<<( std::shared_ptr<const Message> msg );
191
201 std::shared_ptr<ReturnMessage> send_with_reply_blocking( std::shared_ptr<const CallMessage> msg, int timeout_milliseconds = -1 );
202
206 std::shared_ptr<ReturnMessage> send_with_reply_blocking_notimeout( std::shared_ptr<const CallMessage> msg );
207
213 void flush();
214
216
232
233 int unix_fd() const;
234
235 int socket() const;
236
238
247 sigc::signal<void()>& signal_needs_dispatch();
248
257 std::shared_ptr<Object> create_object( const std::string& path,
259
269 RegistrationStatus register_object( std::shared_ptr<Object> object,
271
280 bool change_object_calling_thread( std::shared_ptr<Object> object,
281 ThreadForCalling calling );
282
291 bool change_object_proxy_calling_thread( std::shared_ptr<ObjectProxy> object,
292 ThreadForCalling calling );
293
294 std::shared_ptr<ObjectProxy> create_object_proxy( const std::string& path,
296
297 std::shared_ptr<ObjectProxy> create_object_proxy( const std::string& destination, const std::string& path,
299
307 bool register_object_proxy( std::shared_ptr<ObjectProxy> obj,
309
310
312
313 bool unregister_object_proxy( std::shared_ptr<ObjectProxy> obj );
314
321 bool unregister_object( const std::string& path );
322
334 template<typename... T_arg>
335 std::shared_ptr<SignalProxy<T_arg...> > create_free_signal_proxy( const SignalMatchRule& rule,
337 std::shared_ptr<SignalProxy<T_arg...> > sig;
338 sig = SignalProxy<T_arg...>::create( rule );
339 this->add_free_signal_proxy( sig, calling );
340 return sig;
341 }
342
346 std::shared_ptr<SignalProxyBase> add_free_signal_proxy( std::shared_ptr<SignalProxyBase> Signal,
348
355 bool remove_free_signal_proxy( std::shared_ptr<SignalProxyBase> proxy );
356
358 const std::vector<std::shared_ptr<SignalProxyBase>> get_free_signal_proxies();
359
361 std::vector<std::shared_ptr<SignalProxyBase>> get_free_signal_proxies( const std::string& interface_name );
362
364 std::vector<std::shared_ptr<SignalProxyBase>> get_free_signal_proxies( const std::string& interface_name, const std::string& member );
365
370 template <class T_arg>
371 std::shared_ptr<Signal<T_arg> > create_free_signal( const std::string& path, const std::string& interface_name, const std::string& member ) {
372 std::shared_ptr<Signal<T_arg> > sig;
373 sig = Signal<T_arg>::create( path, interface_name, member );
374 sig->set_connection( shared_from_this() );
375 return sig;
376 }
377
378 std::string introspect( const std::string& destination, const std::string& path );
379
390 void set_dispatching_thread( std::thread::id tid );
391
405 void add_thread_dispatcher( std::weak_ptr<ThreadDispatcher> disp );
406
407private:
414
422 uint32_t write_single_message( std::shared_ptr<const Message> msg );
423
425
427
428 std::shared_ptr<ReturnMessage> send_with_reply_blocking_impl( std::shared_ptr<const CallMessage> msg, int timeout_milliseconds, bool disable_timeout );
429
435 void send_error_on_handler_result( std::shared_ptr<const CallMessage> msg, HandlerResult result );
436
437 void process_call_message( std::shared_ptr<const CallMessage> msg );
438 void process_signal_message( std::shared_ptr<const SignalMessage> msg );
439
440 std::thread::id thread_id_from_calling( ThreadForCalling calling );
441
442private:
443 class priv_data;
444
445 DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
446};
447
448inline
449std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<DBus::Message> msg ) {
450 if( !ptr ) { return ptr; }
451
452 *ptr << msg;
453 return ptr;
454}
455
456inline
457std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<const DBus::Message> msg ) {
458 if( !ptr ) { return ptr; }
459
460 *ptr << msg;
461 return ptr;
462}
463
464inline
465std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<DBus::ReturnMessage> msg ) {
466 if( !ptr ) { return ptr; }
467
468 *ptr << msg;
469 return ptr;
470}
471
472inline
473std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<const DBus::ReturnMessage> msg ) {
474 if( !ptr ) { return ptr; }
475
476 *ptr << msg;
477 return ptr;
478}
479
480inline
481std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<DBus::SignalMessage> msg ) {
482 if( !ptr ) { return ptr; }
483
484 *ptr << msg;
485 return ptr;
486}
487
488inline
489std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<const DBus::SignalMessage> msg ) {
490 if( !ptr ) { return ptr; }
491
492 *ptr << msg;
493 return ptr;
494}
495
496inline
497std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<DBus::ErrorMessage> msg ) {
498 if( !ptr ) { return ptr; }
499
500 *ptr << msg;
501 return ptr;
502}
503
504inline
505std::shared_ptr<DBus::Connection> operator<<( std::shared_ptr<DBus::Connection> ptr, std::shared_ptr<const DBus::ErrorMessage> msg ) {
506 if( !ptr ) { return ptr; }
507
508 *ptr << msg;
509 return ptr;
510}
511
512} /* namespace DBus */
513
514#endif
Connection point to the DBus.
Definition: connection.h:73
int unix_fd() const
Definition: connection.cpp:884
uint32_t send(const std::shared_ptr< const Message > message)
Queues up the message to be sent on the bus.
Definition: connection.cpp:383
bool unregister_object(const std::string &path)
Unregister an object that has been added with register_object.
Definition: connection.cpp:1013
RequestNameResponse request_name(const std::string &name, unsigned int flags=0)
Request the given name on the bus.
Definition: connection.cpp:236
static std::shared_ptr< Connection > create(BusType type)
Connects to a bus daemon.
Definition: connection.cpp:180
bool is_authenticated() const
Definition: connection.cpp:365
~Connection()
Definition: connection.cpp:199
bool is_connected() const
Definition: connection.cpp:359
bool remove_match(const std::string &rule)
Definition: connection.cpp:335
bool is_anonymous() const
Definition: connection.cpp:371
void process_call_message(std::shared_ptr< const CallMessage > msg)
Definition: connection.cpp:697
std::shared_ptr< Object > create_object(const std::string &path, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Create and return a new object, registering the object automatically.
Definition: connection.cpp:906
void send_error_on_handler_result(std::shared_ptr< const CallMessage > msg, HandlerResult result)
Send an error back to the calling application based on HandlerResult.
Definition: connection.cpp:845
bool add_match(const std::string &rule)
Definition: connection.cpp:305
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
void set_dispatching_thread(std::thread::id tid)
Set the ID of the thread that all of the dispatching hapens from.
Definition: connection.cpp:1198
bool name_has_owner(const std::string &name) const
Check to see if the given name currently has an owner.
Definition: connection.cpp:284
sigc::signal< void()> & signal_needs_dispatch()
This signal is emitted whenever we need to be dispatched.
Definition: connection.cpp:902
void add_thread_dispatcher(std::weak_ptr< ThreadDispatcher > disp)
Add a thread dispatcher that will handle messages for a given thread.
Definition: connection.cpp:1215
std::shared_ptr< ReturnMessage > send_with_reply_blocking(std::shared_ptr< const CallMessage > msg, int timeout_milliseconds=-1)
Send a CallMessage, and wait for the reply.
Definition: connection.cpp:579
void flush()
Flushes all data out to the bus.
Definition: connection.cpp:587
std::shared_ptr< ObjectProxy > create_object_proxy(const std::string &path, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Definition: connection.cpp:1001
Connection & operator<<(std::shared_ptr< const Message > msg)
Blindly sends the message on the connection.
Definition: connection.cpp:403
std::shared_ptr< SignalProxy< T_arg... > > create_free_signal_proxy(const SignalMatchRule &rule, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Create and return a signal proxy that lets you listen to signals sent on the DBus as a free proxy.
Definition: connection.h:335
bool has_messages_to_send()
Definition: connection.cpp:896
std::string unique_name() const
Gets the unique name of the connection as assigned by the message bus.
Definition: connection.cpp:230
void process_single_message()
Definition: connection.cpp:653
std::thread::id thread_id_from_calling(ThreadForCalling calling)
Definition: connection.cpp:1314
Connection(BusType type)
Definition: connection.cpp:121
void notify_dispatcher_or_dispatch()
Depending on what thread this is called from, will either notify the dispatcher that we need to be di...
Definition: connection.cpp:1205
void remove_invalid_threaddispatchers_and_associated_objects()
Definition: connection.cpp:1234
int socket() const
Definition: connection.cpp:890
RegistrationStatus register_object(std::shared_ptr< Object > object, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Register an object with this connection.
Definition: connection.cpp:918
bool change_object_proxy_calling_thread(std::shared_ptr< ObjectProxy > object, ThreadForCalling calling)
Change the thread that the signals on this ObjectProxy will be called from.
Definition: connection.cpp:1259
std::shared_ptr< SignalProxyBase > add_free_signal_proxy(std::shared_ptr< SignalProxyBase > Signal, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Adds the given signal proxy to the connection.
Definition: connection.cpp:1042
std::shared_ptr< ReturnMessage > send_with_reply_blocking_impl(std::shared_ptr< const CallMessage > msg, int timeout_milliseconds, bool disable_timeout)
Definition: connection.cpp:409
void process_signal_message(std::shared_ptr< const SignalMessage > msg)
Definition: connection.cpp:764
std::shared_ptr< ReturnMessage > send_with_reply_blocking_notimeout(std::shared_ptr< const CallMessage > msg)
A timeout-less version of send_with_reply_blocking().
Definition: connection.cpp:583
std::shared_ptr< Signal< T_arg > > create_free_signal(const std::string &path, const std::string &interface_name, const std::string &member)
Create a free signal, that when it is emitted will send that signal over the DBus.
Definition: connection.h:371
bool remove_free_signal_proxy(std::shared_ptr< SignalProxyBase > proxy)
Remove a free signal proxy, so that it will not be called anymore.
Definition: connection.cpp:1086
DispatchStatus dispatch_status() const
Definition: connection.cpp:608
const char * server_id() const
Definition: connection.cpp:377
bool is_valid() const
True if this is a valid connection; false otherwise.
Definition: connection.cpp:206
std::string introspect(const std::string &destination, const std::string &path)
Definition: connection.cpp:1164
bool is_registered() const
True if this connection is already registered.
Definition: connection.cpp:226
bool change_object_calling_thread(std::shared_ptr< Object > object, ThreadForCalling calling)
Change the thread that the methods on this object will be called from.
Definition: connection.cpp:983
void add_match_nonblocking(const std::string &rule)
Definition: connection.cpp:330
StartReply start_service(const std::string &name, uint32_t flags=0) const
start_service
Definition: connection.cpp:288
ReleaseNameResponse release_name(const std::string &name)
Release the specified name, after requesting it via request_name.
Definition: connection.cpp:264
uint32_t write_single_message(std::shared_ptr< const Message > msg)
Write a single message, return the serial of this message.
Definition: connection.cpp:602
bool unregister_object_proxy(ObjectProxy *obj)
Definition: connection.cpp:1291
DispatchStatus dispatch()
Dispatch the connection.
Definition: connection.cpp:614
const std::vector< std::shared_ptr< SignalProxyBase > > get_free_signal_proxies()
Gets all the free signal handlers.
Definition: connection.cpp:1128
bool register_object_proxy(std::shared_ptr< ObjectProxy > obj, ThreadForCalling calling=ThreadForCalling::DispatcherThread)
Add an ObjectProxy to this Connection.
Definition: connection.cpp:1279
bool bus_register()
Registers this connection with the bus.
Definition: connection.cpp:210
Object proxies are local proxies that provide local methods and signals for remote objects with dbus ...
Definition: objectproxy.h:45
A special MatchRule for signals.
Definition: matchrule.h:45
Definition: signalproxy.h:26
Definition: signal.h:20
Definition: transport.h:22
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
DispatchStatus
Definition: enums.h:89
std::shared_ptr< DBus::Connection > operator<<(std::shared_ptr< DBus::Connection > ptr, std::shared_ptr< DBus::Message > msg)
Definition: connection.h:449
StartReply
Definition: enums.h:108
BusType
Definition: enums.h:16
ReleaseNameResponse
Definition: enums.h:167
DataType type(const uint8_t &)
Definition: types.h:139
HandlerResult
Definition: enums.h:95
RequestNameResponse
Response status to attempting to register a name on the bus.
Definition: enums.h:156
RegistrationStatus
Definition: enums.h:119
ThreadForCalling
Gives hints to the connection as to which thread should be the one to call the methods on the given o...
Definition: enums.h:133
@ DispatcherThread
Always call methods for this object from the dispatcher thread.