FETPAPI 0.6.0.0
This project provides C++ classes which facilitate the developement of ETP1.2 clients and servers.
Loading...
Searching...
No Matches
PlainClientSession.h
1/*-----------------------------------------------------------------------
2Licensed to the Apache Software Foundation (ASF) under one
3or more contributor license agreements. See the NOTICE file
4distributed with this work for additional information
5regarding copyright ownership. The ASF licenses this file
6to you under the Apache License, Version 2.0 (the
7"License"; you may not use this file except in compliance
8with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing,
13software distributed under the License is distributed on an
14"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15KIND, either express or implied. See the License for the
16specific language governing permissions and limitations
17under the License.
18-----------------------------------------------------------------------*/
19#pragma once
20
21#include "AbstractClientSessionCRTP.h"
22
23namespace ETP_NS
24{
25 class PlainClientSession : public AbstractClientSessionCRTP<PlainClientSession>
26 {
27 public:
28 /*
29 * @param frameSize Sets the size of the write buffer used by the implementation to send frames : https://www.boost.org/doc/libs/1_75_0/libs/beast/doc/html/beast/ref/boost__beast__websocket__stream/write_buffer_bytes/overload1.html.
30 */
31 FETPAPI_DLL_IMPORT_OR_EXPORT PlainClientSession(
32 InitializationParameters const* initializationParams, const std::string & target, const std::string & authorization, const std::string& proxyAuthorization = "",
33 const std::map<std::string, std::string>& additionalHandshakeHeaderFields = {}, std::size_t frameSize = 4096);
34
35 virtual ~PlainClientSession() = default;
36
37 // Called by the base class
38#if BOOST_VERSION < 107000
39 FETPAPI_DLL_IMPORT_OR_EXPORT std::unique_ptr<websocket::stream<tcp::socket>>& ws() { return ws_; }
40#else
41 FETPAPI_DLL_IMPORT_OR_EXPORT std::unique_ptr<websocket::stream<boost::beast::tcp_stream>>& ws() { return ws_; }
42#endif
43
44 bool isTls() const final{ return false; }
45
46 void asyncConnect(const tcp::resolver::results_type& results)
47 {
48 // Reality check: IPv6 is unlikely to be available yet
49 std::vector<tcp::endpoint> endpoints = std::vector<tcp::endpoint>(results.begin(), results.end());
50 std::stable_partition(endpoints.begin(), endpoints.end(), [](auto entry) {return entry.protocol() == tcp::v4(); });
51
52#if BOOST_VERSION < 107000
53 ws_.reset(new websocket::stream<tcp::socket>(ioc));
54 ws_->write_buffer_size(frameSize_);
55 ws_->binary(true);
56
57 // Make the connection on the IP address we get from a lookup
58 boost::asio::async_connect(
59 ws_->next_layer(),
60 endpoints.begin(),
61 endpoints.end(),
62 std::bind(
63 &AbstractClientSessionCRTP::on_ssl_handshake,
64 std::static_pointer_cast<AbstractClientSessionCRTP>(shared_from_this()),
65 std::placeholders::_1));
66#else
67 ws_.reset(new websocket::stream<boost::beast::tcp_stream>(ioc));
68 ws_->write_buffer_bytes(frameSize_);
69 ws_->binary(true);
70
71 // Make the connection on the IP address we get from a lookup
72 boost::beast::get_lowest_layer(*ws_).async_connect(
73 endpoints,
74 boost::beast::bind_front_handler(
75 &PlainClientSession::on_connect,
76 std::static_pointer_cast<PlainClientSession>(shared_from_this())));
77#endif
78 }
79
80#if BOOST_VERSION > 106900
81 void on_connect(boost::beast::error_code ec, tcp::resolver::results_type::endpoint_type)
82 {
83 on_ssl_handshake(ec);
84 }
85#endif
86
87 private:
88#if BOOST_VERSION < 107000
89 std::unique_ptr<websocket::stream<tcp::socket>> ws_;
90#else
91 std::unique_ptr<websocket::stream<boost::beast::tcp_stream>> ws_;
92#endif
93 std::size_t frameSize_;
94 };
95}
Definition AbstractClientSessionCRTP.h:28
Definition InitializationParameters.h:41