forked from boostorg/beast
Use tcp_stream in WebSocket server examples
This commit is contained in:
1 parent
920909673a
commit
6ee1a88292
15 files changed
+215
-283
No files matched your search
@@ -190,11 +190,10 @@ handle_request(
|
||||
|
||||
http_session::
|
||||
http_session(
|
||||
tcp::socket socket,
|
||||
tcp::socket&& socket,
|
||||
boost::shared_ptr<shared_state> const& state)
|
||||
: socket_(std::move(socket))
|
||||
: stream_(std::move(socket))
|
||||
, state_(state)
|
||||
, strand_(socket_.get_executor())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -202,14 +201,16 @@ void
|
||||
http_session::
|
||||
run()
|
||||
{
|
||||
// Set the timeout.
|
||||
stream_.expires_after(std::chrono::seconds(30));
|
||||
|
||||
// Read a request
|
||||
http::async_read(socket_, buffer_, req_,
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
http::async_read(stream_, buffer_, req_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
// Report a failure
|
||||
@@ -239,13 +240,12 @@ operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
// Write the response
|
||||
auto self = self_.shared_from_this();
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
self_.stream_,
|
||||
*sp,
|
||||
net::bind_executor(self_.strand_,
|
||||
[self, sp](beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
}));
|
||||
[self, sp](beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
@@ -255,7 +255,7 @@ on_read(beast::error_code ec, std::size_t)
|
||||
// This means they closed the connection
|
||||
if(ec == http::error::end_of_stream)
|
||||
{
|
||||
socket_.shutdown(tcp::socket::shutdown_send, ec);
|
||||
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -268,7 +268,8 @@ on_read(beast::error_code ec, std::size_t)
|
||||
{
|
||||
// Create a WebSocket session by transferring the socket
|
||||
boost::make_shared<websocket_session>(
|
||||
std::move(socket_), state_)->run(std::move(req_));
|
||||
std::move(stream_.release_socket()),
|
||||
state_)->run(std::move(req_));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -292,23 +293,21 @@ on_read(beast::error_code ec, std::size_t)
|
||||
#if 0
|
||||
// NOTE This causes an ICE in gcc 7.3
|
||||
// Write the response
|
||||
http::async_write(this->socket_, *sp,
|
||||
net::bind_executor(strand_,
|
||||
[self = shared_from_this(), sp](
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
}));
|
||||
http::async_write(this->stream_, *sp,
|
||||
[self = shared_from_this(), sp](
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
#else
|
||||
// Write the response
|
||||
auto self = shared_from_this();
|
||||
http::async_write(this->socket_, *sp,
|
||||
net::bind_executor(strand_,
|
||||
[self, sp](
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
}));
|
||||
http::async_write(stream_, *sp,
|
||||
[self, sp](
|
||||
beast::error_code ec, std::size_t bytes)
|
||||
{
|
||||
self->on_write(ec, bytes, sp->need_eof());
|
||||
});
|
||||
#endif
|
||||
});
|
||||
#else
|
||||
@@ -336,7 +335,7 @@ on_write(beast::error_code ec, std::size_t, bool close)
|
||||
{
|
||||
// This means we should close the connection, usually because
|
||||
// the response indicated the "Connection: close" semantic.
|
||||
socket_.shutdown(tcp::socket::shutdown_send, ec);
|
||||
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -344,8 +343,11 @@ on_write(beast::error_code ec, std::size_t, bool close)
|
||||
// otherwise the read behavior is undefined.
|
||||
req_ = {};
|
||||
|
||||
// Set the timeout.
|
||||
stream_.expires_after(std::chrono::seconds(30));
|
||||
|
||||
// Read another request
|
||||
http::async_read(socket_, buffer_, req_,
|
||||
http::async_read(stream_, buffer_, req_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
shared_from_this(),
|
||||
|
||||
@@ -21,11 +21,10 @@
|
||||
*/
|
||||
class http_session : public boost::enable_shared_from_this<http_session>
|
||||
{
|
||||
tcp::socket socket_;
|
||||
beast::tcp_stream<net::io_context::strand> stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
boost::shared_ptr<shared_state> state_;
|
||||
http::request<http::string_body> req_;
|
||||
net::strand<net::io_context::executor_type> strand_;
|
||||
|
||||
struct send_lambda
|
||||
{
|
||||
@@ -48,7 +47,7 @@ class http_session : public boost::enable_shared_from_this<http_session>
|
||||
|
||||
public:
|
||||
http_session(
|
||||
tcp::socket socket,
|
||||
tcp::socket&& socket,
|
||||
boost::shared_ptr<shared_state> const& state);
|
||||
|
||||
void run();
|
||||
|
||||
@@ -17,7 +17,6 @@ listener(
|
||||
tcp::endpoint endpoint,
|
||||
boost::shared_ptr<shared_state> const& state)
|
||||
: acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, state_(state)
|
||||
{
|
||||
beast::error_code ec;
|
||||
@@ -62,11 +61,11 @@ run()
|
||||
{
|
||||
// Start accepting a connection
|
||||
acceptor_.async_accept(
|
||||
socket_,
|
||||
std::bind(
|
||||
&listener::on_accept,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
// Report a failure
|
||||
@@ -83,21 +82,21 @@ fail(beast::error_code ec, char const* what)
|
||||
// Handle a connection
|
||||
void
|
||||
listener::
|
||||
on_accept(beast::error_code ec)
|
||||
on_accept(beast::error_code ec, tcp::socket socket)
|
||||
{
|
||||
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();
|
||||
|
||||
// Accept another connection
|
||||
acceptor_.async_accept(
|
||||
socket_,
|
||||
std::bind(
|
||||
&listener::on_accept,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
@@ -23,11 +23,10 @@ class shared_state;
|
||||
class listener : public boost::enable_shared_from_this<listener>
|
||||
{
|
||||
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);
|
||||
void on_accept(beast::error_code ec, tcp::socket socket);
|
||||
|
||||
public:
|
||||
listener(
|
||||
|
||||
@@ -35,7 +35,7 @@ main(int argc, char* argv[])
|
||||
std::cerr <<
|
||||
"Usage: websocket-chat-multi <address> <port> <doc_root> <threads>\n" <<
|
||||
"Example:\n" <<
|
||||
" websocket-chat-server 0.0.0.0 8080 .\n";
|
||||
" websocket-chat-server 0.0.0.0 8080 . 5\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto address = net::ip::make_address(argv[1]);
|
||||
|
||||
@@ -12,11 +12,10 @@
|
||||
|
||||
websocket_session::
|
||||
websocket_session(
|
||||
tcp::socket socket,
|
||||
tcp::socket&& socket,
|
||||
boost::shared_ptr<shared_state> const& state)
|
||||
: ws_(std::move(socket))
|
||||
, state_(state)
|
||||
, strand_(ws_.get_executor())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -53,12 +52,11 @@ on_accept(beast::error_code ec)
|
||||
// Read a message
|
||||
ws_.async_read(
|
||||
buffer_,
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -78,12 +76,11 @@ on_read(beast::error_code ec, std::size_t)
|
||||
// Read another message
|
||||
ws_.async_read(
|
||||
buffer_,
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -93,13 +90,13 @@ 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(! strand_.running_in_this_thread())
|
||||
if(! ws_.get_executor().running_in_this_thread())
|
||||
return net::post(
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&websocket_session::send,
|
||||
shared_from_this(),
|
||||
ss)));
|
||||
ws_.get_executor(),
|
||||
std::bind(
|
||||
&websocket_session::send,
|
||||
shared_from_this(),
|
||||
ss));
|
||||
|
||||
// Always add to queue
|
||||
queue_.push_back(ss);
|
||||
@@ -111,12 +108,11 @@ send(boost::shared_ptr<std::string const> const& ss)
|
||||
// We are not currently writing, so send this immediately
|
||||
ws_.async_write(
|
||||
net::buffer(*queue_.front()),
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -134,10 +130,9 @@ on_write(beast::error_code ec, std::size_t)
|
||||
if(! queue_.empty())
|
||||
ws_.async_write(
|
||||
net::buffer(*queue_.front()),
|
||||
net::bind_executor(strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2)));
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
@@ -27,10 +27,9 @@ class shared_state;
|
||||
class websocket_session : public boost::enable_shared_from_this<websocket_session>
|
||||
{
|
||||
beast::flat_buffer buffer_;
|
||||
websocket::stream<tcp::socket> ws_;
|
||||
websocket::stream<beast::tcp_stream<net::io_context::strand>> ws_;
|
||||
boost::shared_ptr<shared_state> state_;
|
||||
std::vector<boost::shared_ptr<std::string const>> queue_;
|
||||
net::strand<net::io_context::executor_type> strand_;
|
||||
|
||||
void fail(beast::error_code ec, char const* what);
|
||||
void on_accept(beast::error_code ec);
|
||||
@@ -39,7 +38,7 @@ class websocket_session : public boost::enable_shared_from_this<websocket_sessio
|
||||
|
||||
public:
|
||||
websocket_session(
|
||||
tcp::socket socket,
|
||||
tcp::socket&& socket,
|
||||
boost::shared_ptr<shared_state> const& state);
|
||||
|
||||
~websocket_session();
|
||||
|
||||
Reference in new issue
Block a user