Fixes to support Asio changes (API Change):

This adjusts Beast's interfaces and implementation to match
the changes in Boost.Asio.
This commit is contained in:
Vinnie Falco committed 2019-02-19 16:23:30 -08:00
1 parent ac24e58fb3
commit 6baa607295
58 files changed
+762 -723

No files matched your search

@@ -331,27 +331,26 @@ public:
}
};
//------------------------------------------------------------------------------
// Handles a plain WebSocket connection
class plain_websocket_session
: public websocket_session<plain_websocket_session>
, public std::enable_shared_from_this<plain_websocket_session>
{
websocket::stream<
beast::tcp_stream<net::io_context::strand>> ws_;
bool close_ = false;
websocket::stream<beast::tcp_stream> ws_;
public:
// Create the session
explicit
plain_websocket_session(
beast::tcp_stream<net::io_context::strand>&& stream)
beast::tcp_stream&& stream)
: ws_(std::move(stream))
{
}
// Called by the base class
websocket::stream<
beast::tcp_stream<net::io_context::strand>>&
websocket::stream<beast::tcp_stream>&
ws()
{
return ws_;
@@ -365,48 +364,28 @@ public:
// Accept the WebSocket upgrade request
do_accept(std::move(req));
}
void
on_close(beast::error_code ec)
{
// Happens when close times out
if(ec == net::error::operation_aborted)
return;
if(ec)
return fail(ec, "close");
// At this point the connection is gracefully closed
}
};
//------------------------------------------------------------------------------
// Handles an SSL WebSocket connection
class ssl_websocket_session
: public websocket_session<ssl_websocket_session>
, public std::enable_shared_from_this<ssl_websocket_session>
{
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>> ws_;
bool eof_ = false;
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws_;
public:
// Create the http_session
// Create the ssl_websocket_session
explicit
ssl_websocket_session(beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>&& stream)
ssl_websocket_session(
beast::ssl_stream<beast::tcp_stream>&& stream)
: ws_(std::move(stream))
{
}
// Called by the base class
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>>&
ws()
{
return ws_;
}
// Start the asynchronous operation
// Start the session
template<class Body, class Allocator>
void
run(http::request<Body, http::basic_fields<Allocator>> req)
@@ -415,18 +394,15 @@ public:
do_accept(std::move(req));
}
void
do_eof()
// Called by the base class
websocket::stream<
beast::ssl_stream<beast::tcp_stream>>&
ws()
{
eof_ = true;
// Perform the SSL shutdown
ws_.next_layer().async_shutdown(
beast::bind_front_handler(
&ssl_websocket_session::on_shutdown,
shared_from_this()));
return ws_;
}
private:
void
on_shutdown(beast::error_code ec)
{
@@ -437,10 +413,12 @@ public:
}
};
//------------------------------------------------------------------------------
template<class Body, class Allocator>
void
make_websocket_session(
beast::tcp_stream<net::io_context::strand> stream,
beast::tcp_stream stream,
http::request<Body, http::basic_fields<Allocator>> req)
{
std::make_shared<plain_websocket_session>(
@@ -450,7 +428,7 @@ make_websocket_session(
template<class Body, class Allocator>
void
make_websocket_session(
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>> stream,
beast::ssl_stream<beast::tcp_stream> stream,
http::request<Body, http::basic_fields<Allocator>> req)
{
std::make_shared<ssl_websocket_session>(
@@ -568,19 +546,15 @@ class http_session
queue queue_;
protected:
net::steady_timer timer_;
beast::flat_buffer buffer_;
public:
// Construct the session
http_session(
net::io_context& ioc,
beast::flat_buffer buffer,
std::shared_ptr<std::string const> const& doc_root)
: doc_root_(doc_root)
, queue_(*this)
, timer_(ioc,
(std::chrono::steady_clock::time_point::max)())
, buffer_(std::move(buffer))
{
}
@@ -621,7 +595,11 @@ public:
// See if it is a WebSocket Upgrade
if(websocket::is_upgrade(req_))
{
// Transfer the stream to a new WebSocket session
// Disable the timeout.
// The websocket::stream uses its own timeout settings.
beast::get_lowest_layer(derived().stream()).expires_never();
// Transfer the stream to a new WebSocket session.
return make_websocket_session(
derived().release_stream(),
std::move(req_));
@@ -663,56 +641,50 @@ public:
}
};
//------------------------------------------------------------------------------
// Handles a plain HTTP connection
class plain_http_session
: public http_session<plain_http_session>
, public std::enable_shared_from_this<plain_http_session>
{
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
public:
// Create the http_session
// Create the session
plain_http_session(
beast::tcp_stream<net::io_context::strand>&& stream,
beast::tcp_stream&& stream,
beast::flat_buffer&& buffer,
std::shared_ptr<std::string const> const& doc_root)
: http_session<plain_http_session>(
stream.get_executor().context(),
std::move(buffer),
doc_root)
, stream_(std::move(stream))
{
}
// Start the session
void
run()
{
this->do_read();
}
// Called by the base class
beast::tcp_stream<net::io_context::strand>&
beast::tcp_stream&
stream()
{
return stream_;
}
// Called by the base class
beast::tcp_stream<net::io_context::strand>
beast::tcp_stream
release_stream()
{
return std::move(stream_);
}
// Start the asynchronous operation
void
run()
{
// Make sure we run on the strand
if(! stream_.get_executor().running_in_this_thread())
return net::post(
stream_.get_executor(),
beast::bind_front_handler(
&plain_http_session::run,
shared_from_this()));
do_read();
}
// Called by the base class
void
do_eof()
{
@@ -722,70 +694,35 @@ public:
// At this point the connection is closed gracefully
}
void
do_timeout()
{
// Closing the socket cancels all outstanding operations. They
// will complete with net::error::operation_aborted
beast::error_code ec;
stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
stream_.socket().close(ec);
}
};
//------------------------------------------------------------------------------
// Handles an SSL HTTP connection
class ssl_http_session
: public http_session<ssl_http_session>
, public std::enable_shared_from_this<ssl_http_session>
{
beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>> stream_;
bool eof_ = false;
beast::ssl_stream<beast::tcp_stream> stream_;
public:
// Create the http_session
ssl_http_session(
beast::tcp_stream<net::io_context::strand>&& stream,
beast::tcp_stream&& stream,
ssl::context& ctx,
beast::flat_buffer&& buffer,
std::shared_ptr<std::string const> const& doc_root)
: http_session<ssl_http_session>(
stream.get_executor().context(),
std::move(buffer),
doc_root)
, stream_(std::move(stream), ctx)
{
}
// Called by the base class
beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>&
stream()
{
return stream_;
}
// Called by the base class
beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>
release_stream()
{
return std::move(stream_);
}
// Start the asynchronous operation
// Start the session
void
run()
{
// Make sure we run on the strand
if(! stream_.get_executor().running_in_this_thread())
return net::post(
stream_.get_executor(),
beast::bind_front_handler(
&ssl_http_session::run,
shared_from_this()));
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
@@ -798,6 +735,36 @@ public:
&ssl_http_session::on_handshake,
shared_from_this()));
}
// Called by the base class
beast::ssl_stream<beast::tcp_stream>&
stream()
{
return stream_;
}
// Called by the base class
beast::ssl_stream<beast::tcp_stream>
release_stream()
{
return std::move(stream_);
}
// Called by the base class
void
do_eof()
{
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown
stream_.async_shutdown(
beast::bind_front_handler(
&ssl_http_session::on_shutdown,
shared_from_this()));
}
private:
void
on_handshake(
beast::error_code ec,
@@ -812,21 +779,6 @@ public:
do_read();
}
void
do_eof()
{
eof_ = true;
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown
stream_.async_shutdown(
beast::bind_front_handler(
&ssl_http_session::on_shutdown,
shared_from_this()));
}
void
on_shutdown(beast::error_code ec)
{
@@ -846,7 +798,7 @@ public:
// Detects SSL handshakes
class detect_session : public std::enable_shared_from_this<detect_session>
{
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
ssl::context& ctx_;
std::shared_ptr<std::string const> doc_root_;
beast::flat_buffer buffer_;
@@ -854,7 +806,7 @@ class detect_session : public std::enable_shared_from_this<detect_session>
public:
explicit
detect_session(
tcp::socket socket,
tcp::socket&& socket,
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
@@ -906,8 +858,10 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -916,8 +870,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -969,13 +925,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -985,11 +942,14 @@ public:
{
// Create the detector http_session and run it
std::make_shared<detect_session>(
std::move(socket),
std::move(socket_),
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
+17 -19
View File
@@ -219,14 +219,13 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
class websocket_session : public std::enable_shared_from_this<websocket_session>
{
websocket::stream<beast::tcp_stream<net::io_context::strand>> ws_;
websocket::stream<beast::tcp_stream> ws_;
beast::flat_buffer buffer_;
char ping_state_ = 0;
public:
// Take ownership of the socket
explicit
websocket_session(tcp::socket socket)
websocket_session(tcp::socket&& socket)
: ws_(std::move(socket))
{
}
@@ -258,6 +257,7 @@ public:
shared_from_this()));
}
private:
void
on_accept(beast::error_code ec)
{
@@ -413,7 +413,7 @@ class http_session : public std::enable_shared_from_this<http_session>
}
};
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -421,9 +421,8 @@ class http_session : public std::enable_shared_from_this<http_session>
public:
// Take ownership of the socket
explicit
http_session(
tcp::socket socket,
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
, doc_root_(doc_root)
@@ -431,21 +430,14 @@ public:
{
}
// Start the asynchronous operation
// Start the session
void
run()
{
// Make sure we run on the strand
if(! stream_.get_executor().running_in_this_thread())
return net::post(
stream_.get_executor(),
std::bind(
&http_session::run,
shared_from_this()));
do_read();
}
private:
void
do_read()
{
@@ -532,6 +524,7 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
@@ -541,8 +534,9 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -594,13 +588,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -610,10 +605,13 @@ public:
{
// Create the http session and run it
std::make_shared<http_session>(
std::move(socket),
std::move(socket_),
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -33,10 +33,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of stream to use
using stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::executor_type>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -48,17 +44,18 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
stream_type stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
public:
// Resolver and stream require an io_context
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
explicit
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, stream_(ioc, ctx)
: resolver_(beast::make_strand(ioc))
, stream_(beast::make_strand(ioc), ctx)
{
}
@@ -29,9 +29,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of stream to use
using stream_type = beast::tcp_stream<net::io_context::executor_type>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -43,17 +40,18 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
stream_type stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
public:
// Resolver and socket require an io_context
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
explicit
session(net::io_context& ioc)
: resolver_(ioc)
, stream_(ioc)
: resolver_(beast::make_strand(ioc))
, stream_(beast::make_strand(ioc))
{
}
@@ -33,11 +33,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of stream to use
// Stackful coroutines are already stranded.
using stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::executor_type>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -60,7 +55,7 @@ do_session(
// These objects perform our I/O
tcp::resolver resolver(ioc);
stream_type stream(ioc, ctx);
beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
@@ -29,10 +29,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of stream to use.
// Stackful coroutines are already stranded.
using stream_type = beast::tcp_stream<net::io_context::executor_type>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -53,8 +49,8 @@ do_session(
beast::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ioc};
stream_type stream(ioc);
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
// Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]);
+26 -84
View File
@@ -151,10 +151,7 @@ class worker : public std::enable_shared_from_this<worker>
crawl_report& report_;
tcp::resolver resolver_;
tcp::socket socket_;
net::steady_timer timer_;
net::strand<
net::io_context::executor_type> strand_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
http::response<http::string_body> res_;
@@ -167,11 +164,8 @@ public:
crawl_report& report,
net::io_context& ioc)
: report_(report)
, resolver_(ioc)
, socket_(ioc)
, timer_(ioc,
(chrono::steady_clock::time_point::max)())
, strand_(ioc.get_executor())
, resolver_(beast::make_strand(ioc))
, stream_(beast::make_strand(ioc))
{
// Set up the common fields of the request
req_.version(11);
@@ -184,44 +178,9 @@ public:
void
run()
{
// Run the timer. The timer is operated
// continuously, this simplifies the code.
on_timer({});
do_get_host();
}
void
on_timer(beast::error_code ec)
{
if(ec && ec != net::error::operation_aborted)
{
// Should never happen
report_.aggregate(
[](crawl_report& rep)
{
++rep.timer_failures;
});
return;
}
// Verify that the timer really expired since the deadline may have moved.
if(timer_.expiry() <= chrono::steady_clock::now())
{
socket_.shutdown(tcp::socket::shutdown_both, ec);
socket_.close(ec);
return;
}
// Wait on the timer
timer_.async_wait(
net::bind_executor(
strand_,
beast::bind_front_handler(
&worker::on_timer,
shared_from_this())));
}
void
do_get_host()
{
@@ -230,27 +189,19 @@ public:
// nullptr means no more work
if(! host)
{
timer_.cancel_one();
return;
}
// The Host HTTP field is required
req_.set(http::field::host, host);
// Set the timer
timer_.expires_after(chrono::seconds(timeout));
// Set up an HTTP GET request message
// Look up the domain name
resolver_.async_resolve(
host,
"http",
net::bind_executor(
strand_,
beast::bind_front_handler(
&worker::on_resolve,
shared_from_this())));
beast::bind_front_handler(
&worker::on_resolve,
shared_from_this()));
}
void
@@ -268,18 +219,16 @@ public:
return do_get_host();
}
// Set the timer
timer_.expires_after(chrono::seconds(timeout));
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(10));
// Make the connection on the IP address we get from a lookup
net::async_connect(
socket_,
beast::async_connect(
stream_,
results,
net::bind_executor(
strand_,
beast::bind_front_handler(
&worker::on_connect,
shared_from_this())));
beast::bind_front_handler(
&worker::on_connect,
shared_from_this()));
}
void
@@ -295,18 +244,16 @@ public:
return do_get_host();
}
// Set the timer
timer_.expires_after(chrono::seconds(timeout));
// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(10));
// Send the HTTP request to the remote host
http::async_write(
socket_,
stream_,
req_,
net::bind_executor(
strand_,
beast::bind_front_handler(
&worker::on_write,
shared_from_this())));
beast::bind_front_handler(
&worker::on_write,
shared_from_this()));
}
void
@@ -325,21 +272,16 @@ public:
});
return do_get_host();
}
// Set the timer
timer_.expires_after(chrono::seconds(timeout));
// Receive the HTTP response
res_ = {};
http::async_read(
socket_,
stream_,
buffer_,
res_,
net::bind_executor(
strand_,
beast::bind_front_handler(
&worker::on_read,
shared_from_this())));
beast::bind_front_handler(
&worker::on_read,
shared_from_this()));
}
void
@@ -368,8 +310,8 @@ public:
});
// Gracefully close the socket
socket_.shutdown(tcp::socket::shutdown_both, ec);
socket_.close(ec);
stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
stream_.close();
// If we get here then the connection is closed gracefully
@@ -412,7 +354,7 @@ int main(int argc, char* argv[])
auto const threads = std::max<int>(1, std::atoi(argv[1]));
// The io_context is required for all I/O
net::io_context ioc{1};
net::io_context ioc;
// The work keeps io_context::run from returning
auto work = net::make_work_guard(ioc);
@@ -57,7 +57,7 @@ int main(int argc, char** argv)
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
ssl::context ctx(ssl::context::sslv23_client);
// This holds the root certificate used for verification
load_root_certificates(ctx);
@@ -66,8 +66,8 @@ int main(int argc, char** argv)
ctx.set_verify_mode(ssl::verify_peer);
// These objects perform our I/O
tcp::resolver resolver{ioc};
beast::ssl_stream<tcp::socket> stream{ioc, ctx};
tcp::resolver resolver(ioc);
beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(stream.native_handle(), host))
@@ -80,7 +80,7 @@ int main(int argc, char** argv)
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
net::connect(stream.next_layer(), results.begin(), results.end());
beast::connect(beast::get_lowest_layer(stream), results);
// Perform the SSL handshake
stream.handshake(ssl::stream_base::client);
@@ -53,14 +53,14 @@ int main(int argc, char** argv)
net::io_context ioc;
// These objects perform our I/O
tcp::resolver resolver{ioc};
tcp::socket socket{ioc};
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
net::connect(socket, results.begin(), results.end());
beast::connect(stream, results);
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, version};
@@ -68,7 +68,7 @@ int main(int argc, char** argv)
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(socket, req);
http::write(stream, req);
// This buffer is used for reading and must be persisted
beast::flat_buffer buffer;
@@ -77,14 +77,14 @@ int main(int argc, char** argv)
http::response<http::dynamic_body> res;
// Receive the HTTP response
http::read(socket, buffer, res);
http::read(stream, buffer, res);
// Write the message to standard out
std::cout << res << std::endl;
// Gracefully close the socket
beast::error_code ec;
socket.shutdown(tcp::socket::shutdown_both, ec);
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes
// so don't bother reporting it.
@@ -255,7 +255,7 @@ class session : public std::enable_shared_from_this<session>
}
};
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>> stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -387,8 +387,10 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -397,8 +399,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(ioc)
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -450,13 +454,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -466,11 +471,14 @@ public:
{
// Create the session and run it
std::make_shared<session>(
std::move(socket),
std::move(socket_),
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -251,7 +251,7 @@ class session : public std::enable_shared_from_this<session>
}
};
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -259,7 +259,7 @@ class session : public std::enable_shared_from_this<session>
send_lambda lambda_;
public:
// Take ownership of the socket
// Take ownership of the stream
session(
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root)
@@ -352,7 +352,9 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -360,7 +362,9 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -412,13 +416,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -428,10 +433,13 @@ public:
{
// Create the session and run it
std::make_shared<session>(
std::move(socket),
std::move(socket_),
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -207,11 +207,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of stream to use
// Stackful coroutines are already stranded.
using stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::executor_type>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -223,13 +218,13 @@ fail(beast::error_code ec, char const* what)
// The function object is used to send an HTTP message.
struct send_lambda
{
stream_type& stream_;
beast::ssl_stream<beast::tcp_stream>& stream_;
bool& close_;
beast::error_code& ec_;
net::yield_context yield_;
send_lambda(
stream_type& stream,
beast::ssl_stream<beast::tcp_stream>& stream,
bool& close,
beast::error_code& ec,
net::yield_context yield)
@@ -258,7 +253,7 @@ struct send_lambda
// Handles an HTTP server connection
void
do_session(
stream_type& stream,
beast::ssl_stream<beast::tcp_stream>& stream,
std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield)
{
@@ -357,10 +352,11 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
stream_type(std::move(socket), ctx),
beast::ssl_stream<beast::tcp_stream>(
std::move(socket), ctx),
doc_root,
std::placeholders::_1));
}
@@ -204,10 +204,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of stream to use.
// Stackful coroutines are already stranded.
using stream_type = beast::tcp_stream<net::io_context::executor_type>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -219,13 +215,13 @@ fail(beast::error_code ec, char const* what)
// The function object is used to send an HTTP message.
struct send_lambda
{
stream_type& stream_;
beast::tcp_stream& stream_;
bool& close_;
beast::error_code& ec_;
net::yield_context yield_;
send_lambda(
stream_type& stream,
beast::tcp_stream& stream,
bool& close,
beast::error_code& ec,
net::yield_context yield)
@@ -254,7 +250,7 @@ struct send_lambda
// Handles an HTTP server connection
void
do_session(
stream_type& stream,
beast::tcp_stream& stream,
std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield)
{
@@ -339,10 +335,10 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
stream_type(std::move(socket)),
beast::tcp_stream(std::move(socket)),
doc_root,
std::placeholders::_1));
}
@@ -99,7 +99,7 @@ private:
std::string doc_root_;
// The socket for the currently connected client.
tcp::socket socket_{acceptor_.get_executor().context()};
tcp::socket socket_{acceptor_.get_executor()};
// The buffer for performing reads
beast::flat_static_buffer<8192> buffer_;
@@ -112,7 +112,7 @@ private:
// The timer putting a time limit on requests.
net::basic_waitable_timer<std::chrono::steady_clock> request_deadline_{
acceptor_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()};
acceptor_.get_executor(), (std::chrono::steady_clock::time_point::max)()};
// The string-based response message.
boost::optional<http::response<http::string_body, http::basic_fields<alloc_t>>> string_response_;
+19 -17
View File
@@ -208,13 +208,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of plain streams
using plain_stream_type = beast::tcp_stream<net::io_context::strand>;
// The type of TLS streams
using ssl_stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -358,7 +351,7 @@ class plain_session
: public session<plain_session>
, public std::enable_shared_from_this<plain_session>
{
plain_stream_type stream_;
beast::tcp_stream stream_;
public:
// Create the session
@@ -374,7 +367,7 @@ public:
}
// Called by the base class
plain_stream_type&
beast::tcp_stream&
stream()
{
return stream_;
@@ -403,7 +396,7 @@ class ssl_session
: public session<ssl_session>
, public std::enable_shared_from_this<ssl_session>
{
ssl_stream_type stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
public:
// Create the session
@@ -420,7 +413,7 @@ public:
}
// Called by the base class
ssl_stream_type&
beast::ssl_stream<beast::tcp_stream>&
stream()
{
return stream_;
@@ -442,6 +435,7 @@ public:
&ssl_session::on_handshake,
shared_from_this()));
}
void
on_handshake(
beast::error_code ec,
@@ -484,14 +478,14 @@ public:
// Detects SSL handshakes
class detect_session : public std::enable_shared_from_this<detect_session>
{
plain_stream_type stream_;
beast::tcp_stream stream_;
ssl::context& ctx_;
std::shared_ptr<std::string const> doc_root_;
beast::flat_buffer buffer_;
public:
detect_session(
tcp::socket socket,
tcp::socket&& socket,
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
@@ -544,8 +538,10 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -554,8 +550,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -607,13 +605,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket sock)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -623,11 +622,14 @@ public:
{
// Create the detector session and run it
std::make_shared<detect_session>(
std::move(sock),
std::move(socket_),
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -76,7 +76,7 @@ private:
// The timer for putting a deadline on connection processing.
net::basic_waitable_timer<std::chrono::steady_clock> deadline_{
socket_.get_executor().context(), std::chrono::seconds(60)};
socket_.get_executor(), std::chrono::seconds(60)};
// Asynchronously receive a complete request message.
void
@@ -209,10 +209,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of TLS streams
using ssl_stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -264,7 +260,7 @@ class session
}
};
ssl_stream_type stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -383,6 +379,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
@@ -394,9 +391,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -470,6 +468,9 @@ public:
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}
@@ -205,9 +205,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of stream to use
using stream_type = beast::tcp_stream<net::io_context::strand>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -258,7 +255,7 @@ class session
}
};
stream_type stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -269,7 +266,7 @@ public:
// Take ownership of the socket
explicit
session(
tcp::socket socket,
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
, doc_root_(doc_root)
@@ -348,6 +345,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
@@ -357,8 +355,9 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -429,6 +428,9 @@ public:
std::move(socket_),
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}
@@ -45,8 +45,8 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::executor_type>>> ws_;
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
@@ -55,8 +55,8 @@ public:
// Resolver and socket require an io_context
explicit
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, ws_(ioc, ctx)
: resolver_(beast::make_strand(ioc))
, ws_(beast::make_strand(ioc), ctx)
{
}
@@ -40,8 +40,7 @@ fail(beast::error_code ec, char const* what)
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<
beast::tcp_stream<net::io_context::executor_type>> ws_;
websocket::stream<beast::tcp_stream> ws_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
@@ -50,8 +49,8 @@ public:
// Resolver and socket require an io_context
explicit
session(net::io_context& ioc)
: resolver_(ioc)
, ws_(ioc)
: resolver_(beast::make_strand(ioc))
, ws_(beast::make_strand(ioc))
{
}
@@ -54,9 +54,9 @@ do_session(
beast::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ioc};
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::executor_type>>> ws(ioc, ctx);
tcp::resolver resolver(ioc);
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws(ioc, ctx);
// Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]);
@@ -48,9 +48,8 @@ do_session(
beast::error_code ec;
// These objects perform our I/O
tcp::resolver resolver{ioc};
websocket::stream<
beast::tcp_stream<net::io_context::executor_type>> ws(ioc);
tcp::resolver resolver(ioc);
websocket::stream<beast::tcp_stream> ws(ioc);
// Look up the domain name
auto const results = resolver.async_resolve(host, port, yield[ec]);
@@ -48,13 +48,13 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
class session : public std::enable_shared_from_this<session>
{
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>> ws_;
websocket::stream<
beast::ssl_stream<beast::tcp_stream>> ws_;
beast::multi_buffer buffer_;
public:
// Take ownership of the socket
session(tcp::socket socket, ssl::context& ctx)
session(tcp::socket&& socket, ssl::context& ctx)
: ws_(std::move(socket), ctx)
{
}
@@ -172,16 +172,20 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
public:
listener(
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
, acceptor_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
{
beast::error_code ec;
@@ -232,13 +236,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -247,9 +252,12 @@ public:
else
{
// Create the session and run it
std::make_shared<session>(std::move(socket), ctx_)->run();
std::make_shared<session>(std::move(socket_), ctx_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -43,13 +43,13 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
class session : public std::enable_shared_from_this<session>
{
websocket::stream<beast::tcp_stream<net::io_context::strand>> ws_;
websocket::stream<beast::tcp_stream> ws_;
beast::multi_buffer buffer_;
public:
// Take ownership of the socket
explicit
session(tcp::socket socket)
session(tcp::socket&& socket)
: ws_(std::move(socket))
{
}
@@ -146,13 +146,17 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ioc)
: ioc_(ioc)
, acceptor_(ioc)
, socket_(beast::make_strand(ioc_))
{
beast::error_code ec;
@@ -203,13 +207,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -218,9 +223,12 @@ public:
else
{
// Create the session and run it
std::make_shared<session>(std::move(socket))->run();
std::make_shared<session>(std::move(socket_))->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -21,7 +21,7 @@
*/
class http_session : public boost::enable_shared_from_this<http_session>
{
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
boost::shared_ptr<shared_state> state_;
http::request<http::string_body> req_;
@@ -16,7 +16,9 @@ listener(
net::io_context& ioc,
tcp::endpoint endpoint,
boost::shared_ptr<shared_state> const& state)
: acceptor_(ioc)
: ioc_(ioc)
, acceptor_(ioc)
, socket_(beast::make_strand(ioc))
, state_(state)
{
beast::error_code ec;
@@ -61,6 +63,7 @@ run()
{
// Start accepting a connection
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
@@ -80,18 +83,22 @@ fail(beast::error_code ec, char const* what)
// Handle a connection
void
listener::
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
return fail(ec, "accept");
else
// Launch a new session for this connection
boost::make_shared<http_session>(
std::move(socket),
std::move(socket_),
state_)->run();
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
@@ -22,11 +22,13 @@ class shared_state;
// Accepts incoming connections and launches the sessions
class listener : public boost::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
boost::shared_ptr<shared_state> state_;
void fail(beast::error_code ec, char const* what);
void on_accept(beast::error_code ec, tcp::socket socket);
void on_accept(beast::error_code ec);
public:
listener(
@@ -83,17 +83,22 @@ void
websocket_session::
send(boost::shared_ptr<std::string const> const& ss)
{
// Get on the strand if we aren't already,
// otherwise we will concurrently access
// objects which are not thread-safe.
if(! ws_.get_executor().running_in_this_thread())
return net::post(
ws_.get_executor(),
beast::bind_front_handler(
&websocket_session::send,
shared_from_this(),
ss));
// Post our work to the strand, this ensures
// that the members of `this` will not be
// accessed concurrently.
net::post(
ws_.get_executor(),
beast::bind_front_handler(
&websocket_session::on_send,
shared_from_this(),
ss));
}
void
websocket_session::
on_send(boost::shared_ptr<std::string const> const& ss)
{
// Always add to queue
queue_.push_back(ss);
@@ -27,7 +27,7 @@ class shared_state;
class websocket_session : public boost::enable_shared_from_this<websocket_session>
{
beast::flat_buffer buffer_;
websocket::stream<beast::tcp_stream<net::io_context::strand>> ws_;
websocket::stream<beast::tcp_stream> ws_;
boost::shared_ptr<shared_state> state_;
std::vector<boost::shared_ptr<std::string const>> queue_;
@@ -50,6 +50,10 @@ public:
// Send a message
void
send(boost::shared_ptr<std::string const> const& ss);
private:
void
on_send(boost::shared_ptr<std::string const> const& ss);
};
template<class Body, class Allocator>
@@ -38,11 +38,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of websocket stream to use
// Stackful coroutines are already stranded.
using ws_type = websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::executor_type>>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -53,7 +48,8 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
void
do_session(
ws_type& ws,
websocket::stream<
beast::ssl_stream<beast::tcp_stream>>& ws,
net::yield_context yield)
{
beast::error_code ec;
@@ -153,10 +149,11 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
ws_type(std::move(socket), ctx),
websocket::stream<beast::ssl_stream<
beast::tcp_stream>>(std::move(socket), ctx),
std::placeholders::_1));
}
}
@@ -33,11 +33,6 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// The type of websocket stream to use
// Stackful coroutines are already stranded.
using ws_type = websocket::stream<
beast::tcp_stream<net::io_context::executor_type>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -47,7 +42,9 @@ fail(beast::error_code ec, char const* what)
// Echoes back all received WebSocket messages
void
do_session(ws_type& ws, net::yield_context yield)
do_session(
websocket::stream<beast::tcp_stream>& ws,
net::yield_context yield)
{
beast::error_code ec;
@@ -133,10 +130,11 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
ws_type(std::move(socket)),
websocket::stream<
beast::tcp_stream>(std::move(socket)),
std::placeholders::_1));
}
}
@@ -157,7 +155,7 @@ int main(int argc, char* argv[])
auto const threads = std::max<int>(1, std::atoi(argv[3]));
// The io_context is required for all I/O
net::io_context ioc{threads};
net::io_context ioc(threads);
// Spawn a listening port
net::spawn(ioc,
@@ -78,13 +78,8 @@ setup_stream(websocket::stream<NextLayer>& ws)
//------------------------------------------------------------------------------
// The type of websocket stream to use
// Stackful coroutines are already stranded.
using ws_type = websocket::stream<
beast::tcp_stream<net::io_context::executor_type>>;
void
do_sync_session(ws_type& ws)
do_sync_session(websocket::stream<beast::tcp_stream>& ws)
{
beast::error_code ec;
@@ -135,7 +130,8 @@ do_sync_listen(
std::thread(std::bind(
&do_sync_session,
ws_type(std::move(socket)))).detach();
websocket::stream<beast::tcp_stream>(
std::move(socket)))).detach();
}
}
@@ -144,13 +140,13 @@ do_sync_listen(
// Echoes back all received WebSocket messages
class async_session : public std::enable_shared_from_this<async_session>
{
websocket::stream<beast::tcp_stream<net::io_context::strand>> ws_;
websocket::stream<beast::tcp_stream> ws_;
beast::multi_buffer buffer_;
public:
// Take ownership of the socket
explicit
async_session(tcp::socket socket)
async_session(tcp::socket&& socket)
: ws_(std::move(socket))
{
setup_stream(ws_);
@@ -160,6 +156,11 @@ public:
void
run()
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
@@ -240,13 +241,17 @@ public:
// Accepts incoming connections and launches the sessions
class async_listener : public std::enable_shared_from_this<async_listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
public:
async_listener(
net::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
{
beast::error_code ec;
@@ -297,13 +302,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&async_listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -312,9 +318,12 @@ public:
else
{
// Create the async_session and run it
std::make_shared<async_session>(std::move(socket))->run();
std::make_shared<async_session>(std::move(socket_))->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -323,12 +332,19 @@ public:
//------------------------------------------------------------------------------
void
do_coro_session(ws_type& ws, net::yield_context yield)
do_coro_session(
websocket::stream<beast::tcp_stream>& ws,
net::yield_context yield)
{
beast::error_code ec;
setup_stream(ws);
// Set suggested timeout settings for the websocket
ws.set_option(
websocket::stream_base::suggested_settings(
websocket::role_type::server));
// Set a decorator to change the Server of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
@@ -395,10 +411,11 @@ do_coro_listen(
}
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_coro_session,
ws_type(std::move(socket)),
websocket::stream<
beast::tcp_stream>(std::move(socket)),
std::placeholders::_1));
}
}
@@ -51,8 +51,7 @@ class session
: public net::coroutine
, public std::enable_shared_from_this<session>
{
websocket::stream<beast::ssl_stream<
beast::tcp_stream<net::io_context::strand>>> ws_;
websocket::stream<beast::ssl_stream<beast::tcp_stream>> ws_;
beast::multi_buffer buffer_;
public:
@@ -166,6 +165,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
@@ -175,7 +175,8 @@ public:
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint)
: ctx_(ctx)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
{
@@ -247,6 +248,9 @@ public:
// Create the session and run it
std::make_shared<session>(std::move(socket_), ctx_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}
@@ -46,8 +46,7 @@ class session
: public net::coroutine
, public std::enable_shared_from_this<session>
{
websocket::stream<
beast::tcp_stream<net::io_context::strand>> ws_;
websocket::stream<beast::tcp_stream> ws_;
beast::multi_buffer buffer_;
public:
@@ -143,6 +142,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
@@ -150,8 +150,9 @@ public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint)
: acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
{
beast::error_code ec;
@@ -221,6 +222,9 @@ public:
// Create the session and run it
std::make_shared<session>(std::move(socket_))->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}