Class Server

Class Documentation

class Server

The ecal_service::Server class represents a binary service server that can be used may multiple clients.

The Server needs an io_context to run. It will listen on a specified port. When desired, the server can be opened on port 0, which will the OS chose a free port. The server uses one callback for the actual service function and another callback for events. When stopping, a Disconnect event for each connected client will be fired.

Callbacks are executed in the context of the io_context. Therfore, long running callbacks can block the server and everything else, that is dependent on the io_context.

Important: Do not stop the io_context while the server is running. This may cause undefined behavior. In fact, the io_context should run out of work on its own.

Tip: Use the ecal_service::ServerManager class to manage the server’s lifecycle. This enables you to stop all servers from

a single place when performing an application shutdown.

Sample code:

auto io_context = std::make_shared<asio::io_context>();
auto dummy_work = std::make_shared<asio::io_context::work>(*io_context);

std::thread io_thread([&io_context]() { io_context->run(); });

auto server = ecal_service::Server::create(io_context, ...)

// Do stuff

server->stop();
dummy_work->reset();
io_thread.join();

Public Types

using EventCallbackT = ServerEventCallbackT
using ServiceCallbackT = ServerServiceCallbackT
using DeleteCallbackT = std::function<void(Server*)>

Public Functions

Server(const Server&) = delete
Server(Server&&) = delete
Server &operator=(const Server&) = delete
Server &operator=(Server&&) = delete
~Server() = default
bool is_connected() const

Checks if any client is currently connected to the server.

Returns:

true, when at least one client is connected. False otherwise.

int get_connection_count() const

Get the number of currently connected clients.

Returns:

the number of connected clients

std::uint16_t get_port() const

Returns the port the server is listening on.

When the server was created with port 0, this will return the port the OS chose.

Returns:

The port

void stop()

Stops the server.

This closes the socket and stops the server. After the server has been stopped, it will still execute disconnect callbacks. The server will not accept new connections, though.

A server that has been stopped cannot be restarted. Create a new server, when desired.

Public Static Functions

static std::shared_ptr<Server> create(const std::shared_ptr<asio::io_context> &io_context, std::uint8_t protocol_version, std::uint16_t port, const ServiceCallbackT &service_callback, bool parallel_service_calls_enabled, const EventCallbackT &event_callback, const LoggerT &logger, const DeleteCallbackT &delete_callback)

Creates a new Server instance.

A new server will directly start listening on the specified port and wait for new connections.

Important: Do not stop the io_context while the server is running. This may cause undefined behavior. In fact, the io_context should run out of work on its own.

Tip: Use the ecal_service::ServerManager class to manage the server’s lifecycle. This enables you to stop all servers from

a single place when performing an application shutdown.

Parameters:
  • io_context – The io_context to use for the server and all callbacks

  • protocol_version – The protocol version to use. When this is 0, the buggy protocol version 0 will be used.

  • port – The port to listen on. When this is 0, the OS will chose a free port.

  • service_callback – The callback to use for service calls. Will be executed in the context of the io_context.

  • parallel_service_calls_enabled – When true, service calls will be executed in parallel. When false, service calls will be executed sequentially.

  • event_callback – The callback to use for events (clients connect or clients disconnect). Will be executed in the context of the io_context.

  • logger – A function used for logging.

  • delete_callback – A callback that will be executed when the server is deleted.

Returns:

The new server instance.

static std::shared_ptr<Server> create(const std::shared_ptr<asio::io_context> &io_context, std::uint8_t protocol_version, std::uint16_t port, const ServiceCallbackT &service_callback, bool parallel_service_calls_enabled, const EventCallbackT &event_callback, const LoggerT &logger = default_logger("Service Server"))
static std::shared_ptr<Server> create(const std::shared_ptr<asio::io_context> &io_context, std::uint8_t protocol_version, std::uint16_t port, const ServiceCallbackT &service_callback, bool parallel_service_calls_enabled, const EventCallbackT &event_callback, const DeleteCallbackT &delete_callback)

Protected Functions

Server(const std::shared_ptr<asio::io_context> &io_context, std::uint8_t protocol_version, std::uint16_t port, const ServiceCallbackT &service_callback, bool parallel_service_calls_enabled, const EventCallbackT &event_callback, const LoggerT &logger)