- Notifications
You must be signed in to change notification settings - Fork209
Custom network module
tacopie is a C++11 TCP Client library used by default bycpp_redis to make it easy to test and develop a software communicating with a Redis server usingcpp_redis.
However,tacopie might not fulfill the needs of everyone: some people already use another networking library for other features or are looking for some feature not supported bytacopie.
To solve this issue,tacopie is not a mandatory dependency but is only provided as-is for convenience. The developer is free to use any other networking library by following the steps listed above.
Only asynchronous TCP clients are supported. No support for synchronous clients is planned.
First, you have to compile with theUSE_CUSTOM_TCP_CLIENT CMake variable.This CMake variable will makecpp_redis compile in a special configuration allowing you to use the library of your choice.
cpp_redis defines thetcp_client_iface interface incpp_redis/network/tcp_client_iface.hpp.
You must provide an implementation for this interface by creating your own class inheriting fromcpp_redis::network::tcp_client_iface and implementing the following methods:
virtualvoidconnect(const std::string& addr, std::uint32_t port) = 0;
Connect the TCP client to the given host and port.
virtualvoiddisconnect(bool wait_for_removal =false) = 0;
Disconnect the TCP client.
Ifwait_for_removal is set to true,disconnect blocks until the underlying TCP client has been effectively removed from theio_service (if any) and that all the underlying callbacks have completed.
virtualboolis_connected(void)const = 0;
Returns whether the client is connected or not.
virtualvoidasync_read(read_request& request) = 0;
Takes as parameter aread_request and process a read operation asynchronously.
read_request is defined as follows:
//! structure to store read requests informationstructread_request { std::size_t size;async_read_callback_t async_read_callback;};
where:
sizeis the number of bytes to readasync_read_callbackis the callback to be executed on read completion
async_read_callback is defined as follow:
typedef std::function<void(read_result&)>async_read_callback_t;
whereread_result is defined as follows:
//! structure to store read requests resultsstructread_result {bool success; std::vector<char> buffer;};
where:
successis whether the operation was successful or notbuffercontains the read bytes
virtualvoidasync_write(write_request& request) = 0;
Takes as parameter awrite_request and process a write operation asynchronously.
write_request is defined as follows:
//! structure to store write requests informationstructwrite_request { std::vector<char> buffer;async_write_callback_t async_write_callback;};
where:
buffercontains the bytes to be writtenasync_write_callbackis the callback to be executed on write completion
async_write_callback is defined as follow:
typedef std::function<void(write_result&)>async_write_callback_t;
wherewrite_result is defined as follows:
//! structure to store write requests resultsstructwrite_result {bool success; std::size_t size;};
where:
successis whether the operation was successful or notsizecontains the number of bytes written
The last step is to simply use the tcp_client you just have just created withcpp_redis.
When compiling withUSE_CUSTOM_TCP_CLIENT, the default constructors ofredis_client,redis_subscriber,future_client are disabled and you must you another constructor instead:
- cpp_redis::client
explicitclient(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
- cpp_redis::subscriber
explicitsubscriber(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
- cpp_redis::sentinel
explicitsentinel(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
Each of these constructors simply takes ashared_ptr to atcp_client_iface to be initialized: simply pass in ashared_ptr to your custom TCP client.
That's it, you are all set and can start usingcpp_redis with your chosen TCP client library!
Need more information?Contact me.