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();
72 const auto endpoint = socket_.remote_endpoint(ec);
73 return ec ? std::string() : endpoint.address().to_string();
80 const auto endpoint = socket_.remote_endpoint(ec);
81 return ec ? 0 : endpoint.port();
86 return socket_.is_open();
95 void shutdown_readwrite()
98 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
101 void shutdown_write()
104 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
110 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
124 using context = void;
130 asio::io_context& get_io_context()
132 return GET_IO_CONTEXT(socket_);
135 stream_protocol::socket& raw_socket()
140 stream_protocol::socket& socket()
145 stream_protocol::endpoint remote_endpoint()
147 return socket_.local_endpoint();
150 std::string address()
const
163 return socket_.is_open();
172 void shutdown_readwrite()
175 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
178 void shutdown_write()
181 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
187 socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
196 stream_protocol::socket socket_;
199#ifdef CROW_ENABLE_SSL
202 using context = asio::ssl::context;
203 using ssl_socket_t = asio::ssl::stream<tcp::socket>;
204 SSLAdaptor(asio::io_context& io_context, context* ctx):
205 ssl_socket_(
new ssl_socket_t(io_context, *ctx))
208 asio::ssl::stream<tcp::socket>& socket()
213 tcp::socket::lowest_layer_type&
216 return ssl_socket_->lowest_layer();
219 tcp::endpoint remote_endpoint()
221 return raw_socket().remote_endpoint();
228 const auto endpoint = ssl_socket_->lowest_layer().remote_endpoint(ec);
229 return ec ? std::string() : endpoint.address().to_string();
236 const auto endpoint = ssl_socket_->lowest_layer().remote_endpoint(ec);
237 return ec ? 0 : endpoint.port();
242 return ssl_socket_ ? raw_socket().is_open() :
false;
250 raw_socket().close(ec);
254 void shutdown_readwrite()
259 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
263 void shutdown_write()
268 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
277 raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
281 asio::io_context& get_io_context()
283 return GET_IO_CONTEXT(raw_socket());
289 ssl_socket_->async_handshake(asio::ssl::stream_base::server,
290 [f](
const error_code& ec) {
295 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:201
std::string address() const
Get the IP address of the remote endpoint, empty when unavailable (socket already closed).
Definition socket_adaptors.h:225
uint16_t remote_port() const
Get the TCP port of the remote endpoint, 0 when unavailable (socket already closed).
Definition socket_adaptors.h:233
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
std::string address() const
Get the IP address of the remote endpoint, empty when unavailable (socket already closed).
Definition socket_adaptors.h:69
uint16_t remote_port() const
Get the TCP port of the remote endpoint, 0 when unavailable (socket already closed).
Definition socket_adaptors.h:77
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:123
uint16_t remote_port() const
Unix domain sockets have no port: always 0.
Definition socket_adaptors.h:156