With C++11 the stdlib gains: - `std::stoi` function family to convert from `std::string` to integral type - `std::to_string` to convert from number types to `std::string` The only reason for hand-writing the conversion code therefore is performance. I benchmarked an `osrm-extract` with the hand-written code against one with the stdlib conversion features and could not find any significant difference (we switch back and forth between C++ and Lua, shaving off a few us in conversion doesn't gain us much). Formatting arithmetic types in the default format with given precision requires streams, but is doable in a few lines of idiomatic stdlib code. For this, there is now the following function template available: template <Arithmetic T, int Precision = 6> inline std::string to_string_with_precision(const T); that requires integral or floating point types and returns a formatted string in the defaukt format with the given precision applied. In addition this completely rips out Boost.Spirit from the `casts.hpp` header, resulting in faster compile times. Boom! References: - http://en.cppreference.com/w/cpp/string/basic_string/stol - http://en.cppreference.com/w/cpp/string/basic_string/to_string - http://www.kumobius.com/2013/08/c-string-to-int/
120 lines
4.3 KiB
C++
120 lines
4.3 KiB
C++
/*
|
|
|
|
Copyright (c) 2015, Project OSRM contributors
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
are permitted provided that the following conditions are met:
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
of conditions and the following disclaimer.
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#ifndef SERVER_HPP
|
|
#define SERVER_HPP
|
|
|
|
#include "connection.hpp"
|
|
#include "request_handler.hpp"
|
|
|
|
#include "../util/integer_range.hpp"
|
|
#include "../util/simple_logger.hpp"
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
class Server
|
|
{
|
|
public:
|
|
// Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else
|
|
static std::shared_ptr<Server>
|
|
CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads)
|
|
{
|
|
SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
|
|
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
|
|
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
|
|
return std::make_shared<Server>(ip_address, ip_port, real_num_threads);
|
|
}
|
|
|
|
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
|
|
: thread_pool_size(thread_pool_size), acceptor(io_service),
|
|
new_connection(std::make_shared<http::Connection>(io_service, request_handler))
|
|
{
|
|
const auto port_string = std::to_string(port);
|
|
|
|
boost::asio::ip::tcp::resolver resolver(io_service);
|
|
boost::asio::ip::tcp::resolver::query query(address, port_string);
|
|
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
|
|
|
|
acceptor.open(endpoint.protocol());
|
|
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
|
acceptor.bind(endpoint);
|
|
acceptor.listen();
|
|
acceptor.async_accept(
|
|
new_connection->socket(),
|
|
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
|
}
|
|
|
|
void Run()
|
|
{
|
|
std::vector<std::shared_ptr<std::thread>> threads;
|
|
for (unsigned i = 0; i < thread_pool_size; ++i)
|
|
{
|
|
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(
|
|
boost::bind(&boost::asio::io_service::run, &io_service));
|
|
threads.push_back(thread);
|
|
}
|
|
for (auto thread : threads)
|
|
{
|
|
thread->join();
|
|
}
|
|
}
|
|
|
|
void Stop() { io_service.stop(); }
|
|
|
|
RequestHandler &GetRequestHandlerPtr() { return request_handler; }
|
|
|
|
private:
|
|
void HandleAccept(const boost::system::error_code &e)
|
|
{
|
|
if (!e)
|
|
{
|
|
new_connection->start();
|
|
new_connection = std::make_shared<http::Connection>(io_service, request_handler);
|
|
acceptor.async_accept(
|
|
new_connection->socket(),
|
|
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
|
}
|
|
}
|
|
|
|
unsigned thread_pool_size;
|
|
boost::asio::io_service io_service;
|
|
boost::asio::ip::tcp::acceptor acceptor;
|
|
std::shared_ptr<http::Connection> new_connection;
|
|
RequestHandler request_handler;
|
|
};
|
|
|
|
#endif // SERVER_HPP
|