PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/labo/2009.08.17/poco-1.3.5-mingw/Net/include/Poco/Net/TCPServer.h

http://mingw-lib.googlecode.com/
C++ Header | 200 lines | 46 code | 32 blank | 122 comment | 0 complexity | ddaa851f8f48b9ce6a70fa485f36cde5 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. //
  2. // TCPServer.h
  3. //
  4. // $Id: //poco/1.3/Net/include/Poco/Net/TCPServer.h#4 $
  5. //
  6. // Library: Net
  7. // Package: TCPServer
  8. // Module: TCPServer
  9. //
  10. // Definition of the TCPServer class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Net_TCPServer_INCLUDED
  38. #define Net_TCPServer_INCLUDED
  39. #include "Poco/Net/Net.h"
  40. #include "Poco/Net/ServerSocket.h"
  41. #include "Poco/Net/TCPServerConnectionFactory.h"
  42. #include "Poco/Net/TCPServerParams.h"
  43. #include "Poco/Runnable.h"
  44. #include "Poco/Thread.h"
  45. #include "Poco/ThreadPool.h"
  46. namespace Poco {
  47. namespace Net {
  48. class TCPServerDispatcher;
  49. class Net_API TCPServer: public Poco::Runnable
  50. /// This class implements a multithreaded TCP server.
  51. ///
  52. /// The server uses a ServerSocket to listen for incoming
  53. /// connections. The ServerSocket must have been bound to
  54. /// an address before it is passed to the TCPServer constructor.
  55. /// Additionally, the ServerSocket must be put into listening
  56. /// state before the TCPServer is started by calling the start()
  57. /// method.
  58. ///
  59. /// The server uses a thread pool to assign threads to incoming
  60. /// connections. Before incoming connections are assigned to
  61. /// a connection thread, they are put into a queue.
  62. /// Connection threads fetch new connections from the queue as soon
  63. /// as they become free. Thus, a connection thread may serve more
  64. /// than one connection.
  65. ///
  66. /// As soon as a connection thread fetches the next connection from
  67. /// the queue, it creates a TCPServerConnection object for it
  68. /// (using the TCPServerConnectionFactory passed to the constructor)
  69. /// and calls the TCPServerConnection's start() method. When the
  70. /// start() method returns, the connection object is deleted.
  71. ///
  72. /// The number of connection threads is adjusted dynamically, depending
  73. /// on the number of connections waiting to be served.
  74. ///
  75. /// It is possible to specify a maximum number of queued connections.
  76. /// This prevents the connection queue from overflowing in the
  77. /// case of an extreme server load. In such a case, connections that
  78. /// cannot be queued are silently and immediately closed.
  79. ///
  80. /// TCPServer uses a separate thread to accept incoming connections.
  81. /// Thus, the call to start() returns immediately, and the server
  82. /// continues to run in the background.
  83. ///
  84. /// To stop the server from accepting new connections, call stop().
  85. ///
  86. /// After calling stop(), no new connections will be accepted and
  87. /// all queued connections will be discarded.
  88. /// Already served connections, however, will continue being served.
  89. {
  90. public:
  91. TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams = 0);
  92. /// Creates the TCPServer, using the given ServerSocket.
  93. ///
  94. /// The server takes ownership of the TCPServerConnectionFactory
  95. /// and deletes it when it's no longer needed.
  96. ///
  97. /// The server also takes ownership of the TCPServerParams object.
  98. /// If no TCPServerParams object is given, the server's TCPServerDispatcher
  99. /// creates its own one.
  100. ///
  101. /// News threads are taken from the default thread pool.
  102. TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::ThreadPool& threadPool, const ServerSocket& socket, TCPServerParams::Ptr pParams = 0);
  103. /// Creates the TCPServer, using the given ServerSocket.
  104. ///
  105. /// The server takes ownership of the TCPServerConnectionFactory
  106. /// and deletes it when it's no longer needed.
  107. ///
  108. /// The server also takes ownership of the TCPServerParams object.
  109. /// If no TCPServerParams object is given, the server's TCPServerDispatcher
  110. /// creates its own one.
  111. ///
  112. /// News threads are taken from the given thread pool.
  113. virtual ~TCPServer();
  114. /// Destroys the TCPServer and its TCPServerConnectionFactory.
  115. const TCPServerParams& params() const;
  116. /// Returns a const reference to the TCPServerParam object
  117. /// used by the server's TCPServerDispatcher.
  118. void start();
  119. /// Starts the server. A new thread will be
  120. /// created that waits for and accepts incoming
  121. /// connections.
  122. ///
  123. /// Before start() is called, the ServerSocket passed to
  124. /// TCPServer must have been bound and put into listening state.
  125. void stop();
  126. /// Stops the server.
  127. ///
  128. /// No new connections will be accepted.
  129. /// Already handled connections will continue to be served.
  130. ///
  131. /// Once the server is stopped, it cannot be restarted.
  132. int currentThreads() const;
  133. /// Returns the number of currently used connection threads.
  134. int totalConnections() const;
  135. /// Returns the total number of handled connections.
  136. int currentConnections() const;
  137. /// Returns the number of currently handled connections.
  138. int maxConcurrentConnections() const;
  139. /// Returns the maximum number of concurrently handled connections.
  140. int queuedConnections() const;
  141. /// Returns the number of queued connections.
  142. int refusedConnections() const;
  143. /// Returns the number of refused connections.
  144. Poco::UInt16 port() const;
  145. /// Returns the port the server socket listens to
  146. protected:
  147. void run();
  148. /// Runs the server. The server will run until
  149. /// the stop() method is called, or the server
  150. /// object is destroyed, which implicitly calls
  151. /// the stop() method.
  152. static std::string threadName(const ServerSocket& socket);
  153. /// Returns a thread name for the server thread.
  154. private:
  155. TCPServer();
  156. TCPServer(const TCPServer&);
  157. TCPServer& operator = (const TCPServer&);
  158. ServerSocket _socket;
  159. TCPServerDispatcher* _pDispatcher;
  160. Poco::Thread _thread;
  161. bool _stopped;
  162. };
  163. inline Poco::UInt16 TCPServer::port() const
  164. {
  165. return _socket.address().port();
  166. }
  167. } } // namespace Poco::Net
  168. #endif // Net_TCPServer_INCLUDED