aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/main/session.cpp
blob: 900e5b41662d5eaa4757ed7e0f04a0cf7d936ed2 (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
111
112
113
114
115
116
117
118
// 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 "session.hpp"
#include "session_manager.hpp"

#include "../common/error.hpp"
#include "../common/log.hpp"
#include "../parser/parser.hpp"

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

#include <cstddef>

#include <exception>
#include <string>
#include <utility>

namespace math::server {
namespace {

std::string reply_to_string(double result) {
    return boost::lexical_cast<std::string>(result);
}

std::string calc_reply(const std::string& input) {
    std::string reply;
    try {
        reply = reply_to_string(Parser{input}.exec());
    } catch (const std::exception& e) {
        reply = e.what();
    }
    return reply;
}

}

Session::Session(SessionManager& mgr, boost::asio::io_context& io_context)
    : m_session_mgr{mgr}, m_strand{io_context}, m_socket{io_context}
{ }

boost::asio::ip::tcp::socket& Session::socket() {
    return m_socket;
}

void Session::start() {
    read();
}

void Session::stop() {
    close();
}

void Session::close() {
    try {
        m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
        m_socket.close();
    } catch (const boost::system::system_error& e) {
        throw Error{e.what()};
    }
}

void Session::read() {
    const auto self = shared_from_this();

    // Stop at LF
    boost::asio::async_read_until(m_socket, m_buffer, '\n', boost::asio::bind_executor(m_strand,
        [this, self] (const boost::system::error_code& ec, std::size_t bytes) {
            handle_read(ec, bytes);
        }));
}

void Session::handle_read(const boost::system::error_code& ec, std::size_t bytes) {
    if (ec) {
        log::error("%1%: %2%", __func__, ec.message());
        m_session_mgr.stop(shared_from_this());
        return;
    }

    write(calc_reply(consume_input(bytes)));
}

std::string Session::consume_input(std::size_t bytes) {
    const auto data = boost::asio::buffer_cast<const char*>(m_buffer.data());
    const std::string input{data, bytes - 1};
    m_buffer.consume(bytes);
    return input;
}

void Session::write(const std::string& output) {
    const auto self = shared_from_this();

    std::ostream os(&m_buffer);
    // Include CR (so that Windows' telnet client works)
    os << output << "\r\n";

    boost::asio::async_write(m_socket, m_buffer, boost::asio::bind_executor(m_strand,
        [this, self] (const boost::system::error_code& ec, std::size_t bytes) {
            handle_write(ec, bytes);
        }));
}

void Session::handle_write(const boost::system::error_code& ec, std::size_t bytes) {
    if (ec) {
        log::error("%1%: %2%", __func__, ec.message());
        m_session_mgr.stop(shared_from_this());
        return;
    }

    read();
}

}