| Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) Benjamin Kietzman (github.com/bkietz) | 
|  | 2 | // | 
|  | 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 
|  | 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|  | 5 |  | 
|  | 6 | #ifndef DBUS_CONNECTION_IPP | 
|  | 7 | #define DBUS_CONNECTION_IPP | 
|  | 8 |  | 
|  | 9 | #include <dbus/dbus.h> | 
|  | 10 | #include <dbus/detail/watch_timeout.hpp> | 
|  | 11 |  | 
|  | 12 | #include <boost/atomic.hpp> | 
|  | 13 |  | 
|  | 14 | namespace dbus { | 
|  | 15 | namespace impl { | 
|  | 16 |  | 
|  | 17 | class connection { | 
|  | 18 | public: | 
|  | 19 | boost::atomic<bool> is_paused; | 
|  | 20 | DBusConnection* conn; | 
|  | 21 |  | 
|  | 22 | connection() : is_paused(true), conn(NULL) {} | 
|  | 23 |  | 
|  | 24 | void open(boost::asio::io_service& io, int bus) { | 
|  | 25 | error e; | 
|  | 26 | conn = dbus_bus_get_private((DBusBusType)bus, e); | 
|  | 27 | e.throw_if_set(); | 
|  | 28 |  | 
|  | 29 | dbus_connection_set_exit_on_disconnect(conn, false); | 
|  | 30 |  | 
|  | 31 | detail::set_watch_timeout_dispatch_functions(conn, io); | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | void open(boost::asio::io_service& io, const string& address) { | 
|  | 35 | error e; | 
|  | 36 | conn = dbus_connection_open_private(address.c_str(), e); | 
|  | 37 | e.throw_if_set(); | 
|  | 38 |  | 
|  | 39 | dbus_bus_register(conn, e); | 
|  | 40 | e.throw_if_set(); | 
|  | 41 |  | 
|  | 42 | dbus_connection_set_exit_on_disconnect(conn, false); | 
|  | 43 |  | 
|  | 44 | detail::set_watch_timeout_dispatch_functions(conn, io); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | ~connection() { | 
|  | 48 | if (conn != NULL) { | 
|  | 49 | dbus_connection_close(conn); | 
|  | 50 | dbus_connection_unref(conn); | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | operator DBusConnection*() { return conn; } | 
|  | 55 | operator const DBusConnection*() const { return conn; } | 
|  | 56 |  | 
|  | 57 | message send_with_reply_and_block(message& m, | 
|  | 58 | int timeout_in_milliseconds = -1) { | 
|  | 59 | error e; | 
|  | 60 | DBusMessage* out = dbus_connection_send_with_reply_and_block( | 
|  | 61 | conn, m, timeout_in_milliseconds, e); | 
|  | 62 | e.throw_if_set(); | 
|  | 63 | message reply(out); | 
|  | 64 |  | 
|  | 65 | return reply; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | void send(message& m) { | 
|  | 69 | // ignoring message serial for now | 
|  | 70 | dbus_connection_send(conn, m, NULL); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | void send_with_reply(message& m, DBusPendingCall** p, | 
|  | 74 | int timeout_in_milliseconds = -1) { | 
|  | 75 | dbus_connection_send_with_reply(conn, m, p, timeout_in_milliseconds); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | // begin asynchronous operation | 
|  | 79 | // FIXME should not get io from an argument | 
|  | 80 | void start(boost::asio::io_service& io) { | 
|  | 81 | bool old_value(true); | 
|  | 82 | if (is_paused.compare_exchange_strong(old_value, false)) { | 
|  | 83 | // If two threads call connection::async_send() | 
|  | 84 | // simultaneously on a paused connection, then | 
|  | 85 | // only one will pass the CAS instruction and | 
|  | 86 | // only one dispatch_handler will be injected. | 
|  | 87 | io.post(detail::dispatch_handler(io, conn)); | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | void cancel(boost::asio::io_service& io) { | 
|  | 92 | bool old_value(false); | 
|  | 93 | if (is_paused.compare_exchange_strong(old_value, true)) { | 
|  | 94 | // TODO | 
|  | 95 | } | 
|  | 96 | } | 
|  | 97 | }; | 
|  | 98 |  | 
|  | 99 | }  // namespace impl | 
|  | 100 | }  // namespace dbus | 
|  | 101 |  | 
|  | 102 | #endif  // DBUS_CONNECTION_IPP |