aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/main/session.hpp
blob: 2ab8f26b0ac8f747856a59fbbefdd72f5baf9cd1 (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
// 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.

#pragma once

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

#include <cstddef>
#include <memory>
#include <string>

namespace math::server {

class SessionManager;

class Session : public std::enable_shared_from_this<Session> {
public:
    Session(SessionManager& mgr, boost::asio::io_context& io_context);

    boost::asio::ip::tcp::socket& socket();

    void start();
    void stop();

private:
    void close();

    void read();
    void write(const std::string&);

    void handle_read(const boost::system::error_code&, std::size_t);
    void handle_write(const boost::system::error_code&, std::size_t);

    std::string consume_input(std::size_t);

    SessionManager& m_session_mgr;

    boost::asio::io_context::strand m_strand;
    boost::asio::ip::tcp::socket m_socket;
    boost::asio::streambuf m_buffer;
};

} // namespace math::server