aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/client/transport.hpp
blob: dbb142f92a345a3ece73452f18d51f5b38feb669 (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
// 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 "error.hpp"

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

#include <functional>
#include <memory>
#include <string>

namespace math::client {
namespace transport {

class Error : public client::Error {
public:
    explicit Error(const std::string& msg) : client::Error{"transport error: " + msg} {}
};

} // namespace transport

class Transport {
public:
    virtual ~Transport() = default;

    using ProcessResult = std::function<void(const std::string&)>;

    virtual void send_query(const std::string&, const ProcessResult&) = 0;
};

using TransportPtr = std::unique_ptr<Transport>;

class NetworkTransport : public Transport {
public:
    static constexpr auto DEFAULT_PORT = "18000";

    NetworkTransport(const std::string& host, const std::string& port)
        : m_host{host}, m_port{port} {}

protected:
    const std::string m_host;
    const std::string m_port;
};

class BlockingNetworkTransport : public NetworkTransport {
public:
    BlockingNetworkTransport(const std::string& host, const std::string& port)
        : NetworkTransport{host, port}, m_socket{m_io_context} {
        try {
            connect();
        } catch (const boost::system::system_error& e) {
            throw transport::Error{e.what()};
        }
    }

    void send_query(const std::string& query, const ProcessResult& on_reply) override {
        std::string reply;
        try {
            reply = send_query(query);
        } catch (const boost::system::system_error& e) {
            throw transport::Error{e.what()};
        }
        on_reply(reply);
    }

private:
    void connect() {
        boost::asio::ip::tcp::resolver resolver{m_io_context};
        boost::asio::connect(m_socket, resolver.resolve(m_host, m_port));
    }

    std::string send_query(const std::string& query) {
        write(query);
        return read_line();
    }

    void write(std::string input) {
        input += '\n';
        boost::asio::write(m_socket, boost::asio::const_buffer{input.c_str(), input.size()});
    }

    std::string read_line() {
        const auto bytes = boost::asio::read_until(m_socket, m_buffer, "\r\n");
        const auto data = boost::asio::buffer_cast<const char*>(m_buffer.data());
        const std::string result{data, bytes - 2}; // Skip \r\n
        m_buffer.consume(bytes);
        return result;
    }

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

inline TransportPtr make_blocking_network_transport(const std::string& host,
                                                    const std::string& port) {
    return std::make_unique<BlockingNetworkTransport>(host, port);
}

} // namespace math::client