CppServer
1.0.4.0
C++ Server Library
|
#include <service.h>
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 |
Service & | operator= (const Service &)=delete |
Service & | operator= (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... | |
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.
|
explicit |
Initialize Asio service with single or multiple working threads.
threads | - Working threads count (default is 1) |
pool | - Asio service thread pool flag (default is false) |
Definition at line 16 of file service.cpp.
|
explicit |
|
delete |
|
delete |
|
virtualdefault |
|
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.
|
inline |
|
inlinevirtualnoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
virtual |
Restart the service.
Definition at line 147 of file service.cpp.
|
virtual |
Start the service.
polling | - Polling loop mode with idle handler call (default is false) |
Definition at line 64 of file service.cpp.
|
virtual |
Stop the service.
Definition at line 105 of file service.cpp.
|
inlinenoexcept |