summaryrefslogtreecommitdiff
path: root/ext/librethinkdbxx/src/connection_p.h
blob: d8a95e3c9def903e81f9a16225f9aaa9e5613177 (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef CONNECTION_P_H
#define CONNECTION_P_H

#include <inttypes.h>

#include "connection.h"
#include "term.h"
#include "json_p.h"

namespace RethinkDB {

extern const int debug_net;

struct Query {
    Protocol::Query::QueryType type;
    uint64_t token;
    Datum term;
    OptArgs optArgs;

    std::string serialize() {
        Array query_arr{static_cast<double>(type)};
        if (term.is_valid()) query_arr.emplace_back(term);
        if (!optArgs.empty())
            query_arr.emplace_back(Term(std::move(optArgs)).datum);

        std::string query_str = write_datum(query_arr);
        if (debug_net > 0) {
            fprintf(stderr, "[%" PRIu64 "] >> %s\n", token, query_str.c_str());
        }

        char header[12];
        memcpy(header, &token, 8);
        uint32_t size = query_str.size();
        memcpy(header + 8, &size, 4);
        query_str.insert(0, header, 12);
        return query_str;
    }
};

// Used internally to convert a raw response type into an enum
Protocol::Response::ResponseType response_type(double t);
Protocol::Response::ErrorType runtime_error_type(double t);

// Contains a response from the server. Use the Cursor class to interact with these responses
class Response {
public:
    Response() = delete;
    explicit Response(Datum&& datum) :
        type(response_type(std::move(datum).extract_field("t").extract_number())),
        error_type(datum.get_field("e") ?
                   runtime_error_type(std::move(datum).extract_field("e").extract_number()) :
                   Protocol::Response::ErrorType(0)),
        result(std::move(datum).extract_field("r").extract_array()) { }
    Error as_error();
    Protocol::Response::ResponseType type;
    Protocol::Response::ErrorType error_type;
    Array result;
};

class Token;
class ConnectionPrivate {
public:
    ConnectionPrivate(int sockfd)
        : guarded_next_token(1), guarded_sockfd(sockfd), guarded_loop_active(false)
    { }

    void run_query(Query query, bool no_reply = false);

    Response wait_for_response(uint64_t, double);
    uint64_t new_token() {
        return guarded_next_token++;
    }

    std::mutex read_lock;
    std::mutex write_lock;
    std::mutex cache_lock;

    struct TokenCache {
        bool closed = false;
        std::condition_variable cond;
        std::queue<Response> responses;
    };

    std::map<uint64_t, TokenCache> guarded_cache;
    uint64_t guarded_next_token;
    int guarded_sockfd;
    bool guarded_loop_active;
};

class CacheLock {
public:
    CacheLock(ConnectionPrivate* conn) : inner_lock(conn->cache_lock) { }

    void lock() {
        inner_lock.lock();
    }

    void unlock() {
        inner_lock.unlock();
    }

    std::unique_lock<std::mutex> inner_lock;
};

class ReadLock {
public:
    ReadLock(ConnectionPrivate* conn_) : lock(conn_->read_lock), conn(conn_) { }

    size_t recv_some(char*, size_t, double wait);
    void recv(char*, size_t, double wait);
    std::string recv(size_t);
    size_t recv_cstring(char*, size_t);

    Response read_loop(uint64_t, CacheLock&&, double);

    std::lock_guard<std::mutex> lock;
    ConnectionPrivate* conn;
};

class WriteLock {
public:
    WriteLock(ConnectionPrivate* conn_) : lock(conn_->write_lock), conn(conn_) { }

    void send(const char*, size_t);
    void send(std::string);

    std::lock_guard<std::mutex> lock;
    ConnectionPrivate* conn;
};

}   // namespace RethinkDB

#endif  // CONNECTION_P_H