aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/main/server.cpp
blob: a7ff2943e674ccb15469b822c6582dca7831c1fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.

#include "server.hpp"

#include "session.hpp"
#include "session_manager.hpp"
#include "settings.hpp"

#include <common/error.hpp>
#include <common/log.hpp>

#include <boost/asio.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>

#include <cstddef>
#include <exception>
#include <thread>
#include <vector>

namespace math::server {
namespace {

boost::asio::ip::tcp::endpoint make_endpoint(unsigned short port) {
    return {boost::asio::ip::tcp::v4(), port};
}

void configure_acceptor(boost::asio::ip::tcp::acceptor& acceptor, unsigned short port) {
    try {
        const auto endpoint = make_endpoint(port);
        acceptor.open(endpoint.protocol());
        acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
        acceptor.bind(endpoint);
        acceptor.listen();
    } catch (const boost::system::system_error& e) {
        throw Error{e.what()};
    }
}

} // namespace

Server::Server(const Settings& settings) : Server{settings.m_port, settings.m_threads} {}

Server::Server(unsigned short port, std::size_t threads)
    : m_numof_threads{threads}, m_signals{m_io_context}, m_acceptor{m_io_context} {
    wait_for_signal();
    configure_acceptor(m_acceptor, port);

    accept();
}

void Server::run() {
    std::vector<std::thread> threads{m_numof_threads};
    for (std::size_t i = 0; i < m_numof_threads; ++i) {
        threads[i] = std::thread{[this]() { m_io_context.run(); }};
    }

    for (std::size_t i = 0; i < m_numof_threads; ++i) {
        threads[i].join();
    }
}

void Server::wait_for_signal() {
    try {
        m_signals.add(SIGINT);
        m_signals.add(SIGTERM);

        m_signals.async_wait(
            [this](const boost::system::error_code& ec, int signo) { handle_signal(ec, signo); });
    } catch (const boost::system::system_error& e) {
        throw Error{e.what()};
    }
}

void Server::handle_signal(const boost::system::error_code& ec, int signo) {
    if (ec) {
        log::error("%1%: %2%", __func__, ec.message());
    }

    log::log("Caught signal %1%", signo);

    try {
        m_acceptor.close();
        m_session_mgr.stop_all();
    } catch (const std::exception& e) {
        log::error(e.what());
    }
}

void Server::accept() {
    const auto session = m_session_mgr.make_session(m_io_context);
    m_acceptor.async_accept(
        session->socket(),
        [session, this](const boost::system::error_code& ec) { handle_accept(session, ec); });
}

void Server::handle_accept(SessionPtr session, const boost::system::error_code& ec) {
    if (ec) {
        log::error("%1%: %2%", __func__, ec.message());
        return;
    }

    m_session_mgr.start(session);
    accept();
}

} // namespace math::server