xref: /openbmc/boost-dbus/include/dbus/connection_service.hpp (revision da3eeb6afd298e879dbc9a049c7bcdd9c848baba)
191cdbe46SBenjamin Kietzman // Copyright (c) Benjamin Kietzman (github.com/bkietz)
291cdbe46SBenjamin Kietzman //
391cdbe46SBenjamin Kietzman // Distributed under the Boost Software License, Version 1.0. (See accompanying
491cdbe46SBenjamin Kietzman // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
591cdbe46SBenjamin Kietzman 
6fc79e461SBenjamin Kietzman #ifndef DBUS_CONNECTION_SERVICE_HPP
7fc79e461SBenjamin Kietzman #define DBUS_CONNECTION_SERVICE_HPP
8fc79e461SBenjamin Kietzman 
9d620effbSBenjamin Kietzman #include <boost/asio.hpp>
10*da3eeb6aSEd Tanous #include <boost/asio/io_service.hpp>
11d620effbSBenjamin Kietzman 
12a1051121SBenjamin Kietzman #include <dbus/detail/async_send_op.hpp>
13*da3eeb6aSEd Tanous #include <dbus/element.hpp>
14*da3eeb6aSEd Tanous #include <dbus/error.hpp>
15*da3eeb6aSEd Tanous #include <dbus/message.hpp>
16fc79e461SBenjamin Kietzman 
17a3cc3d57SBenjamin Kietzman #include <dbus/impl/connection.ipp>
18a3cc3d57SBenjamin Kietzman 
19d620effbSBenjamin Kietzman namespace dbus {
20cd8b76a3SBenjamin Kietzman namespace bus {
21cd8b76a3SBenjamin Kietzman static const int session = DBUS_BUS_SESSION;
22cd8b76a3SBenjamin Kietzman static const int system = DBUS_BUS_SYSTEM;
23cd8b76a3SBenjamin Kietzman static const int starter = DBUS_BUS_STARTER;
24cd8b76a3SBenjamin Kietzman }  // namespace bus
25cd8b76a3SBenjamin Kietzman 
2617826fe7SBenjamin Kietzman class filter;
2717826fe7SBenjamin Kietzman class match;
28*da3eeb6aSEd Tanous class connection;
2917826fe7SBenjamin Kietzman 
30*da3eeb6aSEd Tanous class connection_service : public boost::asio::detail::service_base<connection_service> {
31d620effbSBenjamin Kietzman  public:
32a3cc3d57SBenjamin Kietzman   typedef impl::connection implementation_type;
33d620effbSBenjamin Kietzman 
34*da3eeb6aSEd Tanous   inline explicit connection_service(boost::asio::io_service& io)
35*da3eeb6aSEd Tanous       : boost::asio::detail::service_base<connection_service>(io) {}
36d620effbSBenjamin Kietzman 
37*da3eeb6aSEd Tanous   inline void construct(implementation_type& impl) {}
38d620effbSBenjamin Kietzman 
39*da3eeb6aSEd Tanous   inline void destroy(implementation_type& impl) {}
40d620effbSBenjamin Kietzman 
41*da3eeb6aSEd Tanous   inline void shutdown_service() {
42cd8b76a3SBenjamin Kietzman     // TODO is there anything that needs shutting down?
43d620effbSBenjamin Kietzman   }
44d620effbSBenjamin Kietzman 
45*da3eeb6aSEd Tanous   inline void open(implementation_type& impl, const string& address) {
46*da3eeb6aSEd Tanous     boost::asio::io_service& io = this->get_io_service();
47d620effbSBenjamin Kietzman 
48cfc0655fSBenjamin Kietzman     impl.open(io, address);
49d620effbSBenjamin Kietzman   }
50d620effbSBenjamin Kietzman 
51*da3eeb6aSEd Tanous   inline void open(implementation_type& impl, const int bus = bus::system) {
52*da3eeb6aSEd Tanous     boost::asio::io_service& io = this->get_io_service();
53d620effbSBenjamin Kietzman 
54cfc0655fSBenjamin Kietzman     impl.open(io, bus);
55cd8b76a3SBenjamin Kietzman   }
56d620effbSBenjamin Kietzman 
57*da3eeb6aSEd Tanous   inline message send(implementation_type& impl, message& m) {
58b55ed5d5SBenjamin Kietzman     return impl.send_with_reply_and_block(m);
59d620effbSBenjamin Kietzman   }
60d620effbSBenjamin Kietzman 
61d620effbSBenjamin Kietzman   template <typename Duration>
62*da3eeb6aSEd Tanous   inline message send(implementation_type& impl, message& m, const Duration& timeout) {
63cd8b76a3SBenjamin Kietzman     if (timeout == Duration::zero()) {
64d620effbSBenjamin Kietzman       // TODO this can return false if it failed
65b55ed5d5SBenjamin Kietzman       impl.send(m);
66d620effbSBenjamin Kietzman       return message();
67d620effbSBenjamin Kietzman     } else {
68*da3eeb6aSEd Tanous       return impl.send_with_reply_and_block(
69*da3eeb6aSEd Tanous           m, std::chrono::milliseconds(timeout).count());
70d620effbSBenjamin Kietzman     }
71d620effbSBenjamin Kietzman   }
72d620effbSBenjamin Kietzman 
73d620effbSBenjamin Kietzman   template <typename MessageHandler>
74d620effbSBenjamin Kietzman   inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
75d620effbSBenjamin Kietzman                                        void(boost::system::error_code, message))
76*da3eeb6aSEd Tanous       async_send(implementation_type& impl, message& m,
77*da3eeb6aSEd Tanous                  BOOST_ASIO_MOVE_ARG(MessageHandler) handler) {
782003615dSBenjamin Kietzman     // begin asynchronous operation
792003615dSBenjamin Kietzman     impl.start(this->get_io_service());
802003615dSBenjamin Kietzman 
81a1051121SBenjamin Kietzman     boost::asio::detail::async_result_init<
82*da3eeb6aSEd Tanous         MessageHandler, void(boost::system::error_code, message)>
83*da3eeb6aSEd Tanous         init(BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
84*da3eeb6aSEd Tanous     detail::async_send_op<typename boost::asio::handler_type<
85*da3eeb6aSEd Tanous         MessageHandler, void(boost::system::error_code, message)>::type>(
86a1051121SBenjamin Kietzman         this->get_io_service(),
87a1051121SBenjamin Kietzman         BOOST_ASIO_MOVE_CAST(MessageHandler)(init.handler))(impl, m);
88a1051121SBenjamin Kietzman 
89a1051121SBenjamin Kietzman     return init.result.get();
90d620effbSBenjamin Kietzman   }
91d620effbSBenjamin Kietzman 
92*da3eeb6aSEd Tanous  private:
93*da3eeb6aSEd Tanous   friend connection;
94*da3eeb6aSEd Tanous   inline void new_match(implementation_type& impl, match& m);
95cd8b76a3SBenjamin Kietzman 
96*da3eeb6aSEd Tanous   inline void delete_match(implementation_type& impl, match& m);
97cd8b76a3SBenjamin Kietzman 
98*da3eeb6aSEd Tanous   inline void new_filter(implementation_type& impl, filter& f);
99cd8b76a3SBenjamin Kietzman 
100*da3eeb6aSEd Tanous   inline void delete_filter(implementation_type& impl, filter& f);
101d620effbSBenjamin Kietzman };
102d620effbSBenjamin Kietzman 
103d620effbSBenjamin Kietzman }  // namespace dbus
104fc79e461SBenjamin Kietzman 
105fc79e461SBenjamin Kietzman #endif  // DBUS_CONNECTION_SERVICE_HPP
106