forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathredis_connection.hpp
More file actions
230 lines (197 loc) · 5.95 KB
/
redis_connection.hpp
File metadata and controls
230 lines (197 loc) · 5.95 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// The MIT License (MIT)
//
// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <cpp_redis/builders/reply_builder.hpp>
#include <cpp_redis/network/tcp_client_iface.hpp>
#ifndef __CPP_REDIS_READ_SIZE
#define __CPP_REDIS_READ_SIZE 4096
#endif //! __CPP_REDIS_READ_SIZE //!
namespace cpp_redis {
namespace network {
//!
//! tcp connection wrapper handling redis protocol
//!
//!
class redis_connection {
public:
#ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
//!
//! ctor
//!
//!
redis_connection();
#endif //! __CPP_REDIS_USE_CUSTOM_TCP_CLIENT //!
//!
//! ctor allowing to specify custom tcp client (default ctor uses the default
//! tacopie tcp client)
//!
//! @param tcp_client tcp client to be used for network communications
//!
//!
explicit redis_connection(
const std::shared_ptr<tcp_client_iface> &tcp_client);
//!
//! dtor
//!
//!
~redis_connection();
//!
//! copy ctor
//!
//!
redis_connection(const redis_connection &) = delete;
//!
//! assignment operator
//!
//!
redis_connection &operator=(const redis_connection &) = delete;
public:
//!
//! disconnection handler takes as parameter the instance of the
//! redis_connection
//!
//!
using disconnection_handler_t = std::function<void(redis_connection &)>;
//!
//! reply handler takes as parameter the instance of the redis_connection and
//! the built reply
//!
//!
using reply_callback_t = std::function<void(redis_connection &, reply &)>;
//!
//! connect to the given host and port, and set both disconnection and reply
//! callbacks
//!
//! @param host host to be connected to
//! @param port port to be connected to
//! @param disconnection_handler handler to be called in case of
//! disconnection
//! @param reply_callback handler to be called once a reply is ready
//! @param timeout_ms max time to connect (in ms)
//!
//!
void connect(const string_t &host = "127.0.0.1", std::size_t port = 6379,
const disconnection_handler_t &disconnection_handler = nullptr,
const reply_callback_t &reply_callback = nullptr,
std::uint32_t timeout_ms = 0);
//!
//! disconnect from redis server
//!
//! @param wait_for_removal when sets to true, disconnect blocks until the
//! underlying TCP client has been effectively removed from the io_service
//! and that all the underlying callbacks have completed.
//!
//!
void disconnect(bool wait_for_removal = false);
//!
//! @return whether we are connected to the redis server or not
//!
//!
bool is_connected() const;
//!
//! send the given command
//! the command is actually pipelined and only buffered, so nothing is sent
//! to the network please call commit() to flush the buffer
//!
//! @param redis_cmd command to be sent
//! @return current instance
//!
//!
redis_connection &send(const std::vector<string_t> &redis_cmd);
//!
//! commit pipelined transaction
//! that is, send to the network all commands pipelined by calling send()
//!
//! @return current instance
//!
//!
redis_connection &commit();
private:
//!
//! tcp_client receive handler
//! called by the tcp_client whenever a read has completed
//!
//! @param result read result
//!
//!
void tcp_client_receive_handler(const tcp_client_iface::read_result &result);
//!
//! tcp_client disconnection handler
//! called by the tcp_client whenever a disconnection occurred
//!
//!
void tcp_client_disconnection_handler();
//!
//! transform a user command to a redis command using the redis protocol
//! format for example, transform {"GET", "HELLO"} to something like
//! "*2\r\n+GET\r\n+HELLO\r\n"
//!
//!
string_t build_command(const std::vector<string_t> &redis_cmd);
private:
//!
//! simply call the disconnection handler (does nothing if disconnection
//! handler is set to null)
//!
//!
void call_disconnection_handler();
private:
//!
//! tcp client for redis connection
//!
//!
std::shared_ptr<cpp_redis::network::tcp_client_iface> m_client;
//!
//! reply callback called whenever a reply has been read
//!
//!
reply_callback_t m_reply_callback;
//!
//! disconnection handler whenever a disconnection occurred
//!
//!
disconnection_handler_t m_disconnection_handler;
//!
//! reply builder used to build replies
//!
//!
builders::reply_builder m_builder;
//!
//! internal buffer used for pipelining (commands are buffered here and
//! flushed to the tcp client when commit is called)
//!
//!
string_t m_buffer;
//!
//! protect internal buffer against race conditions
//!
//!
std::mutex m_buffer_mutex;
};
} // namespace network
using redis_connection_t = network::redis_connection;
} // namespace cpp_redis