CppWAMP
C++11 client library for the WAMP protocol
rawsockconnector.hpp
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_RAWSOCKCONNECTOR_HPP
8 #define CPPWAMP_RAWSOCKCONNECTOR_HPP
9 
10 #include <cassert>
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <boost/asio/post.hpp>
15 #include <boost/asio/strand.hpp>
16 #include "../asiodefs.hpp"
17 #include "../connector.hpp"
18 #include "../error.hpp"
19 #include "client.hpp"
20 
21 namespace wamp
22 {
23 
24 namespace internal
25 {
26 
27 //------------------------------------------------------------------------------
28 template <typename TCodec, typename TEndpoint>
29 class RawsockConnector : public Connector
30 {
31 public:
32  using Codec = TCodec;
33  using Endpoint = TEndpoint;
34  using Info = typename Endpoint::Establisher::Info;
35  using Transport = typename Endpoint::Transport;
36  using Ptr = std::shared_ptr<RawsockConnector>;
37 
38  static Ptr create(const AnyIoExecutor& exec, Info info)
39  {
40  return Ptr(new RawsockConnector(IoStrand(exec), std::move(info)));
41  }
42 
43  IoStrand strand() const override {return strand_;}
44 
45 protected:
46  using Establisher = typename Endpoint::Establisher;
47 
48  virtual Connector::Ptr clone() const override
49  {
50  return Connector::Ptr(new RawsockConnector(strand_, info_));
51  }
52 
53  virtual void establish(Handler&& handler) override
54  {
55  struct Established
56  {
57  Ptr self;
58  Handler handler;
59 
60  void operator()(std::error_code ec, int codecId,
61  typename Transport::Ptr trnsp)
62  {
63  auto& me = *self;
64  internal::ClientInterface::Ptr client;
65  using ClientType = internal::Client<Codec, Transport>;
66  if (!ec)
67  {
68  assert(codecId == Codec::id());
69  client = ClientType::create(std::move(trnsp));
70  }
71  boost::asio::post(me.strand_,
72  std::bind(std::move(handler), ec, client));
73  me.endpoint_.reset();
74  }
75  };
76 
77  CPPWAMP_LOGIC_CHECK(!endpoint_, "Connection already in progress");
78  endpoint_.reset(new Endpoint( Establisher(strand_, info_),
79  Codec::id(),
80  info_.maxRxLength() ));
81  auto self =
82  std::static_pointer_cast<RawsockConnector>(shared_from_this());
83  endpoint_->establish(Established{std::move(self), std::move(handler)});
84  }
85 
86  virtual void cancel() override
87  {
88  if (endpoint_)
89  endpoint_->cancel();
90  }
91 
92 private:
93  RawsockConnector(IoStrand strand, Info info)
94  : strand_(std::move(strand)),
95  info_(std::move(info))
96  {}
97 
98  boost::asio::strand<AnyIoExecutor> strand_;
99  Info info_;
100  std::unique_ptr<Endpoint> endpoint_;
101 };
102 
103 } // namespace internal
104 
105 } // namespace wamp
106 
107 #endif // CPPWAMP_RAWSOCKCONNECTOR_HPP
wamp::AnyIoExecutor
boost::asio::any_io_executor AnyIoExecutor
Polymorphic executor for all I/O objects.
Definition: asiodefs.hpp:27
CPPWAMP_LOGIC_CHECK
#define CPPWAMP_LOGIC_CHECK(cond, msg)
Conditionally throws an error::Logic exception having the given message string.
Definition: error.hpp:36
wamp::IoStrand
boost::asio::strand< AnyIoExecutor > IoStrand
Serializes I/O operations.
Definition: asiodefs.hpp:41
wamp::Connector::Ptr
std::shared_ptr< Connector > Ptr
Shared pointer to a Connector.
Definition: connector.hpp:43
wamp
Definition: anyhandler.hpp:36
wamp::Connector::establish
virtual void establish(Handler &&handler)=0
Starts establishing a transport connection.
wamp::Connector::clone
virtual Connector::Ptr clone() const =0
Creates a deep copy of this Connector object.
wamp::Connector::cancel
virtual void cancel()=0
Cancels a transport connection in progress.