2015-01-27 06:35:29 -05:00
|
|
|
#ifndef SERVER_HPP
|
|
|
|
#define SERVER_HPP
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "server/connection.hpp"
|
|
|
|
#include "server/request_handler.hpp"
|
2016-01-28 10:28:44 -05:00
|
|
|
#include "server/service_handler.hpp"
|
2013-06-26 19:48:02 -04:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/integer_range.hpp"
|
2016-12-06 15:30:46 -05:00
|
|
|
#include "util/log.hpp"
|
2014-08-20 05:24:29 -04:00
|
|
|
|
2011-01-09 16:42:27 -05:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2014-08-20 05:24:29 -04:00
|
|
|
#include <zlib.h>
|
|
|
|
|
2016-05-17 19:43:08 -04:00
|
|
|
#ifndef _WIN32
|
2016-05-13 23:49:50 -04:00
|
|
|
#include <sys/socket.h>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <sys/types.h>
|
2016-05-17 19:43:08 -04:00
|
|
|
#endif
|
2016-05-13 23:49:50 -04:00
|
|
|
|
2014-05-09 11:04:55 -04:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <string>
|
2014-05-13 04:02:36 -04:00
|
|
|
#include <thread>
|
2013-06-26 19:48:02 -04:00
|
|
|
#include <vector>
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::server
|
2016-01-05 10:51:13 -05:00
|
|
|
{
|
|
|
|
|
2014-05-07 11:14:57 -04:00
|
|
|
class Server
|
|
|
|
{
|
|
|
|
public:
|
2014-08-20 05:57:31 -04:00
|
|
|
// Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else
|
2023-08-30 14:06:39 -04:00
|
|
|
static std::shared_ptr<Server> CreateServer(std::string &ip_address,
|
|
|
|
int ip_port,
|
|
|
|
unsigned requested_num_threads,
|
|
|
|
short keepalive_timeout)
|
2014-08-20 05:24:29 -04:00
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log() << "http 1.1 compression handled by zlib version " << zlibVersion();
|
2014-08-20 05:24:29 -04:00
|
|
|
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
|
|
|
|
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
|
2023-08-30 14:06:39 -04:00
|
|
|
return std::make_shared<Server>(ip_address, ip_port, real_num_threads, keepalive_timeout);
|
2014-08-20 05:24:29 -04:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:06:39 -04:00
|
|
|
explicit Server(const std::string &address,
|
|
|
|
const int port,
|
|
|
|
const unsigned thread_pool_size,
|
|
|
|
const short keepalive_timeout)
|
|
|
|
: thread_pool_size(thread_pool_size), keepalive_timeout(keepalive_timeout),
|
|
|
|
acceptor(io_context), new_connection(std::make_shared<Connection>(
|
|
|
|
io_context, request_handler, keepalive_timeout))
|
2014-05-07 11:14:57 -04:00
|
|
|
{
|
2015-09-08 21:31:03 -04:00
|
|
|
const auto port_string = std::to_string(port);
|
2014-05-28 06:34:48 -04:00
|
|
|
|
2021-08-24 15:11:08 -04:00
|
|
|
boost::asio::ip::tcp::resolver resolver(io_context);
|
2014-05-28 06:34:48 -04:00
|
|
|
boost::asio::ip::tcp::resolver::query query(address, port_string);
|
2014-05-07 11:14:57 -04:00
|
|
|
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
|
|
|
|
|
|
|
|
acceptor.open(endpoint.protocol());
|
2016-05-11 19:23:50 -04:00
|
|
|
#ifdef SO_REUSEPORT
|
2016-05-13 23:49:50 -04:00
|
|
|
const int option = 1;
|
|
|
|
setsockopt(acceptor.native_handle(), SOL_SOCKET, SO_REUSEPORT, &option, sizeof(option));
|
2016-05-11 19:23:50 -04:00
|
|
|
#endif
|
2016-05-13 23:49:50 -04:00
|
|
|
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
2014-05-07 11:14:57 -04:00
|
|
|
acceptor.bind(endpoint);
|
|
|
|
acceptor.listen();
|
2016-03-18 07:09:28 -04:00
|
|
|
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log() << "Listening on: " << acceptor.local_endpoint();
|
2016-03-18 07:09:28 -04:00
|
|
|
|
2014-05-07 11:14:57 -04:00
|
|
|
acceptor.async_accept(
|
|
|
|
new_connection->socket(),
|
|
|
|
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run()
|
|
|
|
{
|
2014-05-13 04:02:36 -04:00
|
|
|
std::vector<std::shared_ptr<std::thread>> threads;
|
2014-05-07 11:14:57 -04:00
|
|
|
for (unsigned i = 0; i < thread_pool_size; ++i)
|
|
|
|
{
|
2014-05-13 04:02:36 -04:00
|
|
|
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(
|
2021-08-24 15:11:08 -04:00
|
|
|
boost::bind(&boost::asio::io_context::run, &io_context));
|
2014-05-07 11:14:57 -04:00
|
|
|
threads.push_back(thread);
|
|
|
|
}
|
2022-06-30 09:32:12 -04:00
|
|
|
for (const auto &thread : threads)
|
2014-05-13 04:02:36 -04:00
|
|
|
{
|
2014-10-08 08:47:22 -04:00
|
|
|
thread->join();
|
2014-05-13 04:02:36 -04:00
|
|
|
}
|
2014-05-07 11:14:57 -04:00
|
|
|
}
|
|
|
|
|
2021-08-24 15:11:08 -04:00
|
|
|
void Stop() { io_context.stop(); }
|
2014-05-07 11:14:57 -04:00
|
|
|
|
2016-10-27 06:26:23 -04:00
|
|
|
void RegisterServiceHandler(std::unique_ptr<ServiceHandlerInterface> service_handler_)
|
2016-01-28 10:28:44 -05:00
|
|
|
{
|
|
|
|
request_handler.RegisterServiceHandler(std::move(service_handler_));
|
|
|
|
}
|
2014-05-07 11:14:57 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void HandleAccept(const boost::system::error_code &e)
|
|
|
|
{
|
|
|
|
if (!e)
|
|
|
|
{
|
|
|
|
new_connection->start();
|
2023-08-30 14:06:39 -04:00
|
|
|
new_connection =
|
|
|
|
std::make_shared<Connection>(io_context, request_handler, keepalive_timeout);
|
2014-05-07 11:14:57 -04:00
|
|
|
acceptor.async_accept(
|
|
|
|
new_connection->socket(),
|
|
|
|
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
|
|
|
}
|
2021-08-29 15:35:22 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
util::Log(logERROR) << "HandleAccept error: " << e.message();
|
|
|
|
}
|
2014-05-07 11:14:57 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 17:28:12 -04:00
|
|
|
RequestHandler request_handler;
|
2014-05-07 11:14:57 -04:00
|
|
|
unsigned thread_pool_size;
|
2023-08-30 14:06:39 -04:00
|
|
|
short keepalive_timeout;
|
2021-08-24 15:11:08 -04:00
|
|
|
boost::asio::io_context io_context;
|
2014-05-07 11:14:57 -04:00
|
|
|
boost::asio::ip::tcp::acceptor acceptor;
|
2016-01-05 10:51:13 -05:00
|
|
|
std::shared_ptr<Connection> new_connection;
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::server
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2015-01-27 06:35:29 -05:00
|
|
|
#endif // SERVER_HPP
|