Class ServerManager
Defined in File server_manager.h
Inheritance Relationships
Base Type
public std::enable_shared_from_this< ServerManager >
Class Documentation
-
class ServerManager : public std::enable_shared_from_this<ServerManager>
Manager for ecal_service::server instances.
The ServerManager keeps track of and manages all ecal_service::server instances that it created. It is used to create and stop servers. The user doesn’t need to manage the servers manually, e.g. for stopping them from a central place.
The ServerManager is only available as shared_ptr. It must be created using the static create() method.
Upon creation, the ServerManager will create a work object for the given io_context. This will keep the io_context alive, even if there are no servers running.
For creating a server, the create_server() method must be used. This will create a new server instance and return a shared_ptr to it.
For stopping all servers, the stop() method must be used. This will stop all servers and delete the internal work object, so the thread executing it can be joined.
Important: Do not stop the io_context. This may cause undefined behavior. Instead, stop the server manager and wait for the
io_context to run out of work on its own.
Example code:
// Create a server manager auto server_manager = ecal_service::ServerManager::create(io_context); // Create and start an io_context thread. std::thread io_context_thread([&io_context]() { io_context->run(); }); // Create actual server instances auto server1 = server_manager->create_server(...) auto server2 = server_manager->create_server(...) // DO STUFF // Stop ALL servers from a common place server_manager->stop(); // Join the io_context thread. The io_context does not need to be stopped. io_context_thread.join();
Public Functions
-
ServerManager(const ServerManager&) = delete
-
ServerManager(ServerManager&&) = delete
-
ServerManager &operator=(const ServerManager&) = delete
-
ServerManager &operator=(ServerManager&&) = delete
-
~ServerManager()
-
std::shared_ptr<Server> create_server(std::uint8_t protocol_version, std::uint16_t port, const Server::ServiceCallbackT &service_callback, bool parallel_service_calls_enabled, const Server::EventCallbackT &event_callback)
Create a new server instance, which is managed by this server manager.
The server manager will keep track of the create server internally. The server (and all other servers, that have been created by this manager) can be stopped via the stop() method.
- Parameters:
protocol_version – The protocol version, that will be used by this server
port – The port, that the server will listen on. If 0, the OS will choose a free port.
service_callback – The callback, that will be called for each incoming service call. The callback will be executed in the io_context thread.
parallel_service_calls_enabled – If true, the server will handle incoming service calls in parallel. If false, the server will handle incoming service calls sequentially.
event_callback – The callback, that will be called whenever a client connects or disconnects. The callback will be executed in the io_context thread.
- Returns:
a shared pointer to the created server
-
size_t server_count() const
Get the number of servers, that are currently managed by this server manager.
- Returns:
The number of servers
-
void stop()
Stop all servers that are currently managed by this server manager.
This will also delete the internal work object, so the thread executing the io_context can be joined. The io context will run out of work on its own. Obviously, this is only true if no other managers etc. use the io_context.
-
bool is_stopped() const
Returns true, if the server manager is stopped.
If stopped, the server manager cannot be restarted. Create a new one instead. Make sure, that the io_context is executed by a thread again, when creating a new server manger.
- Returns:
true, if the server_manager is stopped. False otherwise.
Public Static Functions
Create a new server manager, that can be used to create and stop servers.
After creation, the server manager will create a work object for the given io_context. This will keep the io_context alive, even if there are no servers running. When stopping the servers, that work object will be deleted and the io_context will run out of work on its own.
- Parameters:
io_context – The io context, that will be used for all servers
logger – A logger-function that will be used for by all servers
- Returns:
A shared_ptr to the created server manager
Protected Functions