-
Notifications
You must be signed in to change notification settings - Fork 133
TCP Server
tacopie::tcp_server
is the class providing TCP Server features.
The tcp_client works entirely asynchronously
, waiting for the io_service
to notify whenever a new client wished to connect.
typedef std::function<bool(const std::shared_ptr<tcp_client>&)> on_new_connection_callback_t;
Type | Description |
---|---|
const std::shared_ptr<tcp_client>& |
Shared pointer to the tcp_client that wishes to connect |
bool |
Return value. Tells whether the client connection request should be accepted or declined |
bool operator==(const tcp_server& rhs) const;
Description
Comparison operator overload.
Parameters
Type | Name | Description |
---|---|---|
const tcp_server& |
rhs | Reference to the tcp_server to compare with |
Return value
Returns true when the underlying sockets are the same (same file descriptor and socket type).
bool operator!=(const tcp_server& rhs) const;
Description
Comparison operator overload.
Parameters
Type | Name | Description |
---|---|---|
const tcp_server& |
rhs | Reference to the tcp_server to compare with |
Return value
Returns true when the underlying sockets are different (different file descriptor or socket type).
void start(const std::string& addr, std::uint32_t port, const on_new_connection_callback_t& callback = nullptr);
Description
Start the tcp_server at the given host and port.
Parameters
Type | Name | Description |
---|---|---|
const std::string& |
host | Hostname |
`std::uint32_t | port | Port. If port is 0, host will be considered as a path to a unix socket and port will be ignored. |
const on_new_connection_callback_t& |
callback | Callback to call whenever a new client requests to connect |
Return value
Returns nothing but throws in case of initialization & connection failure.
void stop(bool wait_for_removal = false, bool recursive_wait_for_removal = true);
Description
Disconnect the tcp_server if it was currently running.
Parameters
Type | Name | Description |
---|---|---|
bool |
wait_for_removal | When sets to true, disconnect blocks until the underlying TCP server has been effectively removed from the io_service and that all the underlying callbacks have completed. |
bool |
recursive_wait_for_removal | When sets to true and wait_for_removal is also set to true, blocks until all the underlying TCP client connected to the TCP server have been effectively removed from the io_service and that all the underlying callbacks have completed. |
Return value
Returns nothing.
bool is_running(void) const;
Description
Indicates whether the tcp_server is running or not
Parameters
None
Return value
Returns true if the tcp_server is running.
tcp_socket& get_socket(void);
const tcp_socket& get_socket(void) const;
Description
Retrieves the tacopie::tcp_socket
associated to the server.
Parameters
None
Return value
Returns the tacopie::tcp_socket
associated to the server.
const std::list<std::shared_ptr<tacopie::tcp_client>>& get_clients(void) const;
Description
Retrieves the list of tacopie::tcp_client
connected to the server.
Parameters
None
Return value
Returns the list of tacopie::tcp_client
connected to the server.
Need more information? Contact me.