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

Go to the SVN repository for this file.

1 #ifndef CONNECT___THREADED_SERVER__HPP
2 #define CONNECT___THREADED_SERVER__HPP
3 
4 /* $Id: threaded_server.hpp 84663 2018-11-27 18:22:00Z ucko $
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  * Author: Aaron Ucko
30  *
31  * File Description:
32  * Framework for a multithreaded network server
33  *
34  */
35 
38 #include <connect/ncbi_socket.hpp>
39 
40 
41 /** @addtogroup ThreadedServer
42  *
43  * @{
44  */
45 
46 
48 
49 
50 /// Exceptions thrown by CThreadedServer::Run
53 {
54 public:
55  enum EErrCode {
56  eBadParameters, ///< Out-of-range parameters given
57  eCouldntListen ///< Unable to bind listening port
58  };
59  virtual const char* GetErrCodeString(void) const override;
61 };
62 
63 
64 /// CThreadedServer - abstract class for network servers using thread pools.
65 /// This code maintains a pool of threads (initially m_InitThreads, but
66 /// potentially as many as m_MaxThreads) to deal with incoming connections;
67 /// each connection gets assigned to one of the worker threads, allowing
68 /// the server to handle multiple requests in parallel while still checking
69 /// for new requests.
70 ///
71 /// You must define Process() to indicate what to do with each incoming
72 /// connection; .../src/connect/test_threaded_server.cpp illustrates
73 /// how you might do this.
74 ///
75 /// @deprecated Use CServer instead.
76 
78  : protected CConnIniter
79 {
80 public:
81  CThreadedServer(unsigned short port) :
85 
86  virtual ~CThreadedServer() { }
87 
88  /// Enter the main loop.
89  void Run(void);
90 
91  /// Start listening immediately, or throw an exception if it is
92  /// impossible to do so. (Does nothing if *this* object is
93  /// already listening on the port.) Calling StartListening()
94  /// before Run() will permit detecting port-in-use problems before
95  /// the last minute. (On the other hand, clients that attempt to
96  /// connect in the interim will get no response until the main
97  /// loop actually starts.)
98  void StartListening(void);
99 
100  /// Runs asynchronously (from a separate thread) for each request.
101  /// Implementor must take care of closing the socket when done.
102  /// (Using it as the basis of a CConn_SocketStream object will do
103  /// so automatically.)
104  virtual void Process(SOCK sock) = 0;
105 
106  /// Get the listening port number back.
107  unsigned short GetPort() const { return m_Port; }
108 
109 protected:
110  /// Runs synchronously when request queue is full.
111  /// Implementor must take care of closing socket when done.
112  virtual void ProcessOverflow(SOCK sock) { SOCK_Close(sock); }
113 
114  /// Runs synchronously when accept has timed out.
115  virtual void ProcessTimeout(void) {}
116 
117  /// Runs synchronously between iterations.
118  virtual bool ShutdownRequested(void) { return false; }
119 
120  /// Called at the beginning of Run, before creating thread pool.
121  virtual void SetParams() {}
122 
123  /// Settings for thread pool (which is local to Run):
124 
125  unsigned int m_InitThreads; ///< Number of initial threads
126  unsigned int m_MaxThreads; ///< Maximum simultaneous threads
127  unsigned int m_QueueSize; ///< Maximum size of request queue
128  unsigned int m_SpawnThreshold; ///< Controls when to spawn more threads
129  const STimeout* m_AcceptTimeout; ///< Maximum time between exit checks
130 
131  /// Temporarily close listener when queue fills?
133 
134 private:
135  unsigned short m_Port; ///< TCP port to listen on
136  CListeningSocket m_LSock; ///< Listening socket
137 };
138 
139 
141 
142 
143 /* @} */
144 
145 #endif /* CONNECT___THREADED_SERVER__HPP */
Generic CONN exception.
Helper hook-up class that installs default logging/registry/locking (but only if they have not yet be...
CListeningSocket::
Exceptions thrown by CThreadedServer::Run.
#define NCBI_DEPRECATED_CLASS
Macro used to mark a class as deprecated.
Definition: ncbimisc.hpp:1215
virtual const char * GetErrCodeString(void) const override
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
EIO_Status SOCK_Close(SOCK sock)
Close the SOCK handle, and destroy all relevant internal data.
Definition: ncbi_socket.c:6851
CListeningSocket m_LSock
Listening socket.
unsigned short GetPort() const
Get the listening port number back.
NCBI_DEPRECATED_CLASS NCBI_XCONNECT_EXPORT CThreadedServer
CThreadedServer - abstract class for network servers using thread pools.
const STimeout * m_AcceptTimeout
Maximum time between exit checks.
unsigned int m_QueueSize
Maximum size of request queue.
void Run(void)
Enter the main loop.
unsigned short m_Port
TCP port to listen on.
unsigned int m_InitThreads
Settings for thread pool (which is local to Run):
void StartListening(void)
Start listening immediately, or throw an exception if it is impossible to do so.
virtual ~CThreadedServer()
bool m_TemporarilyStopListening
Temporarily close listener when queue fills?
virtual bool ShutdownRequested(void)
Runs synchronously between iterations.
virtual void ProcessOverflow(SOCK sock)
Runs synchronously when request queue is full.
NCBI_EXCEPTION_DEFAULT(CThreadedServerException, CConnException)
virtual void Process(SOCK sock)=0
Runs asynchronously (from a separate thread) for each request.
virtual void ProcessTimeout(void)
Runs synchronously when accept has timed out.
unsigned int m_MaxThreads
Maximum simultaneous threads.
virtual void SetParams()
Called at the beginning of Run, before creating thread pool.
unsigned int m_SpawnThreshold
Controls when to spawn more threads.
@ eBadParameters
Out-of-range parameters given.
#define kInfiniteTimeout
Definition: ncbi_types.h:82
#define NCBI_XCONNECT_EXPORT
Timeout structure.
Definition: ncbi_types.h:76
Modified on Wed Jun 12 11:15:26 2024 by modify_doxy.py rev. 669887