-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis_publisher.h
58 lines (43 loc) · 1.19 KB
/
redis_publisher.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
#ifndef REDIS_PUBLISHER_H
#define REDIS_PUBLISHER_H
#include <stdlib.h>
#include <hiredis/async.h>
#include <hiredis/adapters/libevent.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <iostream>
/**
* redis_publisher.h 封装了 hiredis,实现消息发布给 redis的功能
*/
class RedisPublisher {
public:
RedisPublisher();
~RedisPublisher();
bool init();
bool uninit();
bool connect();
bool disconnect();
bool publish(const std::string &channel_name, const std::string &message);
private:
// 下面三个回调函数供redis服务调用
// 连接回调
static void connect_callback(const redisAsyncContext *redis_context, int status);
// 断开连接的回调
static void disconnect_callback(const redisAsyncContext *redis_context, int status);
// 执行命令回调
static void command_callback(redisAsyncContext *redis_context, void *reply, void *privdata);
// 事件分发线程函数
static void *event_thread(void *data);
void *event_proc();
private:
// libevent事件对象
event_base *eventBase;
// 事件线程ID
pthread_t eventThread;
// 事件线程的信号量
sem_t eventSem;
// hiredis异步对象
redisAsyncContext *context;
};
#endif