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 14namespace dbus { 15namespace impl { 16 17class connection 18{ 19public: 20 boost::atomic<bool> is_paused; 21 DBusConnection *conn; 22 23 connection() 24 : is_paused(true), 25 conn(NULL) 26 { 27 } 28 29 void open(boost::asio::io_service& io, int bus) 30 { 31 error e; 32 conn = dbus_bus_get_private((DBusBusType)bus, e); 33 e.throw_if_set(); 34 35 dbus_connection_set_exit_on_disconnect(conn, false); 36 37 detail::set_watch_timeout_dispatch_functions(conn, io); 38 } 39 40 void open(boost::asio::io_service& io, const string& address) 41 { 42 error e; 43 conn = dbus_connection_open_private(address.c_str(), e); 44 e.throw_if_set(); 45 46 dbus_bus_register(conn, e); 47 e.throw_if_set(); 48 49 dbus_connection_set_exit_on_disconnect(conn, false); 50 51 detail::set_watch_timeout_dispatch_functions(conn, io); 52 } 53 54 ~connection() 55 { 56 dbus_connection_close(conn); 57 dbus_connection_unref(conn); 58 } 59 60 operator DBusConnection *() 61 { 62 return conn; 63 } 64 operator const DBusConnection *() const 65 { 66 return conn; 67 } 68 69 message send_with_reply_and_block(message& m, 70 int timeout_in_milliseconds = -1) 71 { 72 error e; 73 message reply(dbus_connection_send_with_reply_and_block(conn, 74 m, timeout_in_milliseconds, e)); 75 76 e.throw_if_set(); 77 return reply; 78 } 79 80 void send(message& m) 81 { 82 // ignoring message serial for now 83 dbus_connection_send(conn, m, NULL); 84 } 85 86 void send_with_reply(message& m, 87 DBusPendingCall **p, 88 int timeout_in_milliseconds = -1) 89 { 90 dbus_connection_send_with_reply(conn, 91 m, p, timeout_in_milliseconds); 92 } 93 94 // begin asynchronous operation 95 //FIXME should not get io from an argument 96 void start(boost::asio::io_service& io) 97 { 98 bool old_value(true); 99 if(is_paused.compare_exchange_strong(old_value, false)) 100 { 101 // If two threads call connection::async_send() 102 // simultaneously on a paused connection, then 103 // only one will pass the CAS instruction and 104 // only one dispatch_handler will be injected. 105 io.post(detail::dispatch_handler(io, conn)); 106 } 107 } 108 109 void cancel(boost::asio::io_service& io) 110 { 111 bool old_value(false); 112 if(is_paused.compare_exchange_strong(old_value, true)) 113 { 114 //TODO 115 } 116 } 117}; 118 119} // namespace impl 120} // namespace dbus 121 122#endif // DBUS_CONNECTION_IPP 123