9 #ifndef CPPSERVER_ASIO_SERVICE_H
10 #define CPPSERVER_ASIO_SERVICE_H
15 #include "threads/thread.h"
21 #include <string_view>
53 class Service :
public std::enable_shared_from_this<Service>
67 explicit Service(
const std::shared_ptr<asio::io_service>& service,
bool strands =
false);
76 size_t threads() const noexcept {
return _threads.size(); }
81 bool IsPolling() const noexcept {
return _polling; }
83 bool IsStarted() const noexcept {
return _started; }
90 virtual bool Start(
bool polling =
false);
111 {
return _services[++_round_robin_index % _services.size()]; }
120 template <
typename CompletionHandler>
122 {
if (_strand_required)
return _strand->dispatch(handler);
else return _services[0]->dispatch(handler); }
130 template <
typename CompletionHandler>
132 {
if (_strand_required)
return _strand->post(handler);
else return _services[0]->post(handler); }
152 virtual void onIdle() { CppCommon::Thread::Yield(); }
160 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
164 std::vector<std::shared_ptr<asio::io_service>> _services;
166 std::vector<std::thread> _threads;
168 std::shared_ptr<asio::io_service::strand> _strand;
170 std::atomic<bool> _strand_required;
172 std::atomic<bool> _polling;
174 std::atomic<bool> _started;
175 std::atomic<size_t> _round_robin_index;
178 static void ServiceThread(
const std::shared_ptr<Service>& service,
const std::shared_ptr<asio::io_service>& io_service);
181 void SendError(std::error_code ec);
Asio C++ Library definition.
Service & operator=(Service &&)=delete
virtual void onStopped()
Handle service stopped notification.
Service(const Service &)=delete
bool IsStrandRequired() const noexcept
Is the service required strand to serialized handler execution?
bool IsPolling() const noexcept
Is the service started with polling loop mode?
virtual ~Service()=default
virtual void onThreadCleanup()
Cleanup thread handler.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
virtual void onIdle()
Handle service idle notification.
ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Post(ASIO_MOVE_ARG(CompletionHandler) handler)
Post the given handler.
virtual std::shared_ptr< asio::io_service > & GetAsioService() noexcept
Get the next available Asio IO service.
ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
Dispatch the given handler.
bool IsStarted() const noexcept
Is the service started?
virtual bool Stop()
Stop the service.
virtual bool Start(bool polling=false)
Start the service.
Service(Service &&)=delete
Service(int threads=1, bool pool=false)
Initialize Asio service with single or multiple working threads.
size_t threads() const noexcept
Get the number of working threads.
virtual void onThreadInitialize()
Initialize thread handler.
Service & operator=(const Service &)=delete
virtual bool Restart()
Restart the service.
virtual void onStarted()
Handle service started notification.
Asio memory manager definition.
C++ Server project definitions.