boost json

This commit is contained in:
CJ Cobb
2020-12-15 12:02:11 -05:00
parent 833df2b3a4
commit 9d99294953
4 changed files with 156 additions and 179 deletions

View File

@@ -13,24 +13,27 @@
//
//------------------------------------------------------------------------------
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/json.hpp>
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
//------------------------------------------------------------------------------
@@ -49,9 +52,7 @@ class session : public std::enable_shared_from_this<session>
public:
// Take ownership of the socket
explicit
session(tcp::socket&& socket)
: ws_(std::move(socket))
explicit session(tcp::socket&& socket) : ws_(std::move(socket))
{
}
@@ -63,10 +64,9 @@ public:
// on the I/O objects in this session. Although not strictly necessary
// for single-threaded contexts, this example code is written to be
// thread-safe by default.
net::dispatch(ws_.get_executor(),
beast::bind_front_handler(
&session::on_run,
shared_from_this()));
net::dispatch(
ws_.get_executor(),
beast::bind_front_handler(&session::on_run, shared_from_this()));
}
// Start the asynchronous operation
@@ -74,29 +74,26 @@ public:
on_run()
{
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::timeout::suggested(
beast::role_type::server));
ws_.set_option(websocket::stream_base::timeout::suggested(
beast::role_type::server));
// Set a decorator to change the Server of the handshake
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
[](websocket::response_type& res) {
res.set(
http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-async");
}));
// Accept the websocket handshake
ws_.async_accept(
beast::bind_front_handler(
&session::on_accept,
shared_from_this()));
beast::bind_front_handler(&session::on_accept, shared_from_this()));
}
void
on_accept(beast::error_code ec)
{
if(ec)
if (ec)
return fail(ec, "accept");
// Read a message
@@ -109,42 +106,34 @@ public:
// Read a message into our buffer
ws_.async_read(
buffer_,
beast::bind_front_handler(
&session::on_read,
shared_from_this()));
beast::bind_front_handler(&session::on_read, shared_from_this()));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
on_read(beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This indicates that the session was closed
if(ec == websocket::error::closed)
if (ec == websocket::error::closed)
return;
if(ec)
if (ec)
fail(ec, "read");
// Echo the message
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
beast::bind_front_handler(
&session::on_write,
shared_from_this()));
beast::bind_front_handler(&session::on_write, shared_from_this()));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
on_write(beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
if (ec)
return fail(ec, "write");
// Clear the buffer
@@ -164,17 +153,14 @@ class listener : public std::enable_shared_from_this<listener>
tcp::acceptor acceptor_;
public:
listener(
net::io_context& ioc,
tcp::endpoint endpoint)
: ioc_(ioc)
, acceptor_(ioc)
listener(net::io_context& ioc, tcp::endpoint endpoint)
: ioc_(ioc), acceptor_(ioc)
{
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if(ec)
if (ec)
{
fail(ec, "open");
return;
@@ -182,7 +168,7 @@ public:
// Allow address reuse
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if(ec)
if (ec)
{
fail(ec, "set_option");
return;
@@ -190,16 +176,15 @@ public:
// Bind to the server address
acceptor_.bind(endpoint, ec);
if(ec)
if (ec)
{
fail(ec, "bind");
return;
}
// Start listening for connections
acceptor_.listen(
net::socket_base::max_listen_connections, ec);
if(ec)
acceptor_.listen(net::socket_base::max_listen_connections, ec);
if (ec)
{
fail(ec, "listen");
return;
@@ -221,14 +206,13 @@ private:
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
&listener::on_accept, shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
{
if(ec)
if (ec)
{
fail(ec, "accept");
}
@@ -243,22 +227,38 @@ private:
}
};
std::optional<boost::json::object>
parse_config(const char* filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
std::stringstream contents;
contents << in.rdbuf();
in.close();
boost::json::value value = boost::json::parse(contents.str());
return value.as_object();
}
return {};
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
int
main(int argc, char* argv[])
{
// Check command line arguments.
if (argc != 4)
{
std::cerr <<
"Usage: websocket-server-async <address> <port> <threads>\n" <<
"Example:\n" <<
" websocket-server-async 0.0.0.0 8080 1\n";
std::cerr
<< "Usage: websocket-server-async <address> <port> <threads>\n"
<< "Example:\n"
<< " websocket-server-async 0.0.0.0 8080 1\n";
return EXIT_FAILURE;
}
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
auto const config = parse_config(argv[4]);
// The io_context is required for all I/O
net::io_context ioc{threads};
@@ -269,12 +269,8 @@ int main(int argc, char* argv[])
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
for (auto i = threads - 1; i > 0; --i)
v.emplace_back([&ioc] { ioc.run(); });
ioc.run();
return EXIT_SUCCESS;