forked from glynos/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathstream_handler.hpp
More file actions
174 lines (149 loc) · 4.75 KB
/
Copy pathstream_handler.hpp
File metadata and controls
174 lines (149 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef NETLIB_IO_STREAM_HANDLER_HPP
#define NETLIB_IO_STREAM_HANDLER_HPP
// Copyright 2014 Jelle Van den Driessche.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <memory>
#include <boost/asio/async_result.hpp>
#include <boost/asio/basic_socket.hpp>
#include <boost/asio/basic_stream_socket.hpp>
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/handler_type_requirements.hpp>
#include <boost/asio/detail/push_options.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/error.hpp>
#include <cstddef>
#ifdef BOOST_NETWORK_ENABLE_HTTPS
#include <boost/asio/ssl.hpp>
#endif
namespace boost {
namespace network {
typedef boost::asio::ip::tcp::socket tcp_socket;
#ifndef BOOST_NETWORK_ENABLE_HTTPS
typedef tcp_socket stream_handler;
typedef void ssl_context;
#else
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
typedef boost::asio::ssl::context ssl_context;
struct stream_handler {
public:
stream_handler(std::shared_ptr<tcp_socket> socket)
: tcp_sock_(std::move(socket)), ssl_enabled(false) {}
~stream_handler() = default;
stream_handler(std::shared_ptr<ssl_socket> socket)
: ssl_sock_(std::move(socket)), ssl_enabled(true) {}
stream_handler(boost::asio::io_service& io,
std::shared_ptr<ssl_context> ctx =
std::shared_ptr<ssl_context>()) {
tcp_sock_ = std::make_shared<tcp_socket>(boost::ref(io));
ssl_enabled = false;
if (ctx) {
/// SSL is enabled
ssl_sock_ =
std::make_shared<ssl_socket>(boost::ref(io), boost::ref(*ctx));
ssl_enabled = true;
}
}
template <typename ConstBufferSequence, typename WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
void(boost::system::error_code, std::size_t))
async_write_some(const ConstBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(WriteHandler) handler) {
try {
if (ssl_enabled) {
ssl_sock_->async_write_some(buffers, handler);
} else {
tcp_sock_->async_write_some(buffers, handler);
}
}
catch (const boost::system::system_error&) {
// print system error
}
}
template <typename MutableBufferSequence, typename ReadHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
void(boost::system::error_code, std::size_t))
async_read_some(const MutableBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(ReadHandler) handler) {
try {
if (ssl_enabled) {
ssl_sock_->async_read_some(buffers, handler);
} else {
tcp_sock_->async_read_some(buffers, handler);
}
}
catch (const boost::system::system_error& e) {
// print system error
}
}
void close(boost::system::error_code& e) {
if (ssl_enabled) {
ssl_sock_->next_layer().close();
} else {
tcp_sock_->close();
}
}
tcp_socket::endpoint_type remote_endpoint() const {
if (ssl_enabled) {
return ssl_sock_->next_layer().remote_endpoint();
} else {
return tcp_sock_->remote_endpoint();
}
}
void shutdown(boost::asio::socket_base::shutdown_type st,
boost::system::error_code& e) {
try {
if (ssl_enabled) {
ssl_sock_->shutdown(e);
} else {
tcp_sock_->shutdown(boost::asio::ip::tcp::socket::shutdown_send, e);
}
}
catch (const boost::system::system_error& e) {
}
}
ssl_socket::next_layer_type& next_layer() const {
if (ssl_enabled) {
return ssl_sock_->next_layer();
} else {
return *tcp_sock_;
}
}
ssl_socket::lowest_layer_type& lowest_layer() const {
if (ssl_enabled) {
return ssl_sock_->lowest_layer();
} else {
return tcp_sock_->lowest_layer();
}
}
template <typename HandshakeHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler,
void(boost::system::error_code))
async_handshake(ssl_socket::handshake_type type,
BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler) {
try {
if (ssl_enabled) {
return ssl_sock_->async_handshake(type, handler);
} else {
// NOOP
}
}
catch (const boost::system::system_error& e) {
}
}
std::shared_ptr<tcp_socket> get_tcp_socket() { return tcp_sock_; }
std::shared_ptr<ssl_socket> get_ssl_socket() { return ssl_sock_; }
bool is_ssl_enabled() { return ssl_enabled; }
private:
std::shared_ptr<tcp_socket> tcp_sock_;
std::shared_ptr<ssl_socket> ssl_sock_;
bool ssl_enabled;
};
#endif
}
}
#endif