-
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;
};
This structure is used to store read requests result.
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;
};
This structure is used to store write requests result.
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;
};
This structure is used to store read requests.
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 |
struct write_request {
std::vector<char> buffer;
async_write_callback_t async_write_callback;
};
This structure is used to store write requests.
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 |
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 |
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 |
typedef std::function<void()> disconnection_handler_t;
bool operator==(const tcp_client& rhs) const;
Comparison operator overload.
Type | Description |
---|---|
const tcp_client& |
Reference to the tcp_client to compare with |
Returns true when the underlying sockets are the same (same file descriptor and socket type).
bool operator!=(const tcp_client& rhs) const;
Comparison operator overload.
Type | Description |
---|---|
const tcp_client& |
Reference to the tcp_client to compare with |
Returns true when the underlying sockets are different (different file descriptor or socket type).
void connect(const std::string& addr, std::uint32_t port);
Connect the tcp_client to the given host and port.
Type | Description |
---|---|
const std::string& |
Hostname |
`std::uint32_t | Port |
Returns nothing but throws in case of initializatio & connection failure.
void disconnect(void);
Disconnect the tcp_client if it was currently connected.
None
Returns nothing.
bool is_connected(void) const;
Indicates whether the tcp_client is connected or not
None
Returns true if the tcp_client is connected.
void async_read(const read_request& request);
Queue a read request that will be processed asynchronously as soon as the socket is readable.
Type | Description |
---|---|
const read_request& |
Read request specifications |
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);
Queue a write request that will be processed asynchronously as soon as the socket is writable.
Type | Description |
---|---|
const write_request& |
Write request specifications |
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);
Set the disconnection handler callback. The disconnection handler is called whenever the client has been disconnected.
Type | Description |
---|---|
const disconnection_handler_t& |
The new disconnection handler |
Nothing
Need more information? Contact me.