CppServer  1.0.4.0
C++ Server Library
Public Member Functions | Protected Member Functions | List of all members
CppServer::Asio::Service Class Reference

Asio service. More...

#include <service.h>

Inheritance diagram for CppServer::Asio::Service:

Public Member Functions

 Service (int threads=1, bool pool=false)
 Initialize Asio service with single or multiple working threads. More...
 
 Service (const std::shared_ptr< asio::io_service > &service, bool strands=false)
 Initialize Asio service manually with a given Asio IO service. More...
 
 Service (const Service &)=delete
 
 Service (Service &&)=delete
 
virtual ~Service ()=default
 
Serviceoperator= (const Service &)=delete
 
Serviceoperator= (Service &&)=delete
 
size_t threads () const noexcept
 Get the number of working threads. More...
 
bool IsStrandRequired () const noexcept
 Is the service required strand to serialized handler execution? More...
 
bool IsPolling () const noexcept
 Is the service started with polling loop mode? More...
 
bool IsStarted () const noexcept
 Is the service started? More...
 
virtual bool Start (bool polling=false)
 Start the service. More...
 
virtual bool Stop ()
 Stop the service. More...
 
virtual bool Restart ()
 Restart the service. More...
 
virtual std::shared_ptr< asio::io_service > & GetAsioService () noexcept
 Get the next available Asio IO service. More...
 
template<typename CompletionHandler >
 ASIO_INITFN_RESULT_TYPE (CompletionHandler, void()) Dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
 Dispatch the given handler. More...
 
template<typename CompletionHandler >
 ASIO_INITFN_RESULT_TYPE (CompletionHandler, void()) Post(ASIO_MOVE_ARG(CompletionHandler) handler)
 Post the given handler. More...
 

Protected Member Functions

virtual void onThreadInitialize ()
 Initialize thread handler. More...
 
virtual void onThreadCleanup ()
 Cleanup thread handler. More...
 
virtual void onStarted ()
 Handle service started notification. More...
 
virtual void onStopped ()
 Handle service stopped notification. More...
 
virtual void onIdle ()
 Handle service idle notification. More...
 
virtual void onError (int error, const std::string &category, const std::string &message)
 Handle error notification. More...
 

Detailed Description

Asio service.

Asio service is used to host all clients/servers based on Asio C++ library. It is implemented based on Asio C++ Library and use one or more threads to perform all asynchronous IO operations and communications.

There are three ways to initialize Asio service:

1) Service(threads, false) - initialize a new Asio service with io-service- per-thread design. In this case each Asio IO will be bounded to its own working thread and all handlers will be dispatched sequentially without strands making the code clean and easy to maintain.

2) Service(threads, true) - initialize a new Asio service with thread-pool design. In this case single Asio IO will be bounded to all threads in pool, but strands will be required to serialize handler execution.

3) Service(service, true | false) - initialize a new Asio service using the existing Asio IO service instance with required strands flag. Strands are required for serialized handler execution when single Asio IO service used in thread pool.

Thread-safe.

https://think-async.com

Definition at line 53 of file service.h.

Constructor & Destructor Documentation

◆ Service() [1/4]

CppServer::Asio::Service::Service ( int  threads = 1,
bool  pool = false 
)
explicit

Initialize Asio service with single or multiple working threads.

Parameters
threads- Working threads count (default is 1)
pool- Asio service thread pool flag (default is false)

Definition at line 16 of file service.cpp.

◆ Service() [2/4]

CppServer::Asio::Service::Service ( const std::shared_ptr< asio::io_service > &  service,
bool  strands = false 
)
explicit

Initialize Asio service manually with a given Asio IO service.

Parameters
service- Asio IO service
strands- Asio IO service strands required flag (default is false)

Definition at line 49 of file service.cpp.

◆ Service() [3/4]

CppServer::Asio::Service::Service ( const Service )
delete

◆ Service() [4/4]

CppServer::Asio::Service::Service ( Service &&  )
delete

◆ ~Service()

virtual CppServer::Asio::Service::~Service ( )
virtualdefault

Member Function Documentation

◆ ASIO_INITFN_RESULT_TYPE() [1/2]

template<typename CompletionHandler >
CppServer::Asio::Service::ASIO_INITFN_RESULT_TYPE ( CompletionHandler  ,
void()   
)
inline

Dispatch the given handler.

The given handler may be executed immediately if this function is called from IO service thread. Otherwise it will be enqueued to the IO service pending operations queue.

Method takes a handler to dispatch as a parameter and returns async result of the handler.

Definition at line 121 of file service.h.

◆ ASIO_INITFN_RESULT_TYPE() [2/2]

template<typename CompletionHandler >
CppServer::Asio::Service::ASIO_INITFN_RESULT_TYPE ( CompletionHandler  ,
void()   
)
inline

Post the given handler.

The given handler will be enqueued to the IO service pending operations queue.

Method takes a handler to dispatch as a parameter and returns async result of the handler.

Definition at line 131 of file service.h.

◆ GetAsioService()

virtual std::shared_ptr<asio::io_service>& CppServer::Asio::Service::GetAsioService ( )
inlinevirtualnoexcept

Get the next available Asio IO service.

Method will return single Asio IO service for manual or thread pool design or will return the next available Asio IO service using round-robin algorithm for io-service-per-thread design.

Returns
Asio IO service

Definition at line 110 of file service.h.

◆ IsPolling()

bool CppServer::Asio::Service::IsPolling ( ) const
inlinenoexcept

Is the service started with polling loop mode?

Definition at line 81 of file service.h.

◆ IsStarted()

bool CppServer::Asio::Service::IsStarted ( ) const
inlinenoexcept

Is the service started?

Definition at line 83 of file service.h.

◆ IsStrandRequired()

bool CppServer::Asio::Service::IsStrandRequired ( ) const
inlinenoexcept

Is the service required strand to serialized handler execution?

Definition at line 79 of file service.h.

◆ onError()

virtual void CppServer::Asio::Service::onError ( int  error,
const std::string &  category,
const std::string &  message 
)
inlineprotectedvirtual

Handle error notification.

Parameters
error- Error code
category- Error category
message- Error message

Definition at line 160 of file service.h.

◆ onIdle()

virtual void CppServer::Asio::Service::onIdle ( )
inlineprotectedvirtual

Handle service idle notification.

Definition at line 152 of file service.h.

◆ onStarted()

virtual void CppServer::Asio::Service::onStarted ( )
inlineprotectedvirtual

Handle service started notification.

Definition at line 147 of file service.h.

◆ onStopped()

virtual void CppServer::Asio::Service::onStopped ( )
inlineprotectedvirtual

Handle service stopped notification.

Definition at line 149 of file service.h.

◆ onThreadCleanup()

virtual void CppServer::Asio::Service::onThreadCleanup ( )
inlineprotectedvirtual

Cleanup thread handler.

This handler can be used to cleanup priority or affinity of the service thread.

Definition at line 144 of file service.h.

◆ onThreadInitialize()

virtual void CppServer::Asio::Service::onThreadInitialize ( )
inlineprotectedvirtual

Initialize thread handler.

This handler can be used to initialize priority or affinity of the service thread.

Definition at line 139 of file service.h.

◆ operator=() [1/2]

Service& CppServer::Asio::Service::operator= ( const Service )
delete

◆ operator=() [2/2]

Service& CppServer::Asio::Service::operator= ( Service &&  )
delete

◆ Restart()

bool CppServer::Asio::Service::Restart ( )
virtual

Restart the service.

Returns
'true' if the service was successfully restarted, 'false' if the service failed to restart

Definition at line 147 of file service.cpp.

◆ Start()

bool CppServer::Asio::Service::Start ( bool  polling = false)
virtual

Start the service.

Parameters
polling- Polling loop mode with idle handler call (default is false)
Returns
'true' if the service was successfully started, 'false' if the service failed to start

Definition at line 64 of file service.cpp.

◆ Stop()

bool CppServer::Asio::Service::Stop ( )
virtual

Stop the service.

Returns
'true' if the service was successfully stopped, 'false' if the service is already stopped

Definition at line 105 of file service.cpp.

◆ threads()

size_t CppServer::Asio::Service::threads ( ) const
inlinenoexcept

Get the number of working threads.

Definition at line 76 of file service.h.


The documentation for this class was generated from the following files: