summaryrefslogtreecommitdiff
path: root/src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h')
-rw-r--r--src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h232
1 files changed, 51 insertions, 181 deletions
diff --git a/src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h b/src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h
index 421462d4d5..86c776bffb 100644
--- a/src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h
+++ b/src/components/transport_manager/test/include/transport_manager/cloud/sample_websocket_server.h
@@ -64,97 +64,32 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
// Report a failure
-void fail(beast::error_code ec, char const* what) {
- std::cerr << what << ": " << ec.message() << "\n";
+void Fail(char const* tag, boost::system::error_code ec) {
+ std::cerr << tag << ": " << ec.message() << "\n";
}
// Accepts incoming connections and launches the WSServer
class WSSession : public std::enable_shared_from_this<WSSession> {
private:
- // Echoes back all received WebSocket messages
class WSServer : public std::enable_shared_from_this<WSServer> {
public:
- // Take ownership of the socket
explicit WSServer(tcp::socket&& socket)
: ws_(std::move(socket)), strand_(ws_.get_executor()) {}
- // Start the asynchronous operation
- void run() {
+ void Run() {
// Accept the websocket handshake
- ws_.async_accept(
- boost::asio::bind_executor(strand_,
- std::bind(&WSServer::on_accept,
- shared_from_this(),
- std::placeholders::_1)));
+ ws_.async_accept(boost::asio::bind_executor(
+ strand_,
+ std::bind(
+ &WSServer::OnAccept, shared_from_this(), std::placeholders::_1)));
}
- void on_accept(beast::error_code ec) {
- std::cout << "SERVER: Accepting connection" << std::endl;
+ void OnAccept(beast::error_code ec) {
if (ec) {
- std::cout << "SERVER: Connection accept FAILED" << std::endl;
- return fail(ec, "accept");
+ return Fail("ERROR_CONNECTION_ACCEPT", ec);
}
- // Read a message
- // do_read();
}
- // void do_read() {
- // // Read a message into our buffer
- // ws_.async_read(
- // buffer_,
- // boost::asio::bind_executor(strand_,
- // std::bind(&WSServer::on_read,
- // shared_from_this(),
- // std::placeholders::_1,
- // std::placeholders::_2)));
- // }
-
- // void do_write() {
- // ws_.text(ws_.got_text());
- // ws_.async_write(buffer_.data(),
- // std::bind(&WSServer::on_write,
- // shared_from_this(),
- // std::placeholders::_1,
- // std::placeholders::_2));
- // }
-
- // void on_read(beast::error_code ec, std::size_t bytes_transferred) {
- // std::cout << "SERVER: receiving data" << std::endl;
-
- // boost::ignore_unused(bytes_transferred);
-
- // // This indicates that the WSServer was closed
- // if (ec == websocket::error::closed) {
- // std::cout << "SERVER: Websocket closed" << std::endl;
- // return;
- // }
-
- // if (ec) {
- // std::cout << "SERVER: Read failed" << std::endl;
- // fail(ec, "read");
- // }
-
- // // Echo the message
- // do_write();
- // }
-
- // void on_write(beast::error_code ec, std::size_t bytes_transferred) {
- // std::cout << "SERVER: Sending data" << std::endl;
-
- // boost::ignore_unused(bytes_transferred);
-
- // if (ec) {
- // std::cout << "SERVER: Write failed" << std::endl;
- // return fail(ec, "write");
- // }
-
- // // Clear the buffer
- // buffer_.consume(buffer_.size());
-
- // // Do another read
- // do_read();
- // }
-
private:
websocket::stream<tcp::socket> ws_;
beast::flat_buffer buffer_;
@@ -165,62 +100,66 @@ class WSSession : public std::enable_shared_from_this<WSSession> {
WSSession(const std::string& address, uint16_t port)
: address_(address), port_(port), acceptor_(ioc_), socket_(ioc_) {
endpoint_ = {boost::asio::ip::make_address(address), port};
-
boost::system::error_code error;
+ // Open the acceptor
acceptor_.open(endpoint_.protocol(), error);
if (error) {
- std::cerr << "ErrorOpen: " << error.message() << std::endl;
+ Fail("ERROR_ACCEPTOR_OPEN", error);
return;
}
+
+ // Allow address reuse
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), error);
if (error) {
- std::cerr << "ErrorSetOption: " << error.message() << std::endl;
+ Fail("ERROR_SET_OPTION", error);
return;
}
+
+ // Bind to the server address
acceptor_.bind(endpoint_, error);
if (error) {
- std::cerr << "ErrorBind: " << error.message() << std::endl;
+ Fail("ERROR_BIND", error);
return;
}
+
+ // Start listening for connections
acceptor_.listen(boost::asio::socket_base::max_listen_connections, error);
if (error) {
- std::cerr << "ErrorListen: " << error.message() << std::endl;
+ Fail("ERROR_LISTEN", error);
return;
}
}
- void run() {
+ void Run() {
if (acceptor_.is_open()) {
acceptor_.async_accept(
socket_,
- std::bind(&WSSession::StartSession, this, std::placeholders::_1));
+ std::bind(&WSSession::on_accept, this, std::placeholders::_1));
ioc_.run();
}
}
- void stop() {
- std::cout << "SERVER: Closing connection" << std::endl;
-
+ void Stop() {
try {
ioc_.stop();
acceptor_.close();
} catch (...) {
- std::cout << "SERVER: Failed to close connection" << std::endl;
+ std::cerr << "Failed to close connection" << std::endl;
}
}
private:
- void StartSession(boost::system::error_code ec) {
+ void on_accept(boost::system::error_code ec) {
if (ec) {
- std::cerr << "ErrorMessage: " << ec.message() << std::endl;
+ Fail("ERROR_ON_ACCEPT", ec);
ioc_.stop();
return;
}
// Make websocket object and start
ws_ = std::make_shared<WSServer>(std::move(socket_));
- ws_->run();
+ ws_->Run();
}
boost::asio::io_context ioc_;
@@ -244,87 +183,30 @@ class WSSSession : public std::enable_shared_from_this<WSSSession> {
: wss_(std::move(socket), ctx) {}
// Start the asynchronous operation
- void run() {
+ void Run() {
// Perform the SSL handshake
wss_.next_layer().async_handshake(ssl::stream_base::server,
- std::bind(&WSSServer::on_handshake,
+ std::bind(&WSSServer::OnSSLHandshake,
shared_from_this(),
std::placeholders::_1));
}
- void on_handshake(beast::error_code ec) {
- std::cout << "DEBUG: SSL Handshake" << std::endl;
- if (ec)
- return fail(ec, "handshake");
+ void OnSSLHandshake(beast::error_code ec) {
+ if (ec) {
+ return Fail("ERROR_SSL_HANDSHAKE", ec);
+ }
// Accept the websocket handshake
wss_.async_accept(std::bind(
- &WSSServer::on_accept, shared_from_this(), std::placeholders::_1));
+ &WSSServer::OnAccept, shared_from_this(), std::placeholders::_1));
}
- void on_accept(beast::error_code ec) {
- std::cout << "DEBUG: Server accepted connection" << std::endl;
- if (ec)
- return fail(ec, "accept");
-
- // Read a message
- // do_read();
+ void OnAccept(beast::error_code ec) {
+ if (ec) {
+ return Fail("ERROR_ON_ACCEPT", ec);
+ }
}
- // void
- // do_read()
- // {
- // // Read a message into our buffer
- // wss_.async_read(
- // buffer_,
- // std::bind(
- // &WSSServer::on_read,
- // shared_from_this(),
- // std::placeholders::_1,
- // std::placeholders::_2));
- // }
-
- // void
- // on_read(
- // beast::error_code ec,
- // std::size_t bytes_transferred)
- // {
- // boost::ignore_unused(bytes_transferred);
-
- // // This indicates that the WSSServer was closed
- // if(ec == websocket::error::closed)
- // return;
-
- // if(ec)
- // fail(ec, "read");
-
- // // Echo the message
- // wss_.text(wss_.got_text());
- // wss_.async_write(
- // buffer_.data(),
- // std::bind(
- // &WSSServer::on_write,
- // shared_from_this(),
- // std::placeholders::_1,
- // std::placeholders::_2));
- // }
-
- // void
- // on_write(
- // beast::error_code ec,
- // std::size_t bytes_transferred)
- // {
- // boost::ignore_unused(bytes_transferred);
-
- // if(ec)
- // return fail(ec, "write");
-
- // // Clear the buffer
- // buffer_.consume(buffer_.size());
-
- // // Do another read
- // do_read();
- // }
private:
websocket::stream<ssl::stream<tcp::socket> > wss_;
beast::flat_buffer buffer_;
@@ -339,81 +221,72 @@ class WSSSession : public std::enable_shared_from_this<WSSSession> {
beast::error_code ec;
endpoint_ = {boost::asio::ip::make_address(address), port};
- std::cout << "DEBUG: Adding certificate" << std::endl;
// Load the certificate
ctx_.use_certificate(
boost::asio::buffer(certificate.c_str(), certificate.size()),
ssl::context::file_format::pem,
ec);
if (ec) {
- fail(ec, "Load certficate");
+ Fail("ERROR_USE_CERTIFICATE", ec);
return;
}
- std::cout << "DEBUG: Adding private key" << std::endl;
// Load the private key
ctx_.use_rsa_private_key(
boost::asio::buffer(private_key.c_str(), private_key.size()),
ssl::context::file_format::pem,
ec);
if (ec) {
- fail(ec, "Load private key");
+ Fail("ERROR_USE_RSA_PRIVATE_KEY", ec);
return;
}
// Open the acceptor
acceptor_.open(endpoint_.protocol(), ec);
if (ec) {
- fail(ec, "open");
+ Fail("EEROR_ACCEPTOR_OPEN", ec);
return;
}
// Allow address reuse
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if (ec) {
- fail(ec, "set_option");
+ Fail("ERROR_SET_OPTION", ec);
return;
}
- std::cout << "DEBUG: Binding to host:" << address << ", port:" << port
- << std::endl;
// Bind to the server address
acceptor_.bind(endpoint_, ec);
if (ec) {
- fail(ec, "bind");
+ Fail("ERROR_BIND", ec);
return;
}
- std::cout << "DEBUG: Start listening for connections" << std::endl;
// Start listening for connections
acceptor_.listen(net::socket_base::max_listen_connections, ec);
if (ec) {
- fail(ec, "listen");
+ Fail("ERROR_LISTEN", ec);
return;
}
}
// Start accepting incoming connections
- void run() {
+ void Run() {
do_accept();
}
- void stop() {
- std::cout << "SERVER: Closing connection" << std::endl;
-
+ void Stop() {
try {
ioc_.stop();
acceptor_.close();
} catch (...) {
- std::cout << "SERVER: Failed to close connection" << std::endl;
+ std::cerr << "Failed to close connection" << std::endl;
}
}
private:
void do_accept() {
if (acceptor_.is_open()) {
- std::cout << "DEBUG: Waiting to accept incoming connections" << std::endl;
- // The new connection gets its own strand
acceptor_.async_accept(socket_,
std::bind(&WSSSession::on_accept,
shared_from_this(),
@@ -423,17 +296,14 @@ class WSSSession : public std::enable_shared_from_this<WSSSession> {
}
void on_accept(boost::system::error_code ec) {
- std::cout << "DEBUG: Accepted incoming connection" << std::endl;
if (ec) {
- fail(ec, "accept");
+ Fail("ERROR_ON_ACCEPT", ec);
ioc_.stop();
return;
- } else {
- std::cout << "DEBUG: Creating secure websocket server" << std::endl;
- // Create the session and run it
- wss_ = std::make_shared<WSSServer>(std::move(socket_), ctx_);
- wss_->run();
}
+ // Create the session and run it
+ wss_ = std::make_shared<WSSServer>(std::move(socket_), ctx_);
+ wss_->Run();
}
private: