Cleanup of server-constructors. Previous constructors have been marked as deprecated.

This commit is contained in:
eidheim 2016-12-29 09:53:58 +01:00
commit 6c3a59d9ba
6 changed files with 58 additions and 30 deletions

View file

@ -30,7 +30,8 @@ int main() {
//HTTP-server at port 8080 using 1 thread
//Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpServer server(8080, 1);
HttpServer server;
server.config.port=8080;
//Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string

View file

@ -28,7 +28,8 @@ int main() {
//HTTPS-server at port 8080 using 1 thread
//Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpsServer server(8080, 1, "server.crt", "server.key");
HttpsServer server("server.crt", "server.key");
server.config.port=8080;
//Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string

View file

@ -20,6 +20,15 @@
#define REGEX_NS std
#endif
// TODO when switching to c++14, use [[deprecated]] instead
#ifdef __GNUC__
#define DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED __declspec(deprecated)
#else
#define DEPRECATED
#endif
namespace SimpleWeb {
template <class socket_type>
class Server;
@ -107,15 +116,21 @@ namespace SimpleWeb {
class Config {
friend class ServerBase<socket_type>;
Config(unsigned short port, size_t num_threads): num_threads(num_threads), port(port), reuse_address(true) {}
size_t num_threads;
Config(unsigned short port): port(port) {}
public:
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
unsigned short port;
///IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
///If empty, the address will be any address.
/// Number of threads that the server will use when start() is called. Defaults to 1 thread.
size_t thread_pool_size=1;
/// Timeout on request handling. Defaults to 5 seconds.
size_t timeout_request=5;
/// Timeout on content handling. Defaults to 300 seconds.
size_t timeout_content=300;
/// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
/// If empty, the address will be any address.
std::string address;
///Set to false to avoid binding the socket to an address that is already in use.
bool reuse_address;
/// Set to false to avoid binding the socket to an address that is already in use. Defaults to true.
bool reuse_address=true;
};
///Set before calling start().
Config config;
@ -175,16 +190,16 @@ namespace SimpleWeb {
accept();
//If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling
//If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
threads.clear();
for(size_t c=1;c<config.num_threads;c++) {
threads.emplace_back([this](){
for(size_t c=1;c<config.thread_pool_size;c++) {
threads.emplace_back([this]() {
io_service->run();
});
}
//Main thread
if(config.num_threads>0)
if(config.thread_pool_size>0)
io_service->run();
//Wait for the rest of the threads, if any, to finish as well
@ -195,7 +210,7 @@ namespace SimpleWeb {
void stop() {
acceptor->close();
if(config.num_threads>0)
if(config.thread_pool_size>0)
io_service->stop();
}
@ -208,17 +223,13 @@ namespace SimpleWeb {
}
/// If you have your own boost::asio::io_service, store its pointer here before running start().
/// You might also want to set config.num_threads to 0.
/// You might also want to set config.thread_pool_size to 0.
std::shared_ptr<boost::asio::io_service> io_service;
protected:
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
std::vector<std::thread> threads;
long timeout_request;
long timeout_content;
ServerBase(unsigned short port, size_t num_threads, long timeout_request, long timeout_send_or_receive) :
config(port, num_threads), timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {}
ServerBase(unsigned short port) : config(port) {}
virtual void accept()=0;
@ -244,7 +255,7 @@ namespace SimpleWeb {
std::shared_ptr<Request> request(new Request(*socket));
//Set timeout on the following boost::asio::async-read or write function
auto timer=this->get_timeout_timer(socket, timeout_request);
auto timer=this->get_timeout_timer(socket, config.timeout_request);
boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n",
[this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
@ -274,7 +285,7 @@ namespace SimpleWeb {
}
if(content_length>num_additional_bytes) {
//Set timeout on the following boost::asio::async-read or write function
auto timer=this->get_timeout_timer(socket, timeout_content);
auto timer=this->get_timeout_timer(socket, config.timeout_content);
boost::asio::async_read(*socket, request->streambuf,
boost::asio::transfer_exactly(content_length-num_additional_bytes),
[this, socket, request, timer]
@ -363,7 +374,7 @@ namespace SimpleWeb {
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
std::shared_ptr<typename ServerBase<socket_type>::Request>)>& resource_function) {
//Set timeout on the following boost::asio::async-read or write function
auto timer=this->get_timeout_timer(socket, timeout_content);
auto timer=this->get_timeout_timer(socket, config.timeout_content);
auto response=std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) {
auto response=std::shared_ptr<Response>(response_ptr);
@ -413,8 +424,15 @@ namespace SimpleWeb {
template<>
class Server<HTTP> : public ServerBase<HTTP> {
public:
Server(unsigned short port, size_t num_threads=1, long timeout_request=5, long timeout_content=300) :
ServerBase<HTTP>::ServerBase(port, num_threads, timeout_request, timeout_content) {}
DEPRECATED Server(unsigned short port, size_t thread_pool_size=1, long timeout_request=5, long timeout_content=300) :
Server() {
config.port=port;
config.thread_pool_size=thread_pool_size;
config.timeout_request=timeout_request;
config.timeout_request=timeout_content;
}
Server() : ServerBase<HTTP>::ServerBase(80) {}
protected:
void accept() {

View file

@ -14,11 +14,18 @@ namespace SimpleWeb {
std::string session_id_context;
bool set_session_id_context=false;
public:
Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file,
DEPRECATED Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file,
long timeout_request=5, long timeout_content=300,
const std::string& verify_file=std::string()) :
ServerBase<HTTPS>::ServerBase(port, num_threads, timeout_request, timeout_content),
context(boost::asio::ssl::context::tlsv12) { // 2016/08/13 only use tls12, see https://www.ssllabs.com/ssltest
Server(cert_file, private_key_file, verify_file) {
config.port=port;
config.thread_pool_size=num_threads;
config.timeout_request=timeout_request;
config.timeout_content=timeout_content;
}
Server(const std::string& cert_file, const std::string& private_key_file, const std::string& verify_file=std::string()):
ServerBase<HTTPS>::ServerBase(443), context(boost::asio::ssl::context::tlsv12) {
context.use_certificate_chain_file(cert_file);
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
@ -60,7 +67,7 @@ namespace SimpleWeb {
socket->lowest_layer().set_option(option);
//Set timeout on the following boost::asio::ssl::stream::async_handshake
auto timer=get_timeout_timer(socket, timeout_request);
auto timer=get_timeout_timer(socket, config.timeout_request);
socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer]
(const boost::system::error_code& ec) {
if(timer)

View file

@ -9,7 +9,8 @@ typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
int main() {
HttpServer server(8080, 1);
HttpServer server;
server.config.port=8080;
server.resource["^/string$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
auto content=request->content.string();

View file

@ -8,7 +8,7 @@ using namespace SimpleWeb;
class ServerTest : public ServerBase<HTTP> {
public:
ServerTest() :
ServerBase<HTTP>::ServerBase(8080, 1, 5, 300) {}
ServerBase<HTTP>::ServerBase(8080) {}
void accept() {}