10 #ifndef __PION_TCP_SERVER_HEADER__ 11 #define __PION_TCP_SERVER_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/config.hpp> 20 #include <pion/logger.hpp> 21 #include <pion/scheduler.hpp> 22 #include <pion/tcp/connection.hpp> 33 private boost::noncopyable
38 virtual ~server() {
if (m_is_listening) stop(
false); }
48 void stop(
bool wait_until_finished =
false);
58 void set_ssl_key_file(
const std::string& pem_key_file);
61 std::size_t get_connections(
void)
const;
64 inline unsigned int get_port(
void)
const {
return m_endpoint.port(); }
67 inline void set_port(
unsigned int p) { m_endpoint.port(p); }
70 inline boost::asio::ip::address
get_address(
void)
const {
return m_endpoint.address(); }
73 inline void set_address(
const boost::asio::ip::address& addr) { m_endpoint.address(addr); }
76 inline const boost::asio::ip::tcp::endpoint&
get_endpoint(
void)
const {
return m_endpoint; }
79 inline void set_endpoint(
const boost::asio::ip::tcp::endpoint& ep) { m_endpoint = ep; }
100 inline boost::asio::ip::tcp::acceptor&
get_acceptor(
void) {
return m_tcp_acceptor; }
103 inline const boost::asio::ip::tcp::acceptor&
get_acceptor(
void)
const {
return m_tcp_acceptor; }
113 explicit server(
const unsigned int tcp_port);
120 explicit server(
const boost::asio::ip::tcp::endpoint& endpoint);
136 server(
scheduler& sched,
const boost::asio::ip::tcp::endpoint& endpoint);
145 tcp_conn->set_lifecycle(connection::LIFECYCLE_CLOSE);
156 inline boost::asio::io_service&
get_io_service(
void) {
return m_active_scheduler.get_io_service(); }
166 void handle_stop_request(
void);
177 void handle_accept(
const tcp::connection_ptr& tcp_conn,
178 const boost::system::error_code& accept_error);
186 void handle_ssl_handshake(
const tcp::connection_ptr& tcp_conn,
187 const boost::system::error_code& handshake_error);
193 void finish_connection(
const tcp::connection_ptr& tcp_conn);
197 std::size_t prune_connections(
void);
201 typedef std::set<tcp::connection_ptr> ConnectionPool;
211 boost::asio::ip::tcp::acceptor m_tcp_acceptor;
214 connection::ssl_context_type m_ssl_context;
217 boost::condition m_server_has_stopped;
220 boost::condition m_no_more_connections;
223 ConnectionPool m_conn_pool;
226 boost::asio::ip::tcp::endpoint m_endpoint;
235 mutable boost::mutex m_mutex;
240 typedef boost::shared_ptr<server> server_ptr;
connection::ssl_context_type & get_ssl_context_type(void)
returns the SSL context for configuration
void set_endpoint(const boost::asio::ip::tcp::endpoint &ep)
sets tcp endpoint that the server listens for connections on
bool get_ssl_flag(void) const
returns true if the server uses SSL to encrypt connections
bool is_listening(void) const
returns true if the server is listening for connections
boost::asio::ip::address get_address(void) const
returns IP address that the server listens for connections on
const boost::asio::ip::tcp::acceptor & get_acceptor(void) const
returns const reference to the TCP connection acceptor
boost::asio::ip::tcp::acceptor & get_acceptor(void)
returns mutable reference to the TCP connection acceptor
boost::asio::io_service & get_io_service(void)
returns an async I/O service used to schedule work
const boost::asio::ip::tcp::endpoint & get_endpoint(void) const
returns tcp endpoint that the server listens for connections on
void set_logger(logger log_ptr)
sets the logger to be used
virtual void handle_connection(const tcp::connection_ptr &tcp_conn)
unsigned int get_port(void) const
returns tcp port number that the server listens for connections on
virtual void before_starting(void)
called before the TCP server starts listening for new connections
void set_ssl_flag(bool b=true)
sets value of SSL flag (true if the server uses SSL to encrypt connections)
virtual void after_stopping(void)
called after the TCP server has stopped listing for new connections
logger m_logger
primary logging interface used by this class
void set_address(const boost::asio::ip::address &addr)
sets IP address that the server listens for connections on
void set_port(unsigned int p)
sets tcp port number that the server listens for connections on
logger get_logger(void)
returns the logger currently in use
virtual ~server()
default destructor