#include <subscriber.hpp>
|
enum | connect_state {
dropped,
start,
sleeping,
ok,
failed,
lookup_failed,
stopped
} |
|
typedef std::function< void(const std::string &host, std::size_t port, connect_state status)> | connect_callback_t |
| connect handler, called whenever a new connection even occurred
|
|
typedef std::function< void(reply &)> | reply_callback_t |
| reply callback called whenever a reply is received, takes as parameter the received reply
|
|
typedef std::function< void(const std::string &, const std::string &)> | read_callback_t |
|
typedef std::function< void(int64_t)> | acknowledgement_callback_t |
|
|
| subscriber () |
| ctor
|
|
| subscriber (const std::shared_ptr< network::tcp_client_iface > &tcp_client) |
|
| ~subscriber () |
| dtor
|
|
| subscriber (const subscriber &)=delete |
| copy ctor
|
|
subscriber & | operator= (const subscriber &)=delete |
| assignment operator
|
|
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_ms=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_ms=0) |
| Connect to redis server. More...
|
|
void | connect (const std::string &m_name, const connect_callback_t &connect_callback=nullptr, std::uint32_t timeout_ms=0, std::int32_t max_reconnects=0, std::uint32_t reconnect_interval_ms=0) |
| Connect to redis server. More...
|
|
bool | is_connected () const |
| determines client connectivity More...
|
|
void | disconnect (bool wait_for_removal=false) |
| disconnect from redis server More...
|
|
bool | is_reconnecting () const |
| determines if reconnect is in progress More...
|
|
void | cancel_reconnect () |
| stop any reconnect in progress
|
|
subscriber & | auth (const std::string &password, const reply_callback_t &reply_callback=nullptr) |
| ability to authenticate on the redis server if necessary this method should not be called repeatedly as the storage of reply_callback is NOT thread safe (only one reply callback is stored for the subscriber client) calling repeatedly auth() is undefined concerning the execution of the associated callbacks More...
|
|
subscriber & | subscribe (const std::string &channel, const read_callback_t &callback, const acknowledgement_callback_t &acknowledgement_callback=nullptr) |
|
subscriber & | psubscribe (const std::string &pattern, const read_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 | add_sentinel (const std::string &host, std::size_t port, std::uint32_t timeout_ms=0) |
|
const sentinel & | get_sentinel () const |
|
sentinel & | get_sentinel () |
|
void | clear_sentinels () |
|
The cpp_redis::subscriber is meant to be used for PUB/SUB communication with the Redis server. Please do not use cpp_redis::client to subscribe to some Redis channels as:
◆ acknowledgement_callback_t
acknowledgment 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)
◆ read_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: attempt of connection has started
- sleeping: sleep between two attempts
- ok: connected
- failed: failed to connect
- lookup failed: failed to retrieve master sentinel
- stopped: stop to try to reconnect
◆ subscriber()
custom ctor to specify custom tcp_client
- Parameters
-
tcp_client | tcp client to be used for network communications |
◆ add_sentinel()
void cpp_redis::subscriber::add_sentinel |
( |
const std::string & |
host, |
|
|
std::size_t |
port, |
|
|
std::uint32_t |
timeout_ms = 0 |
|
) |
| |
add a sentinel definition. Required for connect() or get_master_addr_by_name() when autoconnect is enabled.
- Parameters
-
host | sentinel host |
port | sentinel port |
timeout_ms | maximum time to connect |
◆ 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 thread safe (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
◆ clear_sentinels()
void cpp_redis::subscriber::clear_sentinels |
( |
| ) |
|
clear all existing sentinels.
◆ commit()
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_ms = 0 , |
|
|
std::int32_t |
max_reconnects = 0 , |
|
|
std::uint32_t |
reconnect_interval_ms = 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_ms | maximum time to connect |
max_reconnects | maximum attempts of reconnection if connection dropped |
reconnect_interval_ms | time between two attempts of reconnection |
◆ connect() [2/2]
void cpp_redis::subscriber::connect |
( |
const std::string & |
m_name, |
|
|
const connect_callback_t & |
connect_callback = nullptr , |
|
|
std::uint32_t |
timeout_ms = 0 , |
|
|
std::int32_t |
max_reconnects = 0 , |
|
|
std::uint32_t |
reconnect_interval_ms = 0 |
|
) |
| |
Connect to redis server.
- Parameters
-
m_name | sentinel m_name |
connect_callback | connect handler to be called on connect events (may be null) |
timeout_ms | maximum time to connect |
max_reconnects | maximum attempts of reconnection if connection dropped |
reconnect_interval_ms | time between two attempts of reconnection |
◆ disconnect()
void cpp_redis::subscriber::disconnect |
( |
bool |
wait_for_removal = false | ) |
|
disconnect from redis server
- Parameters
-
wait_for_removal | when set 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 |
( |
| ) |
const |
retrieve sentinel for current client
- Returns
- sentinel associated to current client
◆ get_sentinel() [2/2]
sentinel& cpp_redis::subscriber::get_sentinel |
( |
| ) |
|
retrieve sentinel for current client non-const version
- Returns
- sentinel associated to current client
◆ is_connected()
bool cpp_redis::subscriber::is_connected |
( |
| ) |
const |
determines client connectivity
- Returns
- whether we are connected to the redis server
◆ is_reconnecting()
bool cpp_redis::subscriber::is_reconnecting |
( |
| ) |
const |
determines if reconnect is in progress
- Returns
- whether an attempt to reconnect is in progress
◆ psubscribe()
PSubscribes to the given channel and:
- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls read_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
◆ subscribe()
Subscribes to the given channel and:
- calls acknowledgement_callback once the server has acknowledged about the subscription.
- calls read_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
◆ 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
The documentation for this class was generated from the following file: