NCBI C++ ToolKit
tcp_daemon.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef TCPDAEMON__HPP
2 #define TCPDAEMON__HPP
3 
4 /* $Id: tcp_daemon.hpp 101453 2023-12-14 20:19:17Z satskyse $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Dmitri Dmitrienko
30  *
31  * File Description:
32  *
33  */
34 
35 #include <stdexcept>
36 #include <sstream>
37 #include <vector>
38 #include <string>
39 #include <memory>
40 #include <cassert>
41 
42 #include "uv.h"
43 #include "uv_extra.h"
44 
45 #include "UvHelper.hpp"
46 
47 #include "http_connection.hpp"
48 #include "http_daemon.hpp"
52 
53 class CTcpWorkersList;
54 class CTcpDaemon;
55 struct CTcpWorker;
56 
57 
59 {
60  friend class CTcpDaemon;
61 
62 private:
63  std::vector<std::unique_ptr<CTcpWorker>> m_workers;
65  std::function<void(CTcpDaemon & daemon)> m_on_watch_dog;
66 
67 protected:
68  static void s_WorkerExecute(void * _worker);
69 
70 public:
71  static uv_key_t s_thread_worker_key;
72 
75  {}
77 
78  void Start(struct uv_export_t * exp,
79  unsigned short nworkers,
80  CHttpDaemon & http_daemon,
81  std::function<void(CTcpDaemon & daemon)> OnWatchDog = nullptr);
82 
83  bool AnyWorkerIsRunning(void);
84  void KillAll(void);
85  void JoinWorkers(void);
86 
87  static void s_OnWatchDog(uv_timer_t * handle);
88 };
89 
90 
93  uv_tcp_t m_listener;
94  uv_async_t m_async_stop;
95  uv_async_t m_async_work;
96  uv_timer_t m_timer;
97 
99  m_listener({0}),
100  m_async_stop({0}),
101  m_async_work({0}),
102  m_timer({0})
103  {}
104 };
105 
106 
108 {
109  unsigned int m_id;
110  uv_thread_t m_thread;
111  std::atomic_uint_fast64_t m_request_count;
112  std::atomic_uint_fast16_t m_connection_count;
113  std::atomic_bool m_started;
114  std::atomic_bool m_shutdown;
115  std::atomic_bool m_shuttingdown;
117  bool m_joined;
118  int m_error;
119  std::list<std::tuple<uv_tcp_t, CHttpConnection>> m_connected_list;
120  std::list<std::tuple<uv_tcp_t, CHttpConnection>> m_free_list;
121  struct uv_export_t * m_exp;
127  std::unique_ptr<CTcpWorkerInternal_t> m_internal;
128 
129  CTcpWorker(unsigned int id, struct uv_export_t * exp,
130  CTcpDaemon * daemon,
131  CTcpWorkersList * guard,
132  CHttpDaemon & http_daemon) :
133  m_id(id),
134  m_thread(0),
135  m_request_count(0),
137  m_started(false),
138  m_shutdown(false),
141  m_joined(false),
142  m_error(0),
143  m_exp(exp),
144  m_guard(guard),
145  m_daemon(daemon),
146  m_HttpDaemon(http_daemon),
148  {}
149 
150  void Stop(void);
151  void Execute(void);
152  void CloseAll(void);
153  void OnClientClosed(uv_handle_t * handle);
154  void WakeWorker(void);
155  std::list<std::tuple<uv_tcp_t, CHttpConnection>> & GetConnList(void);
156 
157 private:
158  void OnAsyncWork(void);
159  static void s_OnAsyncWork(uv_async_t * handle);
160  void OnTimer(void);
161  static void s_OnTimer(uv_timer_t * handle);
162  static void s_OnAsyncStop(uv_async_t * handle);
163  static void s_OnTcpConnection(uv_stream_t * listener, const int status);
164  static void s_OnClientClosed(uv_handle_t * handle);
165  static void s_LoopWalk(uv_handle_t * handle, void * arg);
166  void OnTcpConnection(uv_stream_t * listener);
167 };
168 
169 
171 {
172 private:
174  unsigned short m_port;
175  unsigned short m_num_workers;
176  unsigned short m_backlog;
177  unsigned short m_max_connections;
179  std::atomic_uint_fast16_t m_connection_count;
180 
181  friend class CTcpWorkersList;
182  friend class CTcpWorker;
183 
184 private:
185  static void s_OnMainSigInt(uv_signal_t * /* req */, int /* signum */);
186  static void s_OnMainSigTerm(uv_signal_t * /* req */, int /* signum */);
187 
188  static void s_OnMainSigHup(uv_signal_t * /* req */, int /* signum */)
189  { PSG_MESSAGE("SIGHUP received. Ignoring."); }
190 
191  static void s_OnMainSigUsr1(uv_signal_t * /* req */, int /* signum */)
192  { PSG_MESSAGE("SIGUSR1 received. Ignoring."); }
193 
194  static void s_OnMainSigUsr2(uv_signal_t * /* req */, int /* signum */)
195  { PSG_MESSAGE("SIGUSR2 received. Ignoring."); }
196 
197  static void s_OnMainSigWinch(uv_signal_t * /* req */, int /* signum */)
198  { PSG_MESSAGE("SIGWINCH received. Ignoring."); }
199 
200  bool ClientConnected(void);
201  bool ClientDisconnected(void);
202 
203 protected:
204  static constexpr const char IPC_PIPE_NAME[] = "tcp_daemon_startup_rpc";
205 
206 public:
207  CTcpDaemon(const std::string & Address, unsigned short Port,
208  unsigned short NumWorkers, unsigned short BackLog,
209  unsigned short MaxConnections) :
210  m_address(Address),
211  m_port(Port),
212  m_num_workers(NumWorkers),
213  m_backlog(BackLog),
214  m_max_connections(MaxConnections),
217  {}
218 
220 
221  void StopDaemonLoop(void);
222  bool OnRequest(CHttpProto ** http_proto);
223  uint16_t NumOfConnections(void) const;
224  unsigned short GetMaxConnections(void) const
225  {
226  return m_max_connections;
227  }
228 
229  void Run(CHttpDaemon & http_daemon,
230  std::function<void(CTcpDaemon & daemon)>
231  OnWatchDog = nullptr);
232 };
233 
234 #endif
235 
unsigned short m_port
Definition: tcp_daemon.hpp:174
CUvLoop * m_UVLoop
Definition: tcp_daemon.hpp:219
std::string m_address
Definition: tcp_daemon.hpp:173
static void s_OnMainSigUsr2(uv_signal_t *, int)
Definition: tcp_daemon.hpp:194
static void s_OnMainSigWinch(uv_signal_t *, int)
Definition: tcp_daemon.hpp:197
unsigned short GetMaxConnections(void) const
Definition: tcp_daemon.hpp:224
std::atomic_uint_fast16_t m_connection_count
Definition: tcp_daemon.hpp:179
bool ClientConnected(void)
Definition: tcp_daemon.cpp:645
static void s_OnMainSigTerm(uv_signal_t *, int)
Definition: tcp_daemon.cpp:626
CTcpDaemon(const std::string &Address, unsigned short Port, unsigned short NumWorkers, unsigned short BackLog, unsigned short MaxConnections)
Definition: tcp_daemon.hpp:207
bool ClientDisconnected(void)
Definition: tcp_daemon.cpp:652
static void s_OnMainSigUsr1(uv_signal_t *, int)
Definition: tcp_daemon.hpp:191
uint16_t NumOfConnections(void) const
Definition: tcp_daemon.cpp:681
bool OnRequest(CHttpProto **http_proto)
Definition: tcp_daemon.cpp:665
unsigned short m_backlog
Definition: tcp_daemon.hpp:176
unsigned short m_max_connections
Definition: tcp_daemon.hpp:177
static void s_OnMainSigInt(uv_signal_t *, int)
Definition: tcp_daemon.cpp:611
static void s_OnMainSigHup(uv_signal_t *, int)
Definition: tcp_daemon.hpp:188
static constexpr const char IPC_PIPE_NAME[]
Definition: tcp_daemon.hpp:204
void StopDaemonLoop(void)
Definition: tcp_daemon.cpp:659
CTcpWorkersList * m_workers
Definition: tcp_daemon.hpp:178
unsigned short m_num_workers
Definition: tcp_daemon.hpp:175
void Run(CHttpDaemon &http_daemon, std::function< void(CTcpDaemon &daemon)> OnWatchDog=nullptr)
Definition: tcp_daemon.cpp:687
void Start(struct uv_export_t *exp, unsigned short nworkers, CHttpDaemon &http_daemon, std::function< void(CTcpDaemon &daemon)> OnWatchDog=nullptr)
Definition: tcp_daemon.cpp:99
static void s_WorkerExecute(void *_worker)
Definition: tcp_daemon.cpp:82
bool AnyWorkerIsRunning(void)
Definition: tcp_daemon.cpp:124
std::vector< std::unique_ptr< CTcpWorker > > m_workers
Definition: tcp_daemon.hpp:63
static uv_key_t s_thread_worker_key
Definition: tcp_daemon.hpp:71
CTcpDaemon * m_daemon
Definition: tcp_daemon.hpp:64
static void s_OnWatchDog(uv_timer_t *handle)
Definition: tcp_daemon.cpp:140
void KillAll(void)
Definition: tcp_daemon.cpp:133
CTcpWorkersList(CTcpDaemon *daemon)
Definition: tcp_daemon.hpp:73
std::function< void(CTcpDaemon &daemon)> m_on_watch_dog
Definition: tcp_daemon.hpp:65
void JoinWorkers(void)
Definition: tcp_daemon.cpp:181
#define false
Definition: bool.h:36
#define daemon(d, c)
Definition: replacements.h:170
Uint2 uint16_t
string
Definition: cgiapp.hpp:687
#define nullptr
Definition: ncbimisc.hpp:45
#define PSG_MESSAGE(message)
uv_async_t m_async_stop
Definition: tcp_daemon.hpp:94
uv_async_t m_async_work
Definition: tcp_daemon.hpp:95
std::string m_last_error
Definition: tcp_daemon.hpp:124
CTcpDaemon * m_daemon
Definition: tcp_daemon.hpp:123
std::unique_ptr< CTcpWorkerInternal_t > m_internal
Definition: tcp_daemon.hpp:127
CTcpWorkersList * m_guard
Definition: tcp_daemon.hpp:122
std::atomic_bool m_shutdown
Definition: tcp_daemon.hpp:114
static void s_OnTcpConnection(uv_stream_t *listener, const int status)
Definition: tcp_daemon.cpp:469
CHttpDaemon & m_HttpDaemon
Definition: tcp_daemon.hpp:125
unsigned int m_id
Definition: tcp_daemon.hpp:109
void Execute(void)
Definition: tcp_daemon.cpp:221
void Stop(void)
Definition: tcp_daemon.cpp:203
void OnClientClosed(uv_handle_t *handle)
Definition: tcp_daemon.cpp:381
CTcpWorker(unsigned int id, struct uv_export_t *exp, CTcpDaemon *daemon, CTcpWorkersList *guard, CHttpDaemon &http_daemon)
Definition: tcp_daemon.hpp:129
void OnAsyncWork(void)
Definition: tcp_daemon.cpp:429
std::list< std::tuple< uv_tcp_t, CHttpConnection > > & GetConnList(void)
Definition: tcp_daemon.cpp:423
CHttpProto m_protocol
Definition: tcp_daemon.hpp:126
static void s_OnAsyncStop(uv_async_t *handle)
Definition: tcp_daemon.cpp:462
static void s_LoopWalk(uv_handle_t *handle, void *arg)
Definition: tcp_daemon.cpp:488
void CloseAll(void)
Definition: tcp_daemon.cpp:363
std::atomic_bool m_started
Definition: tcp_daemon.hpp:113
struct uv_export_t * m_exp
Definition: tcp_daemon.hpp:121
std::atomic_uint_fast64_t m_request_count
Definition: tcp_daemon.hpp:111
static void s_OnClientClosed(uv_handle_t *handle)
Definition: tcp_daemon.cpp:479
void WakeWorker(void)
Definition: tcp_daemon.cpp:415
static void s_OnTimer(uv_timer_t *handle)
Definition: tcp_daemon.cpp:453
std::list< std::tuple< uv_tcp_t, CHttpConnection > > m_connected_list
Definition: tcp_daemon.hpp:119
void OnTcpConnection(uv_stream_t *listener)
Definition: tcp_daemon.cpp:497
static void s_OnAsyncWork(uv_async_t *handle)
Definition: tcp_daemon.cpp:437
uv_thread_t m_thread
Definition: tcp_daemon.hpp:110
std::list< std::tuple< uv_tcp_t, CHttpConnection > > m_free_list
Definition: tcp_daemon.hpp:120
std::atomic_uint_fast16_t m_connection_count
Definition: tcp_daemon.hpp:112
void OnTimer(void)
Definition: tcp_daemon.cpp:447
std::atomic_bool m_shuttingdown
Definition: tcp_daemon.hpp:115
bool m_close_all_issued
Definition: tcp_daemon.hpp:116
USING_NCBI_SCOPE
Definition: tcp_daemon.hpp:51
Modified on Thu May 30 12:25:23 2024 by modify_doxy.py rev. 669887