#include <subscriber.hpp>
|
| subscriber (void) |
| ctor More...
|
|
| subscriber (const std::shared_ptr< network::tcp_client_iface > &tcp_client) |
|
| ~subscriber (void) |
| dtor More...
|
|
| subscriber (const subscriber &)=delete |
| copy ctor More...
|
|
subscriber & | operator= (const subscriber &)=delete |
| assignment operator More...
|
|
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) |
|
void | connect (const std::string &name, 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) |
|
bool | is_connected (void) const |
|
void | disconnect (bool wait_for_removal=false) |
|
bool | is_reconnecting (void) const |
|
void | cancel_reconnect (void) |
|
subscriber & | auth (const std::string &password, const reply_callback_t &reply_callback=nullptr) |
|
subscriber & | subscribe (const std::string &channel, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr) |
|
subscriber & | psubscribe (const std::string &pattern, const subscribe_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr) |
|
subscriber & | unsubscribe (const std::string &channel) |
|
subscriber & | punsubscribe (const std::string &pattern) |
|
subscriber & | commit (void) |
|
void | add_sentinel (const std::string &host, std::size_t port) |
|
const sentinel & | get_sentinel (void) const |
|
sentinel & | get_sentinel (void) |
|
void | clear_sentinels (void) |
|
◆ acknowledgement_callback_t
acknowledgement callback called whenever a subscribe completes takes as parameter the int returned by the redis server (usually the number of channels you are subscribed to)
◆ connect_callback_t
connect handler, called whenever a new connection even occurred
◆ reply_callback_t
reply callback called whenever a reply is received takes as parameter the received reply
◆ subscribe_callback_t
subscribe callback, called whenever a new message is published on a subscribed channel takes as parameter the channel and the message
◆ connect_state
high availability (re)connection states
- dropped: connection has dropped
- start: attemp of connection has started
- sleeping: sleep between two attemps
- ok: connected
- failed: failed to connect
- lookup failed: failed to retrieve master sentinel
- stopped: stop to try to reconnect
Enumerator |
---|
dropped | |
start | |
sleeping | |
ok | |
failed | |
lookup_failed | |
stopped | |
◆ subscriber() [1/3]
cpp_redis::subscriber::subscriber |
( |
void |
| ) |
|
◆ subscriber() [2/3]
custom ctor to specify custom tcp_client
- Parameters
-
tcp_client | tcp client to be used for network communications |
◆ ~subscriber()
cpp_redis::subscriber::~subscriber |
( |
void |
| ) |
|
◆ subscriber() [3/3]
cpp_redis::subscriber::subscriber |
( |
const subscriber & |
| ) |
|
|
delete |
◆ add_sentinel()
void cpp_redis::subscriber::add_sentinel |
( |
const std::string & |
host, |
|
|
std::size_t |
port |
|
) |
| |
add a sentinel definition. Required for connect() or get_master_addr_by_name() when autoconnect is enabled.
- Parameters
-
host | sentinel host |
port | sentinel port |
◆ auth()
ability to authenticate on the redis server if necessary this method should not be called repeatedly as the storage of reply_callback is NOT threadsafe (only one reply callback is stored for the subscriber client) calling repeatedly auth() is undefined concerning the execution of the associated callbacks
- Parameters
-
password | password to be used for authentication |
reply_callback | callback to be called on auth completion (nullable) |
- Returns
- current instance
◆ call_acknowledgement_callback()
void cpp_redis::subscriber::call_acknowledgement_callback |
( |
const std::string & |
channel, |
|
|
const std::map< std::string, callback_holder > & |
channels, |
|
|
std::mutex & |
channels_mtx, |
|
|
int64_t |
nb_chans |
|
) |
| |
|
private |
find channel or pattern that is associated to the reply and call its ack callback
- Parameters
-
channel | channel or pattern that caused the issuance of this reply |
channels | list of channels or patterns to be searched for the received channel |
channels_mtx | channels or patterns mtx to be locked for race condition |
nb_chans | redis server ack reply |
◆ cancel_reconnect()
void cpp_redis::subscriber::cancel_reconnect |
( |
void |
| ) |
|
stop any reconnect in progress
◆ clear_sentinels()
void cpp_redis::subscriber::clear_sentinels |
( |
void |
| ) |
|
clear all existing sentinels.
◆ clear_subscriptions()
void cpp_redis::subscriber::clear_subscriptions |
( |
void |
| ) |
|
|
private |
clear all subscriptions (dirty way, no unsub/punsub commands send: mostly used for cleaning in disconnection condition)
◆ commit()
subscriber& cpp_redis::subscriber::commit |
( |
void |
| ) |
|
commit pipelined transaction that is, send to the network all commands pipelined by calling send() / subscribe() / ...
- Returns
- current instance
◆ connect() [1/2]
void cpp_redis::subscriber::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 |
|
) |
| |
Connect to redis server
- Parameters
-
host | host to be connected to |
port | port to be connected to |
connect_callback | connect handler to be called on connect events (may be null) |
timeout_msecs | maximum time to connect |
max_reconnects | maximum attemps of reconnection if connection dropped |
reconnect_interval_msecs | time between two attemps of reconnection |
◆ connect() [2/2]
void cpp_redis::subscriber::connect |
( |
const std::string & |
name, |
|
|
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 |
|
) |
| |
Connect to redis server
- Parameters
-
name | sentinel name |
connect_callback | connect handler to be called on connect events (may be null) |
timeout_msecs | maximum time to connect |
max_reconnects | maximum attemps of reconnection if connection dropped |
reconnect_interval_msecs | time between two attemps of reconnection |
◆ connection_disconnection_handler()
redis_connection disconnection handler, triggered whenever a disconnection occured
- Parameters
-
connection | redis_connection instance |
◆ connection_receive_handler()
redis connection receive handler, triggered whenever a reply has been read by the redis connection
- Parameters
-
connection | redis_connection instance |
reply | parsed reply |
◆ disconnect()
void cpp_redis::subscriber::disconnect |
( |
bool |
wait_for_removal = false | ) |
|
disconnect from redis server
- Parameters
-
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. |
◆ get_sentinel() [1/2]
const sentinel& cpp_redis::subscriber::get_sentinel |
( |
void |
| ) |
const |
retrieve sentinel for current client
- Returns
- sentinel associated to current client
◆ get_sentinel() [2/2]
sentinel& cpp_redis::subscriber::get_sentinel |
( |
void |
| ) |
|
retrieve sentinel for current client non-const version
- Returns
- sentinel associated to current client
◆ handle_acknowledgement_reply()
void cpp_redis::subscriber::handle_acknowledgement_reply |
( |
const std::vector< reply > & |
reply | ) |
|
|
private |
trigger the ack callback for matching channel/pattern check if reply is valid
- Parameters
-
◆ handle_psubscribe_reply()
void cpp_redis::subscriber::handle_psubscribe_reply |
( |
const std::vector< reply > & |
reply | ) |
|
|
private |
trigger the sub callback for all matching channels/patterns check if reply is valid
- Parameters
-
◆ handle_subscribe_reply()
void cpp_redis::subscriber::handle_subscribe_reply |
( |
const std::vector< reply > & |
reply | ) |
|
|
private |
trigger the sub callback for all matching channels/patterns check if reply is valid
- Parameters
-
◆ is_connected()
bool cpp_redis::subscriber::is_connected |
( |
void |
| ) |
const |
- Returns
- whether we are connected to the redis server
◆ is_reconnecting()
bool cpp_redis::subscriber::is_reconnecting |
( |
void |
| ) |
const |
- Returns
- whether an attemp to reconnect is in progress
◆ operator=()
◆ psubscribe()
PSubscribes to the given channel and:
- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls subscribe_callback each time a message is published on this channel. The command is not effectively sent immediately but stored in an internal buffer until commit() is called.
- Parameters
-
pattern | pattern to psubscribe |
callback | callback to be called whenever a message is received for this pattern |
acknowledgement_callback | callback to be called on subscription completion (nullable) |
- Returns
- current instance
◆ punsubscribe()
subscriber& cpp_redis::subscriber::punsubscribe |
( |
const std::string & |
pattern | ) |
|
punsubscribe from the given pattern The command is not effectively sent immediately, but stored inside an internal buffer until commit() is called.
- Parameters
-
pattern | pattern to punsubscribe from |
- Returns
- current instance
◆ re_auth()
void cpp_redis::subscriber::re_auth |
( |
void |
| ) |
|
|
private |
re authenticate to redis server based on previously used password
◆ re_subscribe()
void cpp_redis::subscriber::re_subscribe |
( |
void |
| ) |
|
|
private |
resubscribe (sub and psub) to previously subscribed channels/patterns
◆ reconnect()
void cpp_redis::subscriber::reconnect |
( |
void |
| ) |
|
|
private |
reconnect to the previously connected host automatically re authenticate and resubscribe to subscribed channel in case of success
◆ should_reconnect()
bool cpp_redis::subscriber::should_reconnect |
( |
void |
| ) |
const |
|
private |
- Returns
- whether a reconnection attempt should be performed
◆ sleep_before_next_reconnect_attempt()
void cpp_redis::subscriber::sleep_before_next_reconnect_attempt |
( |
void |
| ) |
|
|
private |
sleep between two reconnect attemps if necessary
◆ subscribe()
Subscribes to the given channel and:
- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls subscribe_callback each time a message is published on this channel. The command is not effectively sent immediately but stored in an internal buffer until commit() is called.
- Parameters
-
channel | channel to subscribe |
callback | callback to be called whenever a message is received for this channel |
acknowledgement_callback | callback to be called on subscription completion (nullable) |
- Returns
- current instance
◆ unprotected_psubscribe()
unprotected psub same as psubscribe, but without any mutex lock
- Parameters
-
pattern | pattern to psubscribe |
callback | callback to be called whenever a message is received for this pattern |
acknowledgement_callback | callback to be called on subscription completion (nullable) |
◆ unprotected_subscribe()
unprotected sub same as subscribe, but without any mutex lock
- Parameters
-
channel | channel to subscribe |
callback | callback to be called whenever a message is received for this channel |
acknowledgement_callback | callback to be called on subscription completion (nullable) |
◆ unsubscribe()
subscriber& cpp_redis::subscriber::unsubscribe |
( |
const std::string & |
channel | ) |
|
unsubscribe from the given channel The command is not effectively sent immediately, but stored inside an internal buffer until commit() is called.
- Parameters
-
channel | channel to unsubscribe from |
- Returns
- current instance
◆ m_auth_reply_callback
◆ m_cancel
std::atomic_bool cpp_redis::subscriber::m_cancel = ATOMIC_VAR_INIT(false) |
|
private |
to force cancel reconnection
◆ m_client
tcp client for redis connection
◆ m_connect_callback
◆ m_connect_timeout_msecs
std::uint32_t cpp_redis::subscriber::m_connect_timeout_msecs = 0 |
|
private |
◆ m_current_reconnect_attempts
std::int32_t cpp_redis::subscriber::m_current_reconnect_attempts = 0 |
|
private |
current number of attemps to reconect
◆ m_master_name
std::string cpp_redis::subscriber::m_master_name |
|
private |
master name (if we are using sentinel) we are connected to
◆ m_max_reconnects
std::int32_t cpp_redis::subscriber::m_max_reconnects = 0 |
|
private |
max number of reconnection attemps
◆ m_password
std::string cpp_redis::subscriber::m_password |
|
private |
password used to authenticate
◆ m_psubscribed_channels
std::map<std::string, callback_holder> cpp_redis::subscriber::m_psubscribed_channels |
|
private |
psubscribed channels and their associated channels
◆ m_psubscribed_channels_mutex
std::mutex cpp_redis::subscriber::m_psubscribed_channels_mutex |
|
private |
◆ m_reconnect_interval_msecs
std::uint32_t cpp_redis::subscriber::m_reconnect_interval_msecs = 0 |
|
private |
time between two reconnection attemps
◆ m_reconnecting
std::atomic_bool cpp_redis::subscriber::m_reconnecting = ATOMIC_VAR_INIT(false) |
|
private |
◆ m_redis_port
std::size_t cpp_redis::subscriber::m_redis_port = 0 |
|
private |
◆ m_redis_server
std::string cpp_redis::subscriber::m_redis_server |
|
private |
server we are connected to
◆ m_sentinel
◆ m_subscribed_channels
std::map<std::string, callback_holder> cpp_redis::subscriber::m_subscribed_channels |
|
private |
subscribed channels and their associated channels
◆ m_subscribed_channels_mutex
std::mutex cpp_redis::subscriber::m_subscribed_channels_mutex |
|
private |
The documentation for this class was generated from the following file: