server.hpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_TCP_SERVER_HEADER__
#define __PION_TCP_SERVER_HEADER__
#include <set>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <pion/config.hpp>
#include <pion/logger.hpp>
#include <pion/scheduler.hpp>
#include <pion/tcp/connection.hpp>
namespace pion { // begin namespace pion
namespace tcp { // begin namespace tcp
class PION_API server :
private boost::noncopyable
{
public:
virtual ~server() { if (m_is_listening) stop(false); }
void start(void);
void stop(bool wait_until_finished = false);
void join(void);
void set_ssl_key_file(const std::string& pem_key_file);
std::size_t get_connections(void) const;
inline unsigned int get_port(void) const { return m_endpoint.port(); }
inline void set_port(unsigned int p) { m_endpoint.port(p); }
inline boost::asio::ip::address get_address(void) const { return m_endpoint.address(); }
inline void set_address(const boost::asio::ip::address& addr) { m_endpoint.address(addr); }
inline const boost::asio::ip::tcp::endpoint& get_endpoint(void) const { return m_endpoint; }
inline void set_endpoint(const boost::asio::ip::tcp::endpoint& ep) { m_endpoint = ep; }
inline bool get_ssl_flag(void) const { return m_ssl_flag; }
inline void set_ssl_flag(bool b = true) { m_ssl_flag = b; }
inline connection::ssl_context_type& get_ssl_context_type(void) { return m_ssl_context; }
inline bool is_listening(void) const { return m_is_listening; }
inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
inline logger get_logger(void) { return m_logger; }
inline boost::asio::ip::tcp::acceptor& get_acceptor(void) { return m_tcp_acceptor; }
inline const boost::asio::ip::tcp::acceptor& get_acceptor(void) const { return m_tcp_acceptor; }
protected:
explicit server(const unsigned int tcp_port);
explicit server(const boost::asio::ip::tcp::endpoint& endpoint);
explicit server(scheduler& sched, const unsigned int tcp_port = 0);
server(scheduler& sched, const boost::asio::ip::tcp::endpoint& endpoint);
virtual void handle_connection(const tcp::connection_ptr& tcp_conn) {
tcp_conn->set_lifecycle(connection::LIFECYCLE_CLOSE); // make sure it will get closed
tcp_conn->finish();
}
virtual void before_starting(void) {}
virtual void after_stopping(void) {}
inline boost::asio::io_service& get_io_service(void) { return m_active_scheduler.get_io_service(); }
logger m_logger;
private:
void handle_stop_request(void);
void listen(void);
void handle_accept(const tcp::connection_ptr& tcp_conn,
const boost::system::error_code& accept_error);
void handle_ssl_handshake(const tcp::connection_ptr& tcp_conn,
const boost::system::error_code& handshake_error);
void finish_connection(const tcp::connection_ptr& tcp_conn);
std::size_t prune_connections(void);
typedef std::set<tcp::connection_ptr> ConnectionPool;
single_service_scheduler m_default_scheduler;
scheduler & m_active_scheduler;
boost::asio::ip::tcp::acceptor m_tcp_acceptor;
connection::ssl_context_type m_ssl_context;
boost::condition m_server_has_stopped;
boost::condition m_no_more_connections;
ConnectionPool m_conn_pool;
boost::asio::ip::tcp::endpoint m_endpoint;
bool m_ssl_flag;
bool m_is_listening;
mutable boost::mutex m_mutex;
};
typedef boost::shared_ptr<server> server_ptr;
} // end namespace tcp
} // end namespace pion
#endif
pion::tcp::server::get_ssl_context_type
connection::ssl_context_type & get_ssl_context_type(void)
returns the SSL context for configuration
Definition: server.hpp:88
pion::tcp::server::set_endpoint
void set_endpoint(const boost::asio::ip::tcp::endpoint &ep)
sets tcp endpoint that the server listens for connections on
Definition: server.hpp:79
pion::single_service_scheduler
Definition: scheduler.hpp:246
pion::tcp::server
Definition: server.hpp:32
pion::tcp::server::get_ssl_flag
bool get_ssl_flag(void) const
returns true if the server uses SSL to encrypt connections
Definition: server.hpp:82
pion::tcp::server::is_listening
bool is_listening(void) const
returns true if the server is listening for connections
Definition: server.hpp:91
pion::tcp::server::get_address
boost::asio::ip::address get_address(void) const
returns IP address that the server listens for connections on
Definition: server.hpp:70
pion::tcp::server::get_acceptor
const boost::asio::ip::tcp::acceptor & get_acceptor(void) const
returns const reference to the TCP connection acceptor
Definition: server.hpp:103
pion::tcp::server::get_acceptor
boost::asio::ip::tcp::acceptor & get_acceptor(void)
returns mutable reference to the TCP connection acceptor
Definition: server.hpp:100
pion::tcp::server::get_io_service
boost::asio::io_service & get_io_service(void)
returns an async I/O service used to schedule work
Definition: server.hpp:156
pion::tcp::server::get_endpoint
const boost::asio::ip::tcp::endpoint & get_endpoint(void) const
returns tcp endpoint that the server listens for connections on
Definition: server.hpp:76
pion::tcp::server::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: server.hpp:94
pion::tcp::server::handle_connection
virtual void handle_connection(const tcp::connection_ptr &tcp_conn)
Definition: server.hpp:144
pion::tcp::server::get_port
unsigned int get_port(void) const
returns tcp port number that the server listens for connections on
Definition: server.hpp:64
pion::tcp::server::before_starting
virtual void before_starting(void)
called before the TCP server starts listening for new connections
Definition: server.hpp:150
pion::logger
Definition: logger.hpp:238
pion::tcp::server::set_ssl_flag
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
pion::tcp::server::after_stopping
virtual void after_stopping(void)
called after the TCP server has stopped listing for new connections
Definition: server.hpp:153
pion
Definition: admin_rights.hpp:19
pion::tcp::server::m_logger
logger m_logger
primary logging interface used by this class
Definition: server.hpp:160
pion::tcp::server::set_address
void set_address(const boost::asio::ip::address &addr)
sets IP address that the server listens for connections on
Definition: server.hpp:73
pion::tcp::server::set_port
void set_port(unsigned int p)
sets tcp port number that the server listens for connections on
Definition: server.hpp:67
pion::tcp::server::get_logger
logger get_logger(void)
returns the logger currently in use
Definition: server.hpp:97
pion::tcp::server::~server
virtual ~server()
default destructor
Definition: server.hpp:38
pion::scheduler
Definition: scheduler.hpp:34