2015-09-20 13:06:00 +00:00
|
|
|
#pragma once
|
|
|
|
#include <boost/asio.hpp>
|
2016-08-28 05:46:31 +00:00
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
#endif
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/settings.h"
|
2019-07-01 06:49:10 +00:00
|
|
|
#if BOOST_VERSION >= 107000
|
|
|
|
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
|
|
|
|
#else
|
|
|
|
#define GET_IO_SERVICE(s) ((s).get_io_service())
|
|
|
|
#endif
|
2015-09-20 13:06:00 +00:00
|
|
|
namespace crow
|
|
|
|
{
|
|
|
|
using namespace boost;
|
|
|
|
using tcp = asio::ip::tcp;
|
|
|
|
|
|
|
|
struct SocketAdaptor
|
|
|
|
{
|
|
|
|
using context = void;
|
|
|
|
SocketAdaptor(boost::asio::io_service& io_service, context*)
|
|
|
|
: socket_(io_service)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-28 05:46:31 +00:00
|
|
|
boost::asio::io_service& get_io_service()
|
|
|
|
{
|
2019-07-01 06:49:10 +00:00
|
|
|
return GET_IO_SERVICE(socket_);
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:06:00 +00:00
|
|
|
tcp::socket& raw_socket()
|
|
|
|
{
|
|
|
|
return socket_;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::socket& socket()
|
|
|
|
{
|
|
|
|
return socket_;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::endpoint remote_endpoint()
|
|
|
|
{
|
|
|
|
return socket_.remote_endpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_open()
|
|
|
|
{
|
|
|
|
return socket_.is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
2017-12-25 13:36:30 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
socket_.close(ec);
|
2015-09-20 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 08:48:35 +00:00
|
|
|
void shutdown_readwrite()
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
socket_.shutdown(boost::asio::socket_base::shutdown_type::shutdown_both, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_write()
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
socket_.shutdown(boost::asio::socket_base::shutdown_type::shutdown_send, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_read()
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
socket_.shutdown(boost::asio::socket_base::shutdown_type::shutdown_receive, ec);
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:06:00 +00:00
|
|
|
template <typename F>
|
|
|
|
void start(F f)
|
|
|
|
{
|
|
|
|
f(boost::system::error_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::socket socket_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CROW_ENABLE_SSL
|
|
|
|
struct SSLAdaptor
|
|
|
|
{
|
|
|
|
using context = boost::asio::ssl::context;
|
2016-08-28 05:46:31 +00:00
|
|
|
using ssl_socket_t = boost::asio::ssl::stream<tcp::socket>;
|
2015-09-20 13:06:00 +00:00
|
|
|
SSLAdaptor(boost::asio::io_service& io_service, context* ctx)
|
2016-08-28 05:46:31 +00:00
|
|
|
: ssl_socket_(new ssl_socket_t(io_service, *ctx))
|
2015-09-20 13:06:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::asio::ssl::stream<tcp::socket>& socket()
|
|
|
|
{
|
2016-08-28 05:46:31 +00:00
|
|
|
return *ssl_socket_;
|
2015-09-20 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tcp::socket::lowest_layer_type&
|
|
|
|
raw_socket()
|
|
|
|
{
|
2016-08-28 05:46:31 +00:00
|
|
|
return ssl_socket_->lowest_layer();
|
2015-09-20 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tcp::endpoint remote_endpoint()
|
|
|
|
{
|
|
|
|
return raw_socket().remote_endpoint();
|
|
|
|
}
|
|
|
|
|
2020-11-09 02:00:49 +00:00
|
|
|
bool is_open()
|
|
|
|
{
|
|
|
|
return ssl_socket_ ? raw_socket().is_open() : false;
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:06:00 +00:00
|
|
|
void close()
|
|
|
|
{
|
2020-11-09 02:00:49 +00:00
|
|
|
if (is_open())
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
raw_socket().close(ec);
|
|
|
|
}
|
2015-09-20 13:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 08:48:35 +00:00
|
|
|
void shutdown_readwrite()
|
|
|
|
{
|
2020-11-09 02:00:49 +00:00
|
|
|
if (is_open())
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
raw_socket().shutdown(boost::asio::socket_base::shutdown_type::shutdown_both, ec);
|
|
|
|
}
|
2020-10-20 08:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_write()
|
|
|
|
{
|
2020-11-09 02:00:49 +00:00
|
|
|
if (is_open())
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
raw_socket().shutdown(boost::asio::socket_base::shutdown_type::shutdown_send, ec);
|
|
|
|
}
|
2020-10-20 08:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_read()
|
|
|
|
{
|
2020-11-09 02:00:49 +00:00
|
|
|
if (is_open())
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
raw_socket().shutdown(boost::asio::socket_base::shutdown_type::shutdown_receive, ec);
|
|
|
|
}
|
2020-10-20 08:48:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 05:46:31 +00:00
|
|
|
boost::asio::io_service& get_io_service()
|
|
|
|
{
|
2019-07-01 06:49:10 +00:00
|
|
|
return GET_IO_SERVICE(raw_socket());
|
2016-08-28 05:46:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:06:00 +00:00
|
|
|
template <typename F>
|
|
|
|
void start(F f)
|
|
|
|
{
|
2016-08-28 05:46:31 +00:00
|
|
|
ssl_socket_->async_handshake(boost::asio::ssl::stream_base::server,
|
2015-09-20 13:06:00 +00:00
|
|
|
[f](const boost::system::error_code& ec) {
|
|
|
|
f(ec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-28 05:46:31 +00:00
|
|
|
std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> ssl_socket_;
|
2015-09-20 13:06:00 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
}
|