aboutsummaryrefslogtreecommitdiff
path: root/src/server/connection.h
blob: eb28d519321c53043a05eafb1d501e70cd943c97 (plain)
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
// SPDX-License-Identifier: BSD-3-Clause

#ifndef INVERTER_TOOLS_CONNECTION_H
#define INVERTER_TOOLS_CONNECTION_H

#include <thread>
#include <netinet/in.h>
#include <sstream>

#include "server.h"
#include "../formatter/formatter.h"

namespace server {

class Server;
struct Response;

struct ConnectionOptions {
    ConnectionOptions()
        : version(1), format(formatter::Format::JSON)
    {}

    unsigned version;
    formatter::Format format;
};


class Connection {
private:
    int sock_;
    std::thread thread_;
    struct sockaddr_in addr_;
    Server* server_;
    ConnectionOptions options_;

public:
    explicit Connection(int sock, struct sockaddr_in addr, Server* server);
    ~Connection();
    void run();
    std::string ipv4() const;
    bool sendResponse(Response& resp) const;
    int readLoop(char* buf, size_t bufSize) const;
    bool writeLoop(const char* buf, size_t bufSize) const;
    Response processRequest(char* buf);
};


enum class RequestType {
    Version,
    Format,
    Execute,
    Raw,
};


enum class ResponseType {
    OK,
    Error
};


struct Response {
    ResponseType type;
    std::ostringstream buf;
};
std::ostream& operator<<(std::ostream& os, Response& resp);

}

#endif //INVERTER_TOOLS_CONNECTION_H