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

IO Service

Simon Ninon edited this page Mar 12, 2018 · 5 revisions

What's the IO Service?

The tacopie IO Service is the service that operates IO Handling.

It polls sockets for input and output, processes read and write operations and calls the appropriate callbacks.

The IO Service is defined as tacopie::network::io_service.

Global Instance

For most use cases, you do not have to worry about the IO Service.

Therefore, a default global instance is provided and used by all your tcp_client and tcp_server.

Get the global instance

tacopie::network::io_service defines the following static function:

const std::shared_ptr<io_service>& tacopie::network::get_default_io_service(void);

Therefore, you can access the global io_service instance from anywhere by calling:

tacopie::network::io_service::get_default_io_service();

When calling this function for the first time, the io service is created. Subsequent calls simply return the instance.

Set the global instance

tacopie::network::io_service defines the following static function:

void tacopie::network::set_default_io_service(const std::shared_ptr<io_service>& service);

Therefore, you can set the global io_service instance from anywhere by calling:

tacopie::network::io_service::set_default_io_service(some_instance);

This instance accepts nullptr. This can be useful if you are willing to make sure that the default io_service instance is destroyed. A typical use is if you need to fork in the middle of your program: forking requires to clean working threads to avoid issues in the child process. Then, setting the global instance to nullptr and destroying all objects using the io_service will make sure all background threads are joined.

Custom instance

However, even though there is a default global instance, it does not mean that the io_service is a singleton.

You can create as many io_service as you want and assign specific io_service to your tcp_client and tcp_server instances.

This can be useful in the case you want dedicated io_service for your clients or if you want different io_service configuration on different clients.