/
redisclient.h
642 lines (439 loc) · 14.4 KB
/
redisclient.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISCLIENT_VERSION_H
#define REDIS_CLIENT_VERSION 502 // 0.5.1
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISSYNCCLIENT_REDISBUFFER_H
#include <boost/variant.hpp>
#include <string>
#include <vector>
namespace redisclient {
struct RedisBuffer
{
RedisBuffer() = default;
inline RedisBuffer(const char *ptr, size_t dataSize);
inline RedisBuffer(const char *s);
inline RedisBuffer(std::string s);
inline RedisBuffer(std::vector<char> buf);
inline size_t size() const;
boost::variant<std::string,std::vector<char>> data;
};
RedisBuffer::RedisBuffer(const char *ptr, size_t dataSize)
: data(std::vector<char>(ptr, ptr + dataSize))
{
}
RedisBuffer::RedisBuffer(const char *s)
: data(std::string(s))
{
}
RedisBuffer::RedisBuffer(std::string s)
: data(std::move(s))
{
}
RedisBuffer::RedisBuffer(std::vector<char> buf)
: data(std::move(buf))
{
}
size_t RedisBuffer::size() const
{
if (data.type() == typeid(std::string))
return boost::get<std::string>(data).size();
else
return boost::get<std::vector<char>>(data).size();
}
}
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISCLIENT_REDISVALUE_H
#include <boost/variant.hpp>
#include <string>
#include <vector>
namespace redisclient {
class RedisValue {
public:
struct ErrorTag {};
RedisValue();
RedisValue(RedisValue &&other);
RedisValue(int64_t i);
RedisValue(const char *s);
RedisValue(const std::string &s);
RedisValue(std::vector<char> buf);
RedisValue(std::vector<char> buf, struct ErrorTag);
RedisValue(std::vector<RedisValue> array);
RedisValue(const RedisValue &) = default;
RedisValue& operator = (const RedisValue &) = default;
RedisValue& operator = (RedisValue &&) = default;
// Return the value as a std::string if
// type is a byte string; otherwise returns an empty std::string.
std::string toString() const;
// Return the value as a std::vector<char> if
// type is a byte string; otherwise returns an empty std::vector<char>.
std::vector<char> toByteArray() const;
// Return the value as a std::vector<RedisValue> if
// type is an int; otherwise returns 0.
int64_t toInt() const;
// Return the value as an array if type is an array;
// otherwise returns an empty array.
std::vector<RedisValue> toArray() const;
// Return the string representation of the value. Use
// for dump content of the value.
std::string inspect() const;
// Return true if value not a error
bool isOk() const;
// Return true if value is a error
bool isError() const;
// Return true if this is a null.
bool isNull() const;
// Return true if type is an int
bool isInt() const;
// Return true if type is an array
bool isArray() const;
// Return true if type is a string/byte array. Alias for isString();
bool isByteArray() const;
// Return true if type is a string/byte array. Alias for isByteArray().
bool isString() const;
bool operator == (const RedisValue &rhs) const;
bool operator != (const RedisValue &rhs) const;
protected:
template<typename T>
T castTo() const;
template<typename T>
bool typeEq() const;
private:
struct NullTag {
inline bool operator == (const NullTag &) const {
return true;
}
};
boost::variant<NullTag, int64_t, std::vector<char>, std::vector<RedisValue> > value;
bool error;
};
template<typename T>
T RedisValue::castTo() const
{
if( value.type() == typeid(T) )
return boost::get<T>(value);
else
return T();
}
template<typename T>
bool RedisValue::typeEq() const
{
if( value.type() == typeid(T) )
return true;
else
return false;
}
}
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISCLIENT_REDISPARSER_H
#include <stack>
#include <vector>
#include <utility>
namespace redisclient {
class RedisParser
{
public:
RedisParser();
enum ParseResult {
Completed,
Incompleted,
Error,
};
std::pair<size_t, ParseResult> parse(const char *ptr, size_t size);
RedisValue result();
protected:
std::pair<size_t, ParseResult> parseChunk(const char *ptr, size_t size);
std::pair<size_t, ParseResult> parseArray(const char *ptr, size_t size);
inline bool isChar(int c)
{
return c >= 0 && c <= 127;
}
inline bool isControl(int c)
{
return (c >= 0 && c <= 31) || (c == 127);
}
long int bufToLong(const char *str, size_t size);
private:
enum State {
Start = 0,
String = 1,
StringLF = 2,
ErrorString = 3,
ErrorLF = 4,
Integer = 5,
IntegerLF = 6,
BulkSize = 7,
BulkSizeLF = 8,
Bulk = 9,
BulkCR = 10,
BulkLF = 11,
ArraySize = 12,
ArraySizeLF = 13,
} state;
long int bulkSize;
std::vector<char> buf;
std::stack<long int> arrayStack;
std::stack<RedisValue> valueStack;
static const char stringReply = '+';
static const char errorReply = '-';
static const char integerReply = ':';
static const char bulkReply = '$';
static const char arrayReply = '*';
};
}
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISCLIENT_REDISCLIENTIMPL_H
#include <boost/array.hpp>
#include <boost/noncopyable.hpp>
#include <boost/asio/generic/stream_protocol.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <boost/asio/strand.hpp>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <functional>
#include <memory>
namespace redisclient {
class RedisClientImpl : public std::enable_shared_from_this<RedisClientImpl> {
public:
enum class State {
Unconnected,
Connecting,
Connected,
Subscribed,
Closed
};
RedisClientImpl(boost::asio::io_service &ioService);
~RedisClientImpl();
void handleAsyncConnect(
const boost::system::error_code &ec,
const std::function<void(bool, const std::string &)> &handler);
size_t subscribe(const std::string &command,
const std::string &channel,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler);
void singleShotSubscribe(const std::string &command,
const std::string &channel,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler);
void unsubscribe(const std::string &command,
size_t handle_id, const std::string &channel,
std::function<void(RedisValue)> handler);
void close() noexcept;
State getState() const;
static std::vector<char> makeCommand(const std::deque<RedisBuffer> &items);
RedisValue doSyncCommand(const std::deque<RedisBuffer> &buff);
void doAsyncCommand(
std::vector<char> buff,
std::function<void(RedisValue)> handler);
void sendNextCommand();
void processMessage();
void doProcessMessage(RedisValue v);
void asyncWrite(const boost::system::error_code &ec, const size_t);
void asyncRead(const boost::system::error_code &ec, const size_t);
void onRedisError(const RedisValue &);
static void defaulErrorHandler(const std::string &s);
template<typename Handler>
inline void post(const Handler &handler);
boost::asio::strand strand;
boost::asio::generic::stream_protocol::socket socket;
RedisParser redisParser;
boost::array<char, 4096> buf;
size_t subscribeSeq;
typedef std::pair<size_t, std::function<void(const std::vector<char> &buf)> > MsgHandlerType;
typedef std::function<void(const std::vector<char> &buf)> SingleShotHandlerType;
typedef std::multimap<std::string, MsgHandlerType> MsgHandlersMap;
typedef std::multimap<std::string, SingleShotHandlerType> SingleShotHandlersMap;
std::queue<std::function<void(RedisValue)> > handlers;
std::deque<std::vector<char>> dataWrited;
std::deque<std::vector<char>> dataQueued;
MsgHandlersMap msgHandlers;
SingleShotHandlersMap singleShotMsgHandlers;
std::function<void(const std::string &)> errorHandler;
State state;
};
template<typename Handler>
inline void RedisClientImpl::post(const Handler &handler)
{
strand.post(handler);
}
inline std::string to_string(RedisClientImpl::State state)
{
switch(state)
{
case RedisClientImpl::State::Unconnected:
return "Unconnected";
break;
case RedisClientImpl::State::Connecting:
return "Connecting";
break;
case RedisClientImpl::State::Connected:
return "Connected";
break;
case RedisClientImpl::State::Subscribed:
return "Subscribed";
break;
case RedisClientImpl::State::Closed:
return "Closed";
break;
}
return "Invalid";
}
}
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISASYNCCLIENT_REDISCLIENT_H
#include <boost/asio/io_service.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <list>
#include <type_traits>
#include <functional>
namespace redisclient {
class RedisClientImpl;
class RedisAsyncClient : boost::noncopyable {
public:
// Subscribe handle.
struct Handle {
size_t id;
std::string channel;
};
typedef RedisClientImpl::State State;
RedisAsyncClient(boost::asio::io_service &ioService);
~RedisAsyncClient();
// Connect to redis server
void connect(
const boost::asio::ip::address &address,
unsigned short port,
std::function<void(bool, const std::string &)> handler);
// Connect to redis server
void connect(
const boost::asio::ip::tcp::endpoint &endpoint,
std::function<void(bool, const std::string &)> handler);
void connect(
const boost::asio::local::stream_protocol::endpoint &endpoint,
std::function<void(bool, const std::string &)> handler);
// backward compatibility
inline void asyncConnect(
const boost::asio::ip::address &address,
unsigned short port,
std::function<void(bool, const std::string &)> handler)
{
connect(address, port, std::move(handler));
}
// backward compatibility
inline void asyncConnect(
const boost::asio::ip::tcp::endpoint &endpoint,
std::function<void(bool, const std::string &)> handler)
{
connect(endpoint, handler);
}
// Return true if is connected to redis.
// Deprecated: use state() == RedisAsyncClisend::State::Connected.
bool isConnected() const;
// Return connection state. See RedisClientImpl::State.
State state() const;
// disconnect from redis and clear command queue
void disconnect();
// Set custom error handler.
void installErrorHandler(
std::function<void(const std::string &)> handler);
// Execute command on Redis server with the list of arguments.
void command(
const std::string &cmd, std::deque<RedisBuffer> args,
std::function<void(RedisValue)> handler = dummyHandler);
// Subscribe to channel. Handler msgHandler will be called
// when someone publish message on channel. Call unsubscribe
// to stop the subscription.
Handle subscribe(const std::string &channelName,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler = &dummyHandler);
Handle psubscribe(const std::string &pattern,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler = &dummyHandler);
// Unsubscribe
void unsubscribe(const Handle &handle);
void punsubscribe(const Handle &handle);
// Subscribe to channel. Handler msgHandler will be called
// when someone publish message on channel; it will be
// unsubscribed after call.
void singleShotSubscribe(
const std::string &channel,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler = &dummyHandler);
void singleShotPSubscribe(
const std::string &channel,
std::function<void(std::vector<char> msg)> msgHandler,
std::function<void(RedisValue)> handler = &dummyHandler);
// Publish message on channel.
void publish(
const std::string &channel, const RedisBuffer &msg,
std::function<void(RedisValue)> handler = &dummyHandler);
static void dummyHandler(RedisValue) {}
protected:
bool stateValid() const;
private:
std::shared_ptr<RedisClientImpl> pimpl;
};
}
/*
* Copyright (C) Alex Nekipelov (alex@nekipelov.net)
* License: MIT
*/
#define REDISSYNCCLIENT_REDISCLIENT_H
#include <boost/asio/io_service.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <list>
#include <functional>
namespace redisclient {
class RedisClientImpl;
class RedisSyncClient : boost::noncopyable {
public:
typedef RedisClientImpl::State State;
RedisSyncClient(boost::asio::io_service &ioService);
~RedisSyncClient();
// Connect to redis server
bool connect(
const boost::asio::ip::tcp::endpoint &endpoint,
std::string &errmsg);
// Connect to redis server
bool connect(
const boost::asio::ip::address &address,
unsigned short port,
std::string &errmsg);
bool connect(
const boost::asio::local::stream_protocol::endpoint &endpoint,
std::string &errmsg);
// Set custom error handler.
void installErrorHandler(
std::function<void(const std::string &)> handler);
// Execute command on Redis server with the list of arguments.
RedisValue command(
const std::string &cmd, std::deque<RedisBuffer> args);
// Return connection state. See RedisClientImpl::State.
State state() const;
protected:
bool stateValid() const;
private:
std::shared_ptr<RedisClientImpl> pimpl;
};
}