Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Custom network module

Simon Ninon edited this page Apr 4, 2017 · 5 revisions

Tacopie

tacopie is a C++11 TCP Client library used by default by cpp_redis to make it easy to test and develop a software communicating with a Redis server using cpp_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 by tacopie.

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.

Setting-Up your custom library

CMake USE_CUSTOM_TCP_CLIENT

First, you have to compile with the USE_CUSTOM_TCP_CLIENT CMake variable. This CMake variable will make cpp_redis compile in a special configuration allowing you to use the library of your choice.

Provide implementation for tcp_client_iface

cpp_redis defines the tcp_client_iface interface in cpp_redis/network/tcp_client_iface.hpp.

You must provide an implementation for this interface by creating your own class inheriting from cpp_redis::network::tcp_client_iface and implementing the following methods:

connect

virtual void connect(const std::string& addr, std::uint32_t port) = 0;

Connect the TCP client to the given host and port.

disconnect

virtual void disconnect(bool wait_for_removal = false) = 0;

Disconnect the TCP client.

If wait_for_removal is set to true, disconnect should not return until the tcp_client has been removed entirely from the underlying io_service if any.

This parameter can be ignored if it is not relevant to your implementation. It is actually used for tacopie but might be removed in the future to make it more generic.

is_connected

virtual bool is_connected(void) const = 0;

Returns whether the client is connected or not.

async_read

virtual void async_read(read_request& request)   = 0;

Takes as parameter a read_request and process a read operation asynchronously.

read_request is defined as follow:

//! structure to store read requests information
struct read_request {
  std::size_t size;
  async_read_callback_t async_read_callback;
};

where:

  • size is the number of bytes to read
  • async_read_callback is 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;

where read_result is defined as follow:

//! structure to store read requests result
struct read_result {
  bool success;
  std::vector<char> buffer;
};

where:

  • success is whether the operation was successful or not
  • buffer contains the read bytes

async_write

virtual void async_write(write_request& request) = 0;

Takes as parameter a write_request and process a write operation asynchronously.

write_request is defined as follow:

//! structure to store write requests information
struct write_request {
  std::vector<char> buffer;
  async_write_callback_t async_write_callback;
};

where:

  • buffer contains the bytes to be written
  • async_write_callback is 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;

where write_result is defined as follow:

//! structure to store write requests result
struct write_result {
  bool success;
  std::size_t size;
};

where:

  • success is whether the operation was successful or not
  • size contains the number of bytes written