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 Dec 12, 2016 · 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 operation completion

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 operation completion

Typedefs

async_read_callback_t

  typedef std::function<void(tcp_client&, const read_result&)> async_read_callback_t;
Type Description
tcp_client& Reference to the tcp_client that triggered the callback
const read_result& Result of the operation

async_write_callback_t

  typedef std::function<void(tcp_client&, const write_result&)> async_write_callback_t;
Type Description
tcp_client& Reference to the tcp_client that triggered the callback
const write_result& Result of the operation

disconnection_handler_t

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

Methods

operator==

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

Description

Comparison operator overload.

Parameters

Type Description
const tcp_client& 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 Description
const tcp_client& 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 Description
const std::string& Hostname
`std::uint32_t Port

Return value

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

disconnect

  void disconnect(void);

Description

Disconnect the tcp_client if it was currently connected.

Parameters

None

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 Description
const read_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 Description
const write_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 Description
const disconnection_handler_t& The new disconnection handler

Return value

Nothing

Clone this wiki locally