| cpp_redis 4.0.0 cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.

subscriber.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23 #pragma once
24
25 #include <atomic>
26 #include <functional>
27 #include <map>
28 #include <mutex>
29 #include <string>
30
31 #include <cpp_redis/core/sentinel.hpp>
32 #include <cpp_redis/network/redis_connection.hpp>
33 #include <cpp_redis/network/tcp_client_iface.hpp>
34
35 namespace cpp_redis {
36
43 class subscriber {
44 public:
45 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
46 subscriber(void);
48 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
49
55 explicit subscriber(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
56
58 ~subscriber(void);
59
61 subscriber(const subscriber&) = delete;
63 subscriber& operator=(const subscriber&) = delete;
64
65 public:
76 enum class connect_state {
77 dropped,
78 start,
79 sleeping,
80 ok,
81 failed,
82 lookup_failed,
83 stopped
84 };
85
86 public:
90 typedef std::function<void(const std::string& host, std::size_t port, connect_state status)> connect_callback_t;
91
102 void connect(
103 const std::string& host = "127.0.0.1",
104 std::size_t port = 6379,
105 const connect_callback_t& connect_callback = nullptr,
106 std::uint32_t timeout_msecs = 0,
107 std::int32_t max_reconnects = 0,
108 std::uint32_t reconnect_interval_msecs = 0);
109
119 void connect(
120 const std::string& name,
121 const connect_callback_t& connect_callback = nullptr,
122 std::uint32_t timeout_msecs = 0,
123 std::int32_t max_reconnects = 0,
124 std::uint32_t reconnect_interval_msecs = 0);
125
129 bool is_connected(void) const;
130
136 void disconnect(bool wait_for_removal = false);
137
141 bool is_reconnecting(void) const;
142
146 void cancel_reconnect(void);
147
148 public:
153 typedef std::function<void(reply&)> reply_callback_t;
154
164 subscriber& auth(const std::string& password, const reply_callback_t& reply_callback = nullptr);
165
170 typedef std::function<void(const std::string&, const std::string&)> subscribe_callback_t;
171
176 typedef std::function<void(int64_t)> acknowledgement_callback_t;
177
189 subscriber& subscribe(const std::string& channel, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback = nullptr);
190
202 subscriber& psubscribe(const std::string& pattern, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback = nullptr);
203
211 subscriber& unsubscribe(const std::string& channel);
212
220 subscriber& punsubscribe(const std::string& pattern);
221
228 subscriber& commit(void);
229
230 public:
238 void add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
239
245 const sentinel& get_sentinel(void) const;
246
253 sentinel& get_sentinel(void);
254
258 void clear_sentinels(void);
259
260 private:
264 struct callback_holder {
265 subscribe_callback_t subscribe_callback;
266 acknowledgement_callback_t acknowledgement_callback;
267 };
268
269 private:
276 void connection_receive_handler(network::redis_connection& connection, reply& reply);
277
283 void connection_disconnection_handler(network::redis_connection& connection);
284
291 void handle_acknowledgement_reply(const std::vector<reply>& reply);
292
299 void handle_subscribe_reply(const std::vector<reply>& reply);
300
307 void handle_psubscribe_reply(const std::vector<reply>& reply);
308
317 void call_acknowledgement_callback(const std::string& channel, const std::map<std::string, callback_holder>& channels, std::mutex& channels_mtx, int64_t nb_chans);
318
319 private:
324 void reconnect(void);
325
329 void re_auth(void);
330
334 void re_subscribe(void);
335
339 bool should_reconnect(void) const;
340
344 void sleep_before_next_reconnect_attempt(void);
345
349 void clear_subscriptions(void);
350
351 private:
360 void unprotected_subscribe(const std::string& channel, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback);
361
370 void unprotected_psubscribe(const std::string& pattern, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback);
371
372 private:
376 std::string m_redis_server;
380 std::size_t m_redis_port = 0;
384 std::string m_master_name;
388 std::string m_password;
389
393 network::redis_connection m_client;
394
398 cpp_redis::sentinel m_sentinel;
399
403 std::uint32_t m_connect_timeout_msecs = 0;
407 std::int32_t m_max_reconnects = 0;
411 std::int32_t m_current_reconnect_attempts = 0;
415 std::uint32_t m_reconnect_interval_msecs = 0;
416
420 std::atomic_bool m_reconnecting;
424 std::atomic_bool m_cancel;
425
429 std::map<std::string, callback_holder> m_subscribed_channels;
433 std::map<std::string, callback_holder> m_psubscribed_channels;
434
438 connect_callback_t m_connect_callback;
439
443 std::mutex m_psubscribed_channels_mutex;
447 std::mutex m_subscribed_channels_mutex;
448
452 reply_callback_t m_auth_reply_callback;
453 };
454
455 } // namespace cpp_redis
cpp_redis::subscriber::clear_sentinels
void clear_sentinels(void)
cpp_redis::network::redis_connection
Definition: redis_connection.hpp:45
cpp_redis::subscriber
Definition: subscriber.hpp:43
cpp_redis::subscriber::connect_callback_t
std::function< void(const std::string &host, std::size_t port, connect_state status)> connect_callback_t
Definition: subscriber.hpp:90
cpp_redis::subscriber::~subscriber
~subscriber(void)
dtor
cpp_redis::subscriber::psubscribe
subscriber & psubscribe(const std::string &pattern, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
cpp_redis::subscriber::commit
subscriber & commit(void)
cpp_redis::reply
Definition: reply.hpp:37
cpp_redis::subscriber::reply_callback_t
std::function< void(reply &)> reply_callback_t
Definition: subscriber.hpp:153
cpp_redis::subscriber::operator=
subscriber & operator=(const subscriber &)=delete
assignment operator
cpp_redis::subscriber::is_reconnecting
bool is_reconnecting(void) const
cpp_redis::subscriber::acknowledgement_callback_t
std::function< void(int64_t)> acknowledgement_callback_t
Definition: subscriber.hpp:176
cpp_redis::subscriber::connect_state
connect_state
Definition: subscriber.hpp:76
cpp_redis::subscriber::cancel_reconnect
void cancel_reconnect(void)
cpp_redis::subscriber::auth
subscriber & auth(const std::string &password, const reply_callback_t &reply_callback=nullptr)
cpp_redis::sentinel
Definition: sentinel.hpp:40
cpp_redis::subscriber::add_sentinel
void add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_msecs=0)
cpp_redis::subscriber::disconnect
void disconnect(bool wait_for_removal=false)
cpp_redis::subscriber::get_sentinel
const sentinel & get_sentinel(void) const
cpp_redis::subscriber::punsubscribe
subscriber & punsubscribe(const std::string &pattern)
cpp_redis::subscriber::unsubscribe
subscriber & unsubscribe(const std::string &channel)
cpp_redis::subscriber::connect
void connect(const std::string &host="127.0.0.1", std::size_t port=6379, const connect_callback_t &connect_callback=nullptr, std::uint32_t timeout_msecs=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_msecs=0)
cpp_redis::subscriber::subscribe_callback_t
std::function< void(const std::string &, const std::string &)> subscribe_callback_t
Definition: subscriber.hpp:170
cpp_redis::subscriber::subscriber
subscriber(void)
ctor
cpp_redis::subscriber::is_connected
bool is_connected(void) const
cpp_redis::subscriber::subscribe
subscriber & subscribe(const std::string &channel, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
cpp_redis
Definition: array_builder.hpp:29