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 <functional>
26 #include <map>
27 #include <mutex>
28 #include <string>
29 
30 #include <cpp_redis/core/sentinel.hpp>
31 #include <cpp_redis/network/redis_connection.hpp>
32 #include <cpp_redis/network/tcp_client_iface.hpp>
33 
34 namespace cpp_redis {
35 
42 class subscriber {
43 public:
44 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
45  subscriber(void);
47 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
48 
54  explicit subscriber(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
55 
57  ~subscriber(void);
58 
60  subscriber(const subscriber&) = delete;
62  subscriber& operator=(const subscriber&) = delete;
63 
64 public:
75  enum class connect_state {
76  dropped,
77  start,
78  sleeping,
79  ok,
80  failed,
81  lookup_failed,
82  stopped
83  };
84 
85 public:
89  typedef std::function<void(const std::string& host, std::size_t port, connect_state status)> connect_callback_t;
90 
101  void connect(
102  const std::string& host = "127.0.0.1",
103  std::size_t port = 6379,
104  const connect_callback_t& connect_callback = nullptr,
105  std::uint32_t timeout_msecs = 0,
106  std::int32_t max_reconnects = 0,
107  std::uint32_t reconnect_interval_msecs = 0);
108 
118  void connect(
119  const std::string& name,
120  const connect_callback_t& connect_callback = nullptr,
121  std::uint32_t timeout_msecs = 0,
122  std::int32_t max_reconnects = 0,
123  std::uint32_t reconnect_interval_msecs = 0);
124 
128  bool is_connected(void) const;
129 
135  void disconnect(bool wait_for_removal = false);
136 
140  bool is_reconnecting(void) const;
141 
145  void cancel_reconnect(void);
146 
147 public:
152  typedef std::function<void(reply&)> reply_callback_t;
153 
163  subscriber& auth(const std::string& password, const reply_callback_t& reply_callback = nullptr);
164 
169  typedef std::function<void(const std::string&, const std::string&)> subscribe_callback_t;
170 
175  typedef std::function<void(int64_t)> acknowledgement_callback_t;
176 
188  subscriber& subscribe(const std::string& channel, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback = nullptr);
189 
201  subscriber& psubscribe(const std::string& pattern, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback = nullptr);
202 
210  subscriber& unsubscribe(const std::string& channel);
211 
219  subscriber& punsubscribe(const std::string& pattern);
220 
227  subscriber& commit(void);
228 
229 public:
237  void add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
238 
244  const sentinel& get_sentinel(void) const;
245 
252  sentinel& get_sentinel(void);
253 
257  void clear_sentinels(void);
258 
259 private:
263  struct callback_holder {
264  subscribe_callback_t subscribe_callback;
265  acknowledgement_callback_t acknowledgement_callback;
266  };
267 
268 private:
275  void connection_receive_handler(network::redis_connection& connection, reply& reply);
276 
282  void connection_disconnection_handler(network::redis_connection& connection);
283 
290  void handle_acknowledgement_reply(const std::vector<reply>& reply);
291 
298  void handle_subscribe_reply(const std::vector<reply>& reply);
299 
306  void handle_psubscribe_reply(const std::vector<reply>& reply);
307 
316  void call_acknowledgement_callback(const std::string& channel, const std::map<std::string, callback_holder>& channels, std::mutex& channels_mtx, int64_t nb_chans);
317 
318 private:
323  void reconnect(void);
324 
328  void re_auth(void);
329 
333  void re_subscribe(void);
334 
338  bool should_reconnect(void) const;
339 
343  void sleep_before_next_reconnect_attempt(void);
344 
348  void clear_subscriptions(void);
349 
350 private:
359  void unprotected_subscribe(const std::string& channel, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback);
360 
369  void unprotected_psubscribe(const std::string& pattern, const subscribe_callback_t& callback, const acknowledgement_callback_t& acknowledgement_callback);
370 
371 private:
375  std::string m_redis_server;
379  std::size_t m_redis_port = 0;
383  std::string m_master_name;
387  std::string m_password;
388 
392  network::redis_connection m_client;
393 
397  cpp_redis::sentinel m_sentinel;
398 
402  std::uint32_t m_connect_timeout_msecs = 0;
406  std::int32_t m_max_reconnects = 0;
410  std::int32_t m_current_reconnect_attempts = 0;
414  std::uint32_t m_reconnect_interval_msecs = 0;
415 
419  std::atomic_bool m_reconnecting;
423  std::atomic_bool m_cancel;
424 
428  std::map<std::string, callback_holder> m_subscribed_channels;
432  std::map<std::string, callback_holder> m_psubscribed_channels;
433 
437  connect_callback_t m_connect_callback;
438 
442  std::mutex m_psubscribed_channels_mutex;
446  std::mutex m_subscribed_channels_mutex;
447 
451  reply_callback_t m_auth_reply_callback;
452 };
453 
454 } // namespace cpp_redis
void clear_sentinels(void)
Definition: redis_connection.hpp:45
Definition: subscriber.hpp:42
std::function< void(const std::string &host, std::size_t port, connect_state status)> connect_callback_t
Definition: subscriber.hpp:89
subscriber & psubscribe(const std::string &pattern, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
subscriber & commit(void)
Definition: reply.hpp:37
std::function< void(reply &)> reply_callback_t
Definition: subscriber.hpp:152
subscriber & operator=(const subscriber &)=delete
assignment operator
bool is_reconnecting(void) const
std::function< void(int64_t)> acknowledgement_callback_t
Definition: subscriber.hpp:175
connect_state
Definition: subscriber.hpp:75
void cancel_reconnect(void)
subscriber & auth(const std::string &password, const reply_callback_t &reply_callback=nullptr)
Definition: sentinel.hpp:39
void add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_msecs=0)
void disconnect(bool wait_for_removal=false)
const sentinel & get_sentinel(void) const
subscriber & punsubscribe(const std::string &pattern)
subscriber & unsubscribe(const std::string &channel)
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)
std::function< void(const std::string &, const std::string &)> subscribe_callback_t
Definition: subscriber.hpp:169
bool is_connected(void) const
subscriber & subscribe(const std::string &channel, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr)
Definition: array_builder.hpp:29