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

TCP Client

Simon Ninon edited this page Apr 10, 2017 · 19 revisions

tacopie::tcp_client is the class providing TCP Client features.

The tcp_client works asynchronously, either for writing and reading operations.

Structures

read_result

  struct read_result {
    bool success;
    std::vector<char> buffer;
  };

Description

This structure is used to store read requests result.

Fields

Type Field Description
bool success Whether the read operation has succeeded or not. If false, the client has been disconnected
std::vector<char> buffer Vector containing the read bytes

write_result

  struct write_result {
    bool success;
    std::size_t size;
  };

Description

This structure is used to store write requests result.

Fields

Type Field Description
bool success Whether the write operation has succeeded or not. If false, the client has been disconnected
std::size_t size Number of bytes written

read_request

  struct read_request {
    std::size_t size;
    async_read_callback_t async_read_callback;
  };

Description

This structure is used to store read requests.

Fields

Type Field Description
std::size_t size Number of bytes to read
tcp_client::async_read_callback_t async_read_callback Callback to be called on a read operation completion, even though the operation read less bytes than requested.

write_request

  struct write_request {
    std::vector<char> buffer;
    async_write_callback_t async_write_callback;
  };

Description

This structure is used to store write requests.

Fields

Type Field Description
std::vector<char> buffer Bytes to be written
tcp_client::async_write_callback_t async_write_callback Callback to be called on a write operation completion, even though the operation wrote less bytes than requested.

Typedefs

async_read_callback_t

  typedef std::function<void(read_result&)> async_read_callback_t;
Type Description
const read_result& Result of the operation

async_write_callback_t

  typedef std::function<void(write_result&)> async_write_callback_t;
Type Description
const write_result& Result of the operation

disconnection_handler_t

  typedef std::function<void()> disconnection_handler_t;

Methods

custom constructor

  explicit tcp_client(tcp_socket&& socket);

Description

Constructor a new tcp_client from a tcp_socket which is moved (and not copied).

Parameters

Type Name Description
tcp_socket&& socket Reference to the tcp_socket to be moved to initialize the tcp_client

operator==

  bool operator==(const tcp_client& rhs) const;

Description

Comparison operator overload.

Parameters

Type Name Description
const tcp_client& rhs Reference to the tcp_client to compare with

Return value

Returns true when the underlying sockets are the same (same file descriptor and socket type).

operator!=

  bool operator!=(const tcp_client& rhs) const;

Description

Comparison operator overload.

Parameters

Type Name Description
const tcp_client& rhs Reference to the tcp_client to compare with

Return value

Returns true when the underlying sockets are different (different file descriptor or socket type).

connect

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

Description

Connect the tcp_client to the given host and port.

Parameters

Type Name Description
const std::string& addr Hostname
std::uint32_t port Port. If port is 0, addr will be considered as a path to a Unix socket and port will be ignored.

Return value

Returns nothing but throws in case of initialization & connection failure.

disconnect

  void disconnect(bool wait_for_removal = false);

Description

Disconnect the tcp_client if it was currently connected.

Parameters

Type Name Description
bool 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.

Return value

Returns nothing.

is_connected

  bool is_connected(void) const;

Description

Indicates whether the tcp_client is connected or not

Parameters

None

Return value

Returns true if the tcp_client is connected.

async_read

  void async_read(const read_request& request);

Description

Queue a read request that will be processed asynchronously as soon as the socket is readable.

Parameters

Type Name Description
const read_request& request Read request specifications

Return value

Returns nothing but throws in case of invalid usage (for example, if the tcp_client is not connected).

async_write

  void async_write(const write_request& request);

Description

Queue a write request that will be processed asynchronously as soon as the socket is writable.

Parameters

Type Name Description
const write_request& request Write request specifications

Return value

Returns nothing but throws in case of invalid usage (for example, if the tcp_client is not connected).

set_on_disconnection_handler

  void set_on_disconnection_handler(const disconnection_handler_t& disconnection_handler);

Description

Set the disconnection handler callback. The disconnection handler is called whenever the client has been disconnected.

Parameters

Type Name Description
const disconnection_handler_t& disconnection_handler The new disconnection handler

Return value

Nothing

get_socket

  tcp_socket& get_socket(void);
  const tcp_socket& get_socket(void) const;

Description

Retrieves the tacopie::tcp_socket associated to the client.

Parameters

None

Return value

Returns the tacopie::tcp_socket associated to the client.

Clone this wiki locally