wamp_websocket_transport.hpp
1 //
3 // Copyright (c) Crossbar.io Technologies GmbH and contributors 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_WEBSOCKET_TRANSPORT_HPP
32 #define AUTOBAHN_WEBSOCKET_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  public wamp_transport,
55  public std::enable_shared_from_this<wamp_websocket_transport>
56  {
57  public:
64  const std::string& uri,
65  bool debug_enabled = false);
66 
67  virtual ~wamp_websocket_transport() override = default;
68 
69 
70  /*
71  * CONNECTION INTERFACE
72  */
76  virtual boost::future<void> connect() override;
77 
81  virtual boost::future<void> disconnect() override;
82 
86  virtual bool is_connected() const override;
87 
88  /*
89  * SENDER INTERFACE
90  */
94  virtual void send_message(wamp_message&& message) override;
95 
99  virtual void set_pause_handler(pause_handler&& handler) override;
100 
104  virtual void set_resume_handler(resume_handler&& handler) override;
105 
106  /*
107  * RECEIVER INTERFACE
108  */
114  virtual void pause() override;
115 
120  virtual void resume() override;
121 
125  virtual void attach(
126  const std::shared_ptr<wamp_transport_handler>& handler) override;
127 
131  virtual void detach() override;
132 
136  virtual bool has_handler() const override;
137 
138 
139  protected:
140  virtual bool is_open() const = 0;
141 
142 
143  virtual void async_connect(const std::string& m_uri, boost::promise<void>& connect_promise) = 0;
144  virtual void close() = 0;
145 
146  virtual void write(void const * payload, size_t len) = 0;
147 
148  void receive_message(const std::string& msg);
149 
153  boost::promise<void> m_connect;
154 
158  boost::promise<void> m_disconnect;
159 
160  private:
161 
162 
163  private:
164 
165 
169  pause_handler m_pause_handler;
170 
174  resume_handler m_resume_handler;
175 
179  std::shared_ptr<wamp_transport_handler> m_handler;
180 
184  msgpack::unpacker m_message_unpacker;
185 
189  bool m_debug_enabled;
190 
194  std::string m_uri;
195  };
196 } // namespace autobahn
197 
198 #include "wamp_websocket_transport.ipp"
199 #endif // AUTOBAHN_WEBSOCKET_TRANSPORT_HPP
A class that represents a wamp message in its simplest form.
virtual boost::future< void > connect() override
Attempts to connect the transport.
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 is_connected() const override
Determines if the transport is connected.
virtual void send_message(wamp_message &&message) override
Send the message synchronously over the transport.
wamp_websocket_transport(const std::string &uri, bool debug_enabled=false)
Constructs a websocket transport.
Provides an abstraction for a transport to be used by the session.
virtual bool has_handler() const override
Determines if the transport has a handler attached.
virtual void detach() override
Detaches the handler currently attached to the transport.
virtual void pause() override
Pause receiving of messages.
boost::promise< void > m_disconnect
The promise that is fulfilled when the disconnect attempt is complete.
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...
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 boost::future< void > disconnect() override
Attempts to disconnect the transport.
virtual void attach(const std::shared_ptr< wamp_transport_handler > &handler) override
Attaches a handler to the transport.
boost::promise< void > m_connect
The promise that is fulfilled when the connect attempt is complete.
A class that represents a base websocket transport.
virtual void resume() override
Resume receiving of messages.