CppWAMP
C++11 client library for the WAMP protocol
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
asioconnector.hpp
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_INTERNAL_ASIOCONNECTOR_HPP
8 #define CPPWAMP_INTERNAL_ASIOCONNECTOR_HPP
9 
10 #include <cstdint>
11 #include "../codec.hpp"
12 #include "../error.hpp"
13 #include "../rawsockoptions.hpp"
14 #include "asioendpoint.hpp"
15 
16 namespace wamp
17 {
18 
19 namespace internal
20 {
21 
22 //------------------------------------------------------------------------------
23 template <typename TEstablisher>
24 class AsioConnector : public AsioEndpoint<TEstablisher>
25 {
26 public:
27  using Establisher = TEstablisher;
28 
29  AsioConnector(Establisher&& est, int codecId, RawsockMaxLength maxRxLength)
30  : Base(std::move(est)),
31  codecId_(codecId),
32  maxRxLength_(maxRxLength)
33  {}
34 
35 private:
36  using Base = AsioEndpoint<TEstablisher>;
37 
38 protected:
39  using Handshake = typename Base::Handshake;
40 
41  virtual void onEstablished() override
42  {
43  Base::sendHandshake( Handshake().setCodecId(codecId_)
44  .setMaxLength(maxRxLength_) );
45  }
46 
47  virtual void onHandshakeSent(Handshake) override {Base::receiveHandshake();}
48 
49  virtual void onHandshakeReceived(Handshake hs) override
50  {
51  if (!hs.hasMagicOctet())
52  Base::fail(RawsockErrc::badHandshake);
53  else if (hs.reserved() != 0)
55  else if (hs.codecId() == codecId_)
56  Base::complete(codecId_, hs.maxLengthInBytes(),
57  Handshake::byteLengthOf(maxRxLength_));
58  else if (hs.hasError())
59  Base::fail(hs.errorCode());
60  else
61  Base::fail(RawsockErrc::badHandshake);
62  }
63 
64 private:
65  int codecId_;
66  RawsockMaxLength maxRxLength_;
67 };
68 
69 } // namespace internal
70 
71 } // namespace wamp
72 
73 #endif // CPPWAMP_INTERNAL_ASIOCONNECTOR_HPP
wamp::RawsockErrc::reservedBitsUsed
@ reservedBitsUsed
Use of reserved bits (unsupported feature)
wamp
Definition: anyhandler.hpp:36
wamp::RawsockErrc::badHandshake
@ badHandshake
Invalid handshake format from peer.
wamp::RawsockMaxLength
RawsockMaxLength
Enumerators used to specify the maximum length of messages that a raw socket transport can receive.
Definition: rawsockoptions.hpp:22