-
Notifications
You must be signed in to change notification settings - Fork 133
TCP Client
tacopie::tcp_client
is the class providing TCP Client features.
The tcp_client works asynchronously
, either for writing and reading operations.
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 |
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 |
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. |
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. |
typedef std::function<void(read_result&)> async_read_callback_t;
Type | Description |
---|---|
const read_result& |
Result of the operation |
typedef std::function<void(write_result&)> async_write_callback_t;
Type | Description |
---|---|
const write_result& |
Result of the operation |
typedef std::function<void()> disconnection_handler_t;
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 |
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).
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).
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.
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.
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.
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).
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).
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
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.
Need more information? Contact me.