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

TCP Server

Simon Ninon edited this page Jun 11, 2017 · 11 revisions

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.

Typedefs

on_new_connection_callback_t

  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. Returning true means connection is handled by tcp_client wrapper and nothing will be done by tcp_server. Returning false means connection is handled by tcp_server, will be stored in an internal list and tcp_client disconection_handler overriden.

Methods

operator==

  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).

operator!=

  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).

start

  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.

stop

  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.

is_running

  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.

get_socket

  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.

get_clients

  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.

Clone this wiki locally