pion  5.0.6
server.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
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_TCP_SERVER_HEADER__
11 #define __PION_TCP_SERVER_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/config.hpp>
20 #include <pion/logger.hpp>
21 #include <pion/scheduler.hpp>
22 #include <pion/tcp/connection.hpp>
23 
24 
25 namespace pion { // begin namespace pion
26 namespace tcp { // begin namespace tcp
27 
28 
32 class PION_API server :
33  private boost::noncopyable
34 {
35 public:
36 
38  virtual ~server() { if (m_is_listening) stop(false); }
39 
41  void start(void);
42 
48  void stop(bool wait_until_finished = false);
49 
51  void join(void);
52 
58  void set_ssl_key_file(const std::string& pem_key_file);
59 
61  std::size_t get_connections(void) const;
62 
64  inline unsigned int get_port(void) const { return m_endpoint.port(); }
65 
67  inline void set_port(unsigned int p) { m_endpoint.port(p); }
68 
70  inline boost::asio::ip::address get_address(void) const { return m_endpoint.address(); }
71 
73  inline void set_address(const boost::asio::ip::address& addr) { m_endpoint.address(addr); }
74 
76  inline const boost::asio::ip::tcp::endpoint& get_endpoint(void) const { return m_endpoint; }
77 
79  inline void set_endpoint(const boost::asio::ip::tcp::endpoint& ep) { m_endpoint = ep; }
80 
82  inline bool get_ssl_flag(void) const { return m_ssl_flag; }
83 
85  inline void set_ssl_flag(bool b = true) { m_ssl_flag = b; }
86 
88  inline connection::ssl_context_type& get_ssl_context_type(void) { return m_ssl_context; }
89 
91  inline bool is_listening(void) const { return m_is_listening; }
92 
94  inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
95 
97  inline logger get_logger(void) { return m_logger; }
98 
100  inline boost::asio::ip::tcp::acceptor& get_acceptor(void) { return m_tcp_acceptor; }
101 
103  inline const boost::asio::ip::tcp::acceptor& get_acceptor(void) const { return m_tcp_acceptor; }
104 
105 
106 protected:
107 
113  explicit server(const unsigned int tcp_port);
114 
120  explicit server(const boost::asio::ip::tcp::endpoint& endpoint);
121 
128  explicit server(scheduler& sched, const unsigned int tcp_port = 0);
129 
136  server(scheduler& sched, const boost::asio::ip::tcp::endpoint& endpoint);
137 
144  virtual void handle_connection(tcp::connection_ptr& tcp_conn) {
145  tcp_conn->set_lifecycle(connection::LIFECYCLE_CLOSE); // make sure it will get closed
146  tcp_conn->finish();
147  }
148 
150  virtual void before_starting(void) {}
151 
153  virtual void after_stopping(void) {}
154 
156  inline boost::asio::io_service& get_io_service(void) { return m_active_scheduler.get_io_service(); }
157 
158 
161 
162 
163 private:
164 
166  void handle_stop_request(void);
167 
169  void listen(void);
170 
177  void handle_accept(tcp::connection_ptr& tcp_conn,
178  const boost::system::error_code& accept_error);
179 
186  void handle_ssl_handshake(tcp::connection_ptr& tcp_conn,
187  const boost::system::error_code& handshake_error);
188 
193  void finish_connection(tcp::connection_ptr& tcp_conn);
194 
197  std::size_t prune_connections(void);
198 
199 
201  typedef std::set<tcp::connection_ptr> ConnectionPool;
202 
203 
205  single_service_scheduler m_default_scheduler;
206 
208  scheduler & m_active_scheduler;
209 
211  boost::asio::ip::tcp::acceptor m_tcp_acceptor;
212 
214  connection::ssl_context_type m_ssl_context;
215 
217  boost::condition m_server_has_stopped;
218 
220  boost::condition m_no_more_connections;
221 
223  ConnectionPool m_conn_pool;
224 
226  boost::asio::ip::tcp::endpoint m_endpoint;
227 
229  bool m_ssl_flag;
230 
232  bool m_is_listening;
233 
235  mutable boost::mutex m_mutex;
236 };
237 
238 
240 typedef boost::shared_ptr<server> server_ptr;
241 
242 
243 } // end namespace tcp
244 } // end namespace pion
245 
246 #endif
connection::ssl_context_type & get_ssl_context_type(void)
returns the SSL context for configuration
Definition: server.hpp:88
bool get_ssl_flag(void) const
returns true if the server uses SSL to encrypt connections
Definition: server.hpp:82
void set_endpoint(const boost::asio::ip::tcp::endpoint &ep)
sets tcp endpoint that the server listens for connections on
Definition: server.hpp:79
const boost::asio::ip::tcp::acceptor & get_acceptor(void) const
returns const reference to the TCP connection acceptor
Definition: server.hpp:103
boost::asio::ip::address get_address(void) const
returns IP address that the server listens for connections on
Definition: server.hpp:70
unsigned int get_port(void) const
returns tcp port number that the server listens for connections on
Definition: server.hpp:64
boost::asio::ip::tcp::acceptor & get_acceptor(void)
returns mutable reference to the TCP connection acceptor
Definition: server.hpp:100
boost::asio::io_service & get_io_service(void)
returns an async I/O service used to schedule work
Definition: server.hpp:156
void set_logger(logger log_ptr)
sets the logger to be used
Definition: server.hpp:94
virtual void before_starting(void)
called before the TCP server starts listening for new connections
Definition: server.hpp:150
void set_ssl_flag(bool b=true)
sets value of SSL flag (true if the server uses SSL to encrypt connections)
Definition: server.hpp:85
virtual void after_stopping(void)
called after the TCP server has stopped listing for new connections
Definition: server.hpp:153
bool is_listening(void) const
returns true if the server is listening for connections
Definition: server.hpp:91
logger m_logger
primary logging interface used by this class
Definition: server.hpp:160
void set_address(const boost::asio::ip::address &addr)
sets IP address that the server listens for connections on
Definition: server.hpp:73
void set_port(unsigned int p)
sets tcp port number that the server listens for connections on
Definition: server.hpp:67
logger get_logger(void)
returns the logger currently in use
Definition: server.hpp:97
virtual ~server()
default destructor
Definition: server.hpp:38
virtual void handle_connection(tcp::connection_ptr &tcp_conn)
Definition: server.hpp:144
const boost::asio::ip::tcp::endpoint & get_endpoint(void) const
returns tcp endpoint that the server listens for connections on
Definition: server.hpp:76