9#ifndef CPPSERVER_ASIO_SERVICE_H
10#define CPPSERVER_ASIO_SERVICE_H
15#include "threads/thread.h"
53class Service :
public std::enable_shared_from_this<Service>
67 explicit Service(
const std::shared_ptr<asio::io_context>& io_context,
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 _io_contexts[++_round_robin_index % _io_contexts.size()]; }
120 template <
typename CompletionHandler>
122 {
if (_strand_required)
return asio::dispatch(*_strand, handler);
else return asio::dispatch(*_io_contexts[0], handler); }
130 template <
typename CompletionHandler>
132 {
if (_strand_required)
return asio::post(*_strand, handler);
else return asio::post(*_io_contexts[0], 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_context>> _io_contexts;
166 std::vector<std::thread> _threads;
168 std::shared_ptr<asio::io_context::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_context>& io_context);
181 void SendError(std::error_code ec);
Asio C++ Library definition.
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 std::shared_ptr< asio::io_context > & GetAsioContext() noexcept
Get the next available Asio IO context.
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.
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.
Service & operator=(Service &&)=delete
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.