Skip to content

Commit 64e52f2

Browse files
committed
fixed review findings
1 parent 30a4027 commit 64e52f2

7 files changed

Lines changed: 43 additions & 42 deletions

File tree

src/boost.beast/data.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <nlohmann/json.hpp>
3+
#include <string>
34

45
namespace data {
56
// a simple struct to model a person
@@ -20,5 +21,4 @@ inline void from_json(const nlohmann::json &j, person &p)
2021
j.at("age").get_to(p.age);
2122
j.at("id").get_to(p.id);
2223
}
23-
24-
}// namespace data
24+
}// namespace data

src/boost.beast/error_handling.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <boost/beast/core.hpp>
3+
#include <boost/beast/http.hpp>
4+
#include <boost/beast/version.hpp>
5+
#include <boost/asio/dispatch.hpp>
6+
#include <boost/asio/strand.hpp>
7+
#include <fmt/format.h>
8+
9+
namespace beast = boost::beast;// from <boost/beast.hpp>
10+
namespace http = beast::http;// from <boost/beast/http.hpp>
11+
namespace asio = boost::asio;// from <boost/asio.hpp>
12+
using tcp = boost::asio::ip::tcp;// from <boost/asio/ip/tcp.hpp>
13+
14+
15+
inline void fail(beast::error_code ec, char const *what) { fmt::format("FAILED {0}: {1}", what, ec.message()); }

src/boost.beast/helper.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/boost.beast/listener.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#pragma once
2-
#include "helper.h"
2+
#include "error_handling.h"
33
#include "data.h"
44
#include "session.h"
55

66
// Accepts incoming connections and launches the sessions
77
class Listener : public std::enable_shared_from_this<Listener>
88
{
99
public:
10-
Listener(net::io_context &ioc, const tcp::endpoint &endpoint) : m_ioc(ioc), m_acceptor(net::make_strand(ioc))
10+
Listener(asio::io_context &ioc, const tcp::endpoint &endpoint) : m_ioc(ioc), m_acceptor(asio::make_strand(ioc))
1111
{
1212
beast::error_code ec;
1313

@@ -20,7 +20,7 @@ class Listener : public std::enable_shared_from_this<Listener>
2020
}
2121

2222
// Allow address reuse
23-
m_acceptor.set_option(net::socket_base::reuse_address(true), ec);
23+
m_acceptor.set_option(asio::socket_base::reuse_address(true), ec);
2424
if (ec)
2525
{
2626
fail(ec, "set_option");
@@ -36,7 +36,7 @@ class Listener : public std::enable_shared_from_this<Listener>
3636
}
3737

3838
// Start listening for connections
39-
m_acceptor.listen(net::socket_base::max_listen_connections, ec);
39+
m_acceptor.listen(asio::socket_base::max_listen_connections, ec);
4040
if (ec)
4141
{
4242
fail(ec, "listen");
@@ -45,13 +45,13 @@ class Listener : public std::enable_shared_from_this<Listener>
4545
}
4646

4747
// Start accepting incoming connections
48-
void run() { doAccept(); }
48+
void run() { acceptNextConnection(); }
4949

5050
private:
51-
void doAccept()
51+
void acceptNextConnection()
5252
{
5353
// The new connection gets its own strand
54-
m_acceptor.async_accept(net::make_strand(m_ioc), beast::bind_front_handler(&Listener::onAccept, shared_from_this()));
54+
m_acceptor.async_accept(asio::make_strand(m_ioc), beast::bind_front_handler(&Listener::onAccept, shared_from_this()));
5555
}
5656

5757
void onAccept(beast::error_code ec, tcp::socket socket)
@@ -67,10 +67,10 @@ class Listener : public std::enable_shared_from_this<Listener>
6767
}
6868

6969
// Accept another connection
70-
doAccept();
70+
acceptNextConnection();
7171
}
7272

73-
net::io_context &m_ioc;
73+
asio::io_context &m_ioc;
7474
tcp::acceptor m_acceptor;
7575
std::vector<data::person> m_persons;
7676
};

src/boost.beast/main.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
#include <boost/beast/core.hpp>
2-
#include <boost/beast/http.hpp>
3-
#include <boost/beast/version.hpp>
4-
#include <boost/asio/dispatch.hpp>
5-
#include <boost/asio/strand.hpp>
61
#include <cstdlib>
7-
#include <iostream>
82
#include <memory>
93
#include <string>
104
#include <thread>
@@ -14,19 +8,19 @@
148

159
int main()
1610
{
17-
auto const address = net::ip::make_address("0.0.0.0");
18-
auto const port = static_cast<unsigned short>(8080);
19-
auto const threads = 1;
11+
const auto address = asio::ip::make_address("0.0.0.0");
12+
const uint16_t port = 8080u;
13+
const auto threads = 1;
2014

2115
// The io_context is required for all I/O
22-
net::io_context ioc{ threads };
16+
asio::io_context ioc{ threads };
2317

2418
// Create and launch a listening port
2519
std::make_shared<Listener>(ioc, tcp::endpoint{ address, port })->run();
2620

2721
// Run the I/O service on the requested number of threads
2822
std::vector<std::thread> v;
29-
v.reserve(threads - 1);
23+
v.reserve(threads);
3024
for (auto i = 0; i < threads; i++)
3125
{
3226
v.emplace_back([&ioc] { ioc.run(); });

src/boost.beast/request.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
2+
#include "error_handling.h"
23

3-
template<typename Request> http::response<http::string_body> createResponse(Request &&req, http::status status, const nlohmann::json &jsonResponse)
4+
template<typename Request> http::response<http::string_body> inline createResponse(Request &&req, http::status status, const nlohmann::json &jsonResponse)
45
{
56
http::response<http::string_body> res{ status, req.version() };
67
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
@@ -11,18 +12,19 @@ template<typename Request> http::response<http::string_body> createResponse(Requ
1112
return res;
1213
}
1314

14-
// Returns a bad request response
15-
const auto badRequest = [&req](beast::string_view why) {
16-
const auto j = nlohmann::json::parse(std::string(why));
17-
return createResponse(req, http::status::bad_request, j);
18-
};
1915

2016
// This function produces an HTTP response for the given
2117
// request. The type of the response object depends on the
2218
// contents of the request, so the interface requires the
2319
// caller to pass a generic lambda for receiving the response.
24-
template<class Body, class Allocator, class Send> void handleRequest(http::request<Body, http::basic_fields<Allocator>> &&req, Send &&send, std::vector<data::person> &data)
20+
template<class Body, class Allocator, class Send> inline void handleRequest(http::request<Body, http::basic_fields<Allocator>> &&req, Send &&send, std::vector<data::person> &data)
2521
{
22+
// Returns a bad request response
23+
const auto badRequest = [&req](beast::string_view why) {
24+
const auto j = nlohmann::json::parse(std::string(why));
25+
return createResponse(req, http::status::bad_request, j);
26+
};
27+
2628
// Make sure we can handle the method
2729
switch (req.method())
2830
{

src/boost.beast/session.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "helper.h"
2+
#include "error_handling.h"
33
#include "request.h"
44

55
// Handles an HTTP server connection
@@ -16,7 +16,7 @@ class Session : public std::enable_shared_from_this<Session>
1616
// on the I/O objects in this session. Although not strictly necessary
1717
// for single-threaded contexts, this example code is written to be
1818
// thread-safe by default.
19-
net::dispatch(m_stream.get_executor(), beast::bind_front_handler(&Session::doRead, shared_from_this()));
19+
asio::dispatch(m_stream.get_executor(), beast::bind_front_handler(&Session::doRead, shared_from_this()));
2020
}
2121

2222
void doRead()
@@ -85,7 +85,7 @@ class Session : public std::enable_shared_from_this<Session>
8585

8686
private:
8787
std::vector<data::person> &m_person;
88-
// This is the C++11 equivalent of a generic lambda.
88+
8989
// The function object is used to send an HTTP message.
9090
struct sendLambda
9191
{

0 commit comments

Comments
 (0)