wamp_rawsocket_transport.hpp
1 //
3 // Copyright (c) Crossbar.io Technologies GmbH and contributors
4 //
5 // Boost Software License - Version 1.0 - August 17th, 2003
6 //
7 // Permission is hereby granted, free of charge, to any person or organization
8 // obtaining a copy of the software and accompanying documentation covered by
9 // this license (the "Software") to use, reproduce, display, distribute,
10 // execute, and transmit the Software, and to prepare derivative works of the
11 // Software, and to permit third-parties to whom the Software is furnished to
12 // do so, all subject to the following:
13 //
14 // The copyright notices in the Software and this entire statement, including
15 // the above license grant, this restriction and the following disclaimer,
16 // must be included in all copies of the Software, in whole or in part, and
17 // all derivative works of the Software, unless such copies or derivative
18 // works are solely in the form of machine-executable object code generated by
19 // a source language processor.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 //
30 
31 #ifndef AUTOBAHN_WAMP_NETWORK_TRANSPORT_HPP
32 #define AUTOBAHN_WAMP_NETWORK_TRANSPORT_HPP
33 
34 #include "boost_config.hpp"
35 #include "wamp_transport.hpp"
36 
37 #include <boost/asio/io_service.hpp>
38 #include <cstddef>
39 #include <memory>
40 #include <msgpack.hpp>
41 
42 namespace autobahn {
43 
44 class wamp_message;
45 class wamp_transport_handler;
46 
54 template <typename Socket>
56  public wamp_transport,
57  public std::enable_shared_from_this<wamp_rawsocket_transport<Socket>>
58 {
59 public:
63  typedef Socket socket_type;
64 
68  typedef typename Socket::endpoint_type endpoint_type;
69 
70 public:
78  boost::asio::io_service& io_service,
79  const endpoint_type& remote_endpoint,
80  bool debug_enabled=false);
81 
82  virtual ~wamp_rawsocket_transport() override = default;
83 
84  /*
85  * CONNECTION INTERFACE
86  */
90  virtual boost::future<void> connect() override;
91 
95  virtual boost::future<void> disconnect() override;
96 
100  virtual bool is_connected() const override;
101 
102  /*
103  * SENDER INTERFACE
104  */
108  virtual void send_message(wamp_message&& message) override;
109 
113  virtual void set_pause_handler(pause_handler&& handler) override;
114 
118  virtual void set_resume_handler(resume_handler&& handler) override;
119 
120  /*
121  * RECEIVER INTERFACE
122  */
128  virtual void pause() override;
129 
134  virtual void resume() override;
135 
139  virtual void attach(
140  const std::shared_ptr<wamp_transport_handler>& handler) override;
141 
145  virtual void detach() override;
146 
150  virtual bool has_handler() const override;
151 
152 protected:
153  socket_type& socket();
154 
155 private:
156 
157  void handshake_reply_handler(
158  const boost::system::error_code& error_code,
159  std::size_t /* bytes_transferred */);
160 
161  void receive_message();
162 
163  void receive_message_header(
164  const boost::system::error_code& error,
165  std::size_t /* bytes transferred */);
166 
167  void receive_message_body(
168  const boost::system::error_code& error,
169  std::size_t /* bytes transferred */);
170 
171 private:
175  socket_type m_socket;
176 
180  endpoint_type m_remote_endpoint;
181 
185  boost::promise<void> m_connect;
186 
190  boost::promise<void> m_disconnect;
191 
195  pause_handler m_pause_handler;
196 
200  resume_handler m_resume_handler;
201 
205  std::shared_ptr<wamp_transport_handler> m_handler;
206 
210  uint8_t m_handshake_buffer[4];
211 
215  uint32_t m_message_length;
216 
220  msgpack::unpacker m_message_unpacker;
221 
225  bool m_debug_enabled;
226 };
227 
228 } // namespace autobahn
229 
230 #include "wamp_rawsocket_transport.ipp"
231 
232 #endif // AUTOBAHN_WAMP_NETWORK_TRANSPORT_HPP
A class that represents a wamp message in its simplest form.
virtual void attach(const std::shared_ptr< wamp_transport_handler > &handler) override
Attaches a handler to the transport.
virtual void send_message(wamp_message &&message) override
Send the message synchronously over the transport.
virtual void pause() override
Pause receiving of messages.
virtual bool is_connected() const override
Determines if the transport is connected.
Provides an abstraction for a transport to be used by the session.
virtual void set_pause_handler(pause_handler &&handler) override
Set the handler to be invoked when the transport detects congestion sending to the remote peer and ne...
virtual bool has_handler() const override
Determines if the transport has a handler attached.
wamp_rawsocket_transport(boost::asio::io_service &io_service, const endpoint_type &remote_endpoint, bool debug_enabled=false)
Constructs a rawsocket transport.
A class that represents a rawsocket transport.
virtual void resume() override
Resume receiving of messages.
virtual boost::future< void > connect() override
Attempts to connect the transport.
std::function< void()> resume_handler
Handler to invoke when resuming transport transmission.
std::function< void()> pause_handler
Handler to invoke when pausing transport transmission.
virtual void detach() override
Detaches the handler currently attached to the transport.
virtual boost::future< void > disconnect() override
Attempts to disconnect the transport.
Socket socket_type
Convenience type for the socket being used.
Socket::endpoint_type endpoint_type
Convenience type for the endpoint being used.
virtual void set_resume_handler(resume_handler &&handler) override
Set the handler to be invoked when the transport detects congestion has subsided on the remote peer a...