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

Go to the SVN repository for this file.

1 #ifndef CONNECT___SERVER_CONNECTION__HPP
2 #define CONNECT___SERVER_CONNECTION__HPP
3 
4 /* $Id: server_connection.hpp 91806 2020-12-14 15:02:47Z grichenk $
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: Aaron Ucko, Victor Joukov
30  *
31  */
32 
33 /// @file server_connection.hpp
34 /// Internal header for threaded server connections.
35 
36 #include <connect/server.hpp>
37 #include <string>
38 
39 
40 /** @addtogroup ThreadedServer
41  *
42  * @{
43  */
44 
45 
47 
48 
57 };
59 
60 
62 
64 {
65 public:
67  virtual EIO_Event GetEventsToPollFor(const CTime** /*alarm_time*/) const
68  { return eIO_Read; }
70  CServer_ConnectionPool& connPool,
71  const STimeout* timeout) = 0;
72  virtual bool IsOpen(void) { return true; }
73  virtual bool IsReadyToProcess(void) { return true; }
74  virtual void OnTimeout(void) { }
75  virtual void OnTimer(void) { }
76  virtual void OnOverflow(EOverflowReason) { }
77  virtual void Activate(void) { }
78  virtual void Passivate(void) { }
79 
80 private:
81  friend class CServer_ConnectionPool;
82  friend class IServer_MessageHandler;
83 
87 };
88 
90  public CSocket // CPollable
91 {
92 public:
94  : m_Handler(handler), m_Open(true)
95  { m_Handler->SetSocket(this); }
96  virtual EIO_Event GetEventsToPollFor(const CTime** alarm_time) const
97  { return m_Handler->GetEventsToPollFor(alarm_time); }
99  CServer_ConnectionPool& connPool,
100  const STimeout* timeout);
101  virtual void OnTimeout(void) { m_Handler->OnTimeout(); }
102  virtual void OnOverflow(EOverflowReason reason) {
103  m_Handler->OnOverflow(reason);
104  }
105  virtual bool IsReadyToProcess(void) {
106  return m_Handler->IsReadyToProcess();
107  }
108  virtual bool IsOpen(void);
109  // connection-specific methods
110  void OnSocketEvent(EServIO_Event event);
111  virtual ~CServer_Connection();
112 private:
113  unique_ptr<IServer_ConnectionHandler> m_Handler;
114  bool m_Open;
115 };
116 
118  public CListeningSocket // CPollable
119 {
120 public:
121  CServer_Listener(IServer_ConnectionFactory* factory, unsigned short port)
122  : m_Factory(factory), m_Port(port)
123  { }
124  virtual CStdRequest* CreateRequest(EServIO_Event event,
125  CServer_ConnectionPool& connPool,
126  const STimeout* timeout);
127  virtual void Activate(void) {
128  EIO_Status st = GetStatus();
129  while (st != eIO_Success) {
130  // Set backlog to high enough value because Windows actually
131  // uses it and we have no reason not to buffer incoming connections
132  if ((st = Listen(m_Port, 128)) == eIO_Success) return;
134  m_Factory->OnFailure(&m_Port);
136  NCBI_THROW(CServer_Exception, eCouldntListen, "Port busy");
137  else if (action == IServer_ConnectionFactory::eLAIgnore)
138  return;
139  }
140  }
141  virtual void Passivate(void) { Close(); }
142  unsigned short GetPort(void) const { return m_Port; }
143 private:
144  friend class CAcceptRequest;
145  unique_ptr<IServer_ConnectionFactory> m_Factory;
146  unsigned short m_Port;
147 } ;
148 
149 
151 
152 
153 /* @} */
154 
155 #endif /* CONNECT___SERVER_CONNECTION__HPP */
CFastMutex –.
Definition: ncbimtx.hpp:667
CListeningSocket::
CServer_Exception::
Definition: server.hpp:465
CSocket::
CTime –.
Definition: ncbitime.hpp:296
IServer_ConnectionFactory::
Definition: server.hpp:388
IServer_ConnectionHandler::
Definition: server.hpp:226
IServer_MessageHandler::
Definition: server.hpp:313
void(*)(CSeq_entry_Handle seh, IWorkbench *wb, const CSerialObject &obj) handler
#define true
Definition: bool.h:35
string
Definition: cgiapp.hpp:687
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#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 GetStatus(void) const
Return eIO_Closed if not yet bound or Close()'d.
EIO_Status Close(void)
EIO_Status Listen(unsigned short port, unsigned short backlog=64, TSOCK_Flags flags=fSOCK_LogDefault)
unsigned short m_Port
virtual void OnTimeout(void)
EServIO_Event
Extended copy of the type EIO_Event allowing to distinguish between connection closing from client an...
Definition: server.hpp:61
std::string g_ServerConnTypeToString(enum EServerConnType conn_type)
virtual void Passivate(void)
virtual void OnTimer(void)
virtual void OnTimeout(void)
EListenAction
What to do if the port is busy.
Definition: server.hpp:391
CServer_Listener(IServer_ConnectionFactory *factory, unsigned short port)
virtual bool IsReadyToProcess(void)
virtual CStdRequest * CreateRequest(EServIO_Event event, CServer_ConnectionPool &connPool, const STimeout *timeout)
Definition: server.cpp:510
volatile EServerConnType type
EServerConnType
EOverflowReason
Error codes for OnOverflow method in IServer_ConnectionHandler.
Definition: server.hpp:210
unsigned short GetPort(void) const
virtual void Passivate(void)
virtual EIO_Event GetEventsToPollFor(const CTime **) const
virtual EIO_Event GetEventsToPollFor(const CTime **alarm_time) const
virtual void OnOverflow(EOverflowReason reason)
unique_ptr< IServer_ConnectionFactory > m_Factory
virtual void Activate(void)
virtual bool IsOpen(void)
virtual void Activate(void)
virtual CStdRequest * CreateRequest(EServIO_Event event, CServer_ConnectionPool &connPool, const STimeout *timeout)=0
unique_ptr< IServer_ConnectionHandler > m_Handler
virtual void OnOverflow(EOverflowReason)
CServer_Connection(IServer_ConnectionHandler *handler)
virtual bool IsReadyToProcess(void)
@ eClosedSocket
@ eActiveSocket
@ ePreDeferredSocket
@ eDeferredSocket
@ eInactiveSocket
@ ePreClosedSocket
@ eListener
EIO_Status
I/O status.
Definition: ncbi_core.h:132
EIO_Event
I/O event (or direction).
Definition: ncbi_core.h:118
@ eIO_Success
everything is fine, no error occurred
Definition: ncbi_core.h:133
@ eIO_Read
read
Definition: ncbi_core.h:120
#define NCBI_XCONNECT_EXPORT
Framework to create multithreaded network servers with thread-per-request scheduling.
Timeout structure.
Definition: ncbi_types.h:76
Modified on Mon Apr 22 04:05:29 2024 by modify_doxy.py rev. 669887