4#include <boost/asio.hpp>
5#include <boost/asio/version.hpp>
7#include <boost/asio/ssl.hpp>
10#ifndef ASIO_STANDALONE
11#define ASIO_STANDALONE
14#include <asio/version.hpp>
16#include <asio/ssl.hpp>
19#include "crow/settings.h"
21#if (defined(CROW_USE_BOOST) && BOOST_VERSION >= 107000) || (ASIO_VERSION >= 101008)
22#define GET_IO_CONTEXT(s) ((asio::io_context&)(s).get_executor().context())
24#define GET_IO_CONTEXT(s) ((s).get_io_service())
30 namespace asio = boost::asio;
31 using error_code = boost::system::error_code;
33 using error_code = asio::error_code;
35 using tcp = asio::ip::tcp;
36 using stream_protocol = asio::local::stream_protocol;
46 asio::io_context& get_io_context()
48 return GET_IO_CONTEXT(socket_);
63 tcp::endpoint remote_endpoint()
const
65 return socket_.remote_endpoint();
68 std::string address()
const
70 return socket_.remote_endpoint().address().to_string();
77 const auto endpoint = socket_.remote_endpoint(ec);
78 return ec ? 0 : endpoint.port();
83 return socket_.is_open();
92 void shutdown_readwrite()
95 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
101 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
107 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
121 using context = void;
127 asio::io_context& get_io_context()
129 return GET_IO_CONTEXT(socket_);
132 stream_protocol::socket& raw_socket()
137 stream_protocol::socket& socket()
142 stream_protocol::endpoint remote_endpoint()
144 return socket_.local_endpoint();
147 std::string address()
const
160 return socket_.is_open();
169 void shutdown_readwrite()
172 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
175 void shutdown_write()
178 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
184 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
193 stream_protocol::socket socket_;
196#ifdef CROW_ENABLE_SSL
199 using context = asio::ssl::context;
200 using ssl_socket_t = asio::ssl::stream<tcp::socket>;
201 SSLAdaptor(asio::io_context& io_context, context* ctx):
202 ssl_socket_(
new ssl_socket_t(io_context, *ctx))
205 asio::ssl::stream<tcp::socket>& socket()
210 tcp::socket::lowest_layer_type&
213 return ssl_socket_->lowest_layer();
216 tcp::endpoint remote_endpoint()
218 return raw_socket().remote_endpoint();
221 std::string address()
const
223 return ssl_socket_->lowest_layer().remote_endpoint().address().to_string();
230 const auto endpoint = ssl_socket_->lowest_layer().remote_endpoint(ec);
231 return ec ? 0 : endpoint.port();
236 return ssl_socket_ ? raw_socket().is_open() :
false;
244 raw_socket().close(ec);
248 void shutdown_readwrite()
253 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
257 void shutdown_write()
262 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
271 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
275 asio::io_context& get_io_context()
277 return GET_IO_CONTEXT(raw_socket());
283 ssl_socket_->async_handshake(asio::ssl::stream_base::server,
284 [f](
const error_code& ec) {
289 std::unique_ptr<asio::ssl::stream<tcp::socket>> ssl_socket_;
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition socket_adaptors.h:198
uint16_t remote_port() const
Get the TCP port of the remote endpoint, 0 when unavailable (socket already closed).
Definition socket_adaptors.h:227
A wrapper for the asio::ip::tcp::socket and asio::ssl::stream.
Definition socket_adaptors.h:40
tcp::socket & socket()
Get the object handling data transfers, this can be either a TCP socket or an SSL stream (if SSL is e...
Definition socket_adaptors.h:58
uint16_t remote_port() const
Get the TCP port of the remote endpoint, 0 when unavailable (socket already closed).
Definition socket_adaptors.h:74
tcp::socket & raw_socket()
Get the TCP socket handling data transfers, regardless of what layer is handling transfers on top of ...
Definition socket_adaptors.h:52
Definition socket_adaptors.h:120
uint16_t remote_port() const
Unix domain sockets have no port: always 0.
Definition socket_adaptors.h:153