pion-net  4.0.9
TCPServer.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_TCPSERVER_HEADER__
11 #define __PION_TCPSERVER_HEADER__
12 
13 #include <set>
14 #include <boost/asio.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/thread/mutex.hpp>
18 #include <boost/thread/condition.hpp>
19 #include <pion/PionConfig.hpp>
20 #include <pion/PionLogger.hpp>
21 #include <pion/PionScheduler.hpp>
22 #include <pion/net/TCPConnection.hpp>
23 
24 
25 namespace pion { // begin namespace pion
26 namespace net { // begin namespace net (Pion Network Library)
27 
31 class PION_NET_API TCPServer :
32  private boost::noncopyable
33 {
34 public:
35 
37  virtual ~TCPServer() { if (m_is_listening) stop(false); }
38 
40  void start(void);
41 
47  void stop(bool wait_until_finished = false);
48 
50  void join(void);
51 
57  void setSSLKeyFile(const std::string& pem_key_file);
58 
60  std::size_t getConnections(void) const;
61 
63  inline unsigned int getPort(void) const { return m_endpoint.port(); }
64 
66  inline void setPort(unsigned int p) { m_endpoint.port(p); }
67 
69  inline boost::asio::ip::address getAddress(void) const { return m_endpoint.address(); }
70 
72  inline void setAddress(const boost::asio::ip::address& addr) { m_endpoint.address(addr); }
73 
75  inline const boost::asio::ip::tcp::endpoint& getEndpoint(void) const { return m_endpoint; }
76 
78  inline void setEndpoint(const boost::asio::ip::tcp::endpoint& ep) { m_endpoint = ep; }
79 
81  inline bool getSSLFlag(void) const { return m_ssl_flag; }
82 
84  inline void setSSLFlag(bool b = true) { m_ssl_flag = b; }
85 
87  inline TCPConnection::SSLContext& getSSLContext(void) { return m_ssl_context; }
88 
90  inline bool isListening(void) const { return m_is_listening; }
91 
93  inline void setLogger(PionLogger log_ptr) { m_logger = log_ptr; }
94 
96  inline PionLogger getLogger(void) { return m_logger; }
97 
98 
99 protected:
100 
106  explicit TCPServer(const unsigned int tcp_port);
107 
113  explicit TCPServer(const boost::asio::ip::tcp::endpoint& endpoint);
114 
121  explicit TCPServer(PionScheduler& scheduler, const unsigned int tcp_port = 0);
122 
129  TCPServer(PionScheduler& scheduler, const boost::asio::ip::tcp::endpoint& endpoint);
130 
137  virtual void handleConnection(TCPConnectionPtr& tcp_conn) {
138  tcp_conn->setLifecycle(TCPConnection::LIFECYCLE_CLOSE); // make sure it will get closed
139  tcp_conn->finish();
140  }
141 
143  virtual void beforeStarting(void) {}
144 
146  virtual void afterStopping(void) {}
147 
149  inline boost::asio::io_service& getIOService(void) { return m_active_scheduler.getIOService(); }
150 
151 
154 
155 
156 private:
157 
159  void handleStopRequest(void);
160 
162  void listen(void);
163 
170  void handleAccept(TCPConnectionPtr& tcp_conn,
171  const boost::system::error_code& accept_error);
172 
179  void handleSSLHandshake(TCPConnectionPtr& tcp_conn,
180  const boost::system::error_code& handshake_error);
181 
186  void finishConnection(TCPConnectionPtr& tcp_conn);
187 
190  std::size_t pruneConnections(void);
191 
193  typedef std::set<TCPConnectionPtr> ConnectionPool;
194 
195 
197  PionSingleServiceScheduler m_default_scheduler;
198 
200  PionScheduler & m_active_scheduler;
201 
203  boost::asio::ip::tcp::acceptor m_tcp_acceptor;
204 
206  TCPConnection::SSLContext m_ssl_context;
207 
209  boost::condition m_server_has_stopped;
210 
212  boost::condition m_no_more_connections;
213 
215  ConnectionPool m_conn_pool;
216 
218  boost::asio::ip::tcp::endpoint m_endpoint;
219 
221  bool m_ssl_flag;
222 
224  bool m_is_listening;
225 
227  mutable boost::mutex m_mutex;
228 };
229 
230 
232 typedef boost::shared_ptr<TCPServer> TCPServerPtr;
233 
234 
235 } // end namespace net
236 } // end namespace pion
237 
238 #endif
virtual void beforeStarting(void)
called before the TCP server starts listening for new connections
Definition: TCPServer.hpp:143
void setAddress(const boost::asio::ip::address &addr)
sets IP address that the server listens for connections on
Definition: TCPServer.hpp:72
virtual void afterStopping(void)
called after the TCP server has stopped listing for new connections
Definition: TCPServer.hpp:146
void setPort(unsigned int p)
sets tcp port number that the server listens for connections on
Definition: TCPServer.hpp:66
bool getSSLFlag(void) const
returns true if the server uses SSL to encrypt connections
Definition: TCPServer.hpp:81
boost::asio::ip::address getAddress(void) const
returns IP address that the server listens for connections on
Definition: TCPServer.hpp:69
virtual ~TCPServer()
default destructor
Definition: TCPServer.hpp:37
virtual void handleConnection(TCPConnectionPtr &tcp_conn)
Definition: TCPServer.hpp:137
bool isListening(void) const
returns true if the server is listening for connections
Definition: TCPServer.hpp:90
PionLogger m_logger
primary logging interface used by this class
Definition: TCPServer.hpp:153
unsigned int getPort(void) const
returns tcp port number that the server listens for connections on
Definition: TCPServer.hpp:63
the following enables use of the lock-free cache
void setLogger(PionLogger log_ptr)
sets the logger to be used
Definition: TCPServer.hpp:93
TCPConnection::SSLContext & getSSLContext(void)
returns the SSL context for configuration
Definition: TCPServer.hpp:87
PionLogger getLogger(void)
returns the logger currently in use
Definition: TCPServer.hpp:96
const boost::asio::ip::tcp::endpoint & getEndpoint(void) const
returns tcp endpoint that the server listens for connections on
Definition: TCPServer.hpp:75
boost::asio::io_service & getIOService(void)
returns an async I/O service used to schedule work
Definition: TCPServer.hpp:149
void setEndpoint(const boost::asio::ip::tcp::endpoint &ep)
sets tcp endpoint that the server listens for connections on
Definition: TCPServer.hpp:78
void setSSLFlag(bool b=true)
sets value of SSL flag (true if the server uses SSL to encrypt connections)
Definition: TCPServer.hpp:84