10 #ifndef __PION_TCPSERVER_HEADER__
11 #define __PION_TCPSERVER_HEADER__
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>
32 private boost::noncopyable
37 virtual ~TCPServer() {
if (m_is_listening) stop(
false); }
47 void stop(
bool wait_until_finished =
false);
57 void setSSLKeyFile(
const std::string& pem_key_file);
60 std::size_t getConnections(
void)
const;
63 inline unsigned int getPort(
void)
const {
return m_endpoint.port(); }
66 inline void setPort(
unsigned int p) { m_endpoint.port(p); }
69 inline boost::asio::ip::address
getAddress(
void)
const {
return m_endpoint.address(); }
72 inline void setAddress(
const boost::asio::ip::address& addr) { m_endpoint.address(addr); }
75 inline const boost::asio::ip::tcp::endpoint&
getEndpoint(
void)
const {
return m_endpoint; }
78 inline void setEndpoint(
const boost::asio::ip::tcp::endpoint& ep) { m_endpoint = ep; }
81 inline bool getSSLFlag(
void)
const {
return m_ssl_flag; }
84 inline void setSSLFlag(
bool b =
true) { m_ssl_flag = b; }
87 inline TCPConnection::SSLContext&
getSSLContext(
void) {
return m_ssl_context; }
90 inline bool isListening(
void)
const {
return m_is_listening; }
106 explicit TCPServer(
const unsigned int tcp_port);
113 explicit TCPServer(
const boost::asio::ip::tcp::endpoint& endpoint);
138 tcp_conn->setLifecycle(TCPConnection::LIFECYCLE_CLOSE);
149 inline boost::asio::io_service&
getIOService(
void) {
return m_active_scheduler.getIOService(); }
159 void handleStopRequest(
void);
170 void handleAccept(TCPConnectionPtr& tcp_conn,
171 const boost::system::error_code& accept_error);
179 void handleSSLHandshake(TCPConnectionPtr& tcp_conn,
180 const boost::system::error_code& handshake_error);
186 void finishConnection(TCPConnectionPtr& tcp_conn);
190 std::size_t pruneConnections(
void);
193 typedef std::set<TCPConnectionPtr> ConnectionPool;
203 boost::asio::ip::tcp::acceptor m_tcp_acceptor;
206 TCPConnection::SSLContext m_ssl_context;
209 boost::condition m_server_has_stopped;
212 boost::condition m_no_more_connections;
215 ConnectionPool m_conn_pool;
218 boost::asio::ip::tcp::endpoint m_endpoint;
227 mutable boost::mutex m_mutex;
232 typedef boost::shared_ptr<TCPServer> TCPServerPtr;
virtual void beforeStarting(void)
called before the TCP server starts listening for new connections
void setAddress(const boost::asio::ip::address &addr)
sets IP address that the server listens for connections on
virtual void afterStopping(void)
called after the TCP server has stopped listing for new connections
void setPort(unsigned int p)
sets tcp port number that the server listens for connections on
bool getSSLFlag(void) const
returns true if the server uses SSL to encrypt connections
boost::asio::ip::address getAddress(void) const
returns IP address that the server listens for connections on
virtual ~TCPServer()
default destructor
virtual void handleConnection(TCPConnectionPtr &tcp_conn)
bool isListening(void) const
returns true if the server is listening for connections
PionLogger m_logger
primary logging interface used by this class
unsigned int getPort(void) const
returns tcp port number that the server listens for connections on
the following enables use of the lock-free cache
void setLogger(PionLogger log_ptr)
sets the logger to be used
TCPConnection::SSLContext & getSSLContext(void)
returns the SSL context for configuration
PionLogger getLogger(void)
returns the logger currently in use
const boost::asio::ip::tcp::endpoint & getEndpoint(void) const
returns tcp endpoint that the server listens for connections on
boost::asio::io_service & getIOService(void)
returns an async I/O service used to schedule work
void setEndpoint(const boost::asio::ip::tcp::endpoint &ep)
sets tcp endpoint that the server listens for connections on
void setSSLFlag(bool b=true)
sets value of SSL flag (true if the server uses SSL to encrypt connections)