CppServer  1.0.4.0
C++ Server Library
service.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_SERVICE_H
10 #define CPPSERVER_ASIO_SERVICE_H
11 
12 #include "asio.h"
13 #include "memory.h"
14 
15 #include "threads/thread.h"
16 
17 #include <atomic>
18 #include <cassert>
19 #include <memory>
20 #include <string>
21 #include <string_view>
22 #include <vector>
23 
24 namespace CppServer {
25 namespace Asio {
26 
28 
53 class Service : public std::enable_shared_from_this<Service>
54 {
55 public:
57 
61  explicit Service(int threads = 1, bool pool = false);
63 
67  explicit Service(const std::shared_ptr<asio::io_service>& service, bool strands = false);
68  Service(const Service&) = delete;
69  Service(Service&&) = delete;
70  virtual ~Service() = default;
71 
72  Service& operator=(const Service&) = delete;
73  Service& operator=(Service&&) = delete;
74 
76  size_t threads() const noexcept { return _threads.size(); }
77 
79  bool IsStrandRequired() const noexcept { return _strand_required; }
81  bool IsPolling() const noexcept { return _polling; }
83  bool IsStarted() const noexcept { return _started; }
84 
86 
90  virtual bool Start(bool polling = false);
92 
95  virtual bool Stop();
97 
100  virtual bool Restart();
101 
103 
110  virtual std::shared_ptr<asio::io_service>& GetAsioService() noexcept
111  { return _services[++_round_robin_index % _services.size()]; }
112 
114 
120  template <typename CompletionHandler>
121  ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
122  { if (_strand_required) return _strand->dispatch(handler); else return _services[0]->dispatch(handler); }
123 
125 
130  template <typename CompletionHandler>
131  ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Post(ASIO_MOVE_ARG(CompletionHandler) handler)
132  { if (_strand_required) return _strand->post(handler); else return _services[0]->post(handler); }
133 
134 protected:
136 
139  virtual void onThreadInitialize() {}
141 
144  virtual void onThreadCleanup() {}
145 
147  virtual void onStarted() {}
149  virtual void onStopped() {}
150 
152  virtual void onIdle() { CppCommon::Thread::Yield(); }
153 
155 
160  virtual void onError(int error, const std::string& category, const std::string& message) {}
161 
162 private:
163  // Asio IO services
164  std::vector<std::shared_ptr<asio::io_service>> _services;
165  // Asio service working threads
166  std::vector<std::thread> _threads;
167  // Asio service strand for serialized handler execution
168  std::shared_ptr<asio::io_service::strand> _strand;
169  // Asio service strands required flag
170  std::atomic<bool> _strand_required;
171  // Asio service polling loop mode flag
172  std::atomic<bool> _polling;
173  // Asio service state
174  std::atomic<bool> _started;
175  std::atomic<size_t> _round_robin_index;
176 
178  static void ServiceThread(const std::shared_ptr<Service>& service, const std::shared_ptr<asio::io_service>& io_service);
179 
181  void SendError(std::error_code ec);
182 };
183 
186 } // namespace Asio
187 } // namespace CppServer
188 
189 #endif // CPPSERVER_ASIO_SERVICE_H
Asio C++ Library definition.
Asio service.
Definition: service.h:54
Service & operator=(Service &&)=delete
virtual void onStopped()
Handle service stopped notification.
Definition: service.h:149
Service(const Service &)=delete
bool IsStrandRequired() const noexcept
Is the service required strand to serialized handler execution?
Definition: service.h:79
bool IsPolling() const noexcept
Is the service started with polling loop mode?
Definition: service.h:81
virtual ~Service()=default
virtual void onThreadCleanup()
Cleanup thread handler.
Definition: service.h:144
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: service.h:160
virtual void onIdle()
Handle service idle notification.
Definition: service.h:152
ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Post(ASIO_MOVE_ARG(CompletionHandler) handler)
Post the given handler.
Definition: service.h:131
virtual std::shared_ptr< asio::io_service > & GetAsioService() noexcept
Get the next available Asio IO service.
Definition: service.h:110
ASIO_INITFN_RESULT_TYPE(CompletionHandler, void()) Dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
Dispatch the given handler.
Definition: service.h:121
bool IsStarted() const noexcept
Is the service started?
Definition: service.h:83
virtual bool Stop()
Stop the service.
Definition: service.cpp:105
virtual bool Start(bool polling=false)
Start the service.
Definition: service.cpp:64
Service(Service &&)=delete
Service(int threads=1, bool pool=false)
Initialize Asio service with single or multiple working threads.
Definition: service.cpp:16
size_t threads() const noexcept
Get the number of working threads.
Definition: service.h:76
virtual void onThreadInitialize()
Initialize thread handler.
Definition: service.h:139
Service & operator=(const Service &)=delete
virtual bool Restart()
Restart the service.
Definition: service.cpp:147
virtual void onStarted()
Handle service started notification.
Definition: service.h:147
Asio memory manager definition.
C++ Server project definitions.
Definition: asio.h:56