-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graceful shutdown on SIGINT/SIGTERM, rate-limiting, basic echo websoc…
…ket server
- Loading branch information
Showing
8 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "websocket_session.hpp" | ||
|
||
websocket_session::websocket_session( | ||
tcp::socket&& socket | ||
) : ws(std::move(socket)) {} | ||
|
||
void websocket_session::on_accept(beast::error_code ec) { | ||
if (ec) { | ||
std::cerr << "Couldn't accept a WebSocket request: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
// read the message | ||
do_read(); | ||
} | ||
|
||
void websocket_session::do_read() { | ||
// read into buffer | ||
ws.async_read( | ||
buffer, | ||
beast::bind_front_handler( | ||
&websocket_session::on_read, | ||
shared_from_this() | ||
) | ||
); | ||
} | ||
|
||
void websocket_session::on_read( | ||
beast::error_code ec, | ||
std::size_t bytes_transferred | ||
) { | ||
boost::ignore_unused(bytes_transferred); | ||
|
||
if (ec == websocket::error::closed) { | ||
return; | ||
} | ||
|
||
if (ec) { | ||
std::cerr << "Couldn't read incoming WebSocket message: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
ws.text(ws.got_text()); | ||
|
||
ws.async_write( | ||
buffer.data(), | ||
beast::bind_front_handler( | ||
&websocket_session::on_write, | ||
shared_from_this() | ||
) | ||
); | ||
} | ||
|
||
void websocket_session::on_write( | ||
beast::error_code ec, | ||
std::size_t bytes_transferred | ||
) { | ||
boost::ignore_unused(bytes_transferred); | ||
|
||
if (ec) { | ||
std::cerr << "Couldn't write to a WebSocket stream: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
buffer.consume(buffer.size()); | ||
|
||
// start another read | ||
do_read(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef WEBSOCKET_SESSION_HPP | ||
#define WEBSOCKET_SESSION_HPP | ||
|
||
#include "common.hpp" | ||
#include <boost/beast/websocket.hpp> | ||
#include <memory> | ||
|
||
class websocket_session : public std::enable_shared_from_this<websocket_session> { | ||
websocket::stream<beast::tcp_stream> ws; | ||
beast::flat_buffer buffer; | ||
|
||
public: | ||
explicit websocket_session(tcp::socket&& socket); | ||
|
||
template<class Body, class Allocator> | ||
void do_accept( | ||
http::request<Body, http::basic_fields<Allocator>> req | ||
) { | ||
ws.set_option( | ||
websocket::stream_base::timeout::suggested( | ||
beast::role_type::server | ||
) | ||
); | ||
|
||
// append a Server field to every response | ||
ws.set_option( | ||
websocket::stream_base::decorator( | ||
[](websocket::response_type& res) { | ||
res.set(http::field::server, VERSION); | ||
} | ||
) | ||
); | ||
|
||
ws.async_accept( | ||
req, | ||
beast::bind_front_handler( | ||
&websocket_session::on_accept, | ||
shared_from_this() | ||
) | ||
); | ||
} | ||
|
||
private: | ||
void on_accept(beast::error_code ec); | ||
void do_read(); | ||
void on_write(beast::error_code ec, std::size_t bytes_transferred); | ||
void on_read(beast::error_code ec, std::size_t bytes_transferred); | ||
}; | ||
|
||
#endif |