2014-03-31 16:51:50 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-08-17 09:35:21 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2014-03-31 16:51:50 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-07-08 09:31:52 +00:00
|
|
|
#include <cstdint>
|
2014-05-02 05:17:49 +00:00
|
|
|
#include <atomic>
|
2014-08-07 16:14:27 +00:00
|
|
|
#include <future>
|
2014-03-31 16:51:50 +00:00
|
|
|
|
2014-07-29 11:20:50 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
#include "http_connection.h"
|
2014-05-02 05:17:49 +00:00
|
|
|
#include "datetime.h"
|
2014-05-20 16:17:56 +00:00
|
|
|
#include "logging.h"
|
2014-08-16 02:55:26 +00:00
|
|
|
#include "dumb_timer_queue.h"
|
2014-03-31 16:51:50 +00:00
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
|
|
|
using namespace boost;
|
|
|
|
using tcp = asio::ip::tcp;
|
2014-07-08 09:31:52 +00:00
|
|
|
|
2014-09-06 19:30:53 +00:00
|
|
|
template <typename Handler, typename ... Middlewares>
|
2014-03-31 16:51:50 +00:00
|
|
|
class Server
|
|
|
|
{
|
|
|
|
public:
|
2014-10-23 17:33:03 +00:00
|
|
|
Server(Handler* handler, uint16_t port, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1)
|
2014-05-02 05:17:49 +00:00
|
|
|
: acceptor_(io_service_, tcp::endpoint(asio::ip::address(), port)),
|
|
|
|
signals_(io_service_, SIGINT, SIGTERM),
|
|
|
|
handler_(handler),
|
2014-05-20 16:17:56 +00:00
|
|
|
concurrency_(concurrency),
|
2014-10-23 17:33:03 +00:00
|
|
|
port_(port),
|
|
|
|
middlewares_(middlewares)
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2014-08-17 09:35:21 +00:00
|
|
|
if (concurrency_ < 0)
|
|
|
|
concurrency_ = 1;
|
|
|
|
|
|
|
|
for(int i = 0; i < concurrency_; i++)
|
|
|
|
io_service_pool_.emplace_back(new boost::asio::io_service());
|
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
std::vector<std::future<void>> v;
|
|
|
|
for(uint16_t i = 0; i < concurrency_; i ++)
|
|
|
|
v.push_back(
|
2014-08-17 09:35:21 +00:00
|
|
|
std::async(std::launch::async, [this, i]{
|
2014-09-06 19:30:53 +00:00
|
|
|
// initializing timer queue
|
2014-08-16 02:55:26 +00:00
|
|
|
auto& timer_queue = detail::dumb_timer_queue::get_current_dumb_timer_queue();
|
2014-09-06 19:30:53 +00:00
|
|
|
|
2014-08-17 09:35:21 +00:00
|
|
|
timer_queue.set_io_service(*io_service_pool_[i]);
|
|
|
|
boost::asio::deadline_timer timer(*io_service_pool_[i]);
|
|
|
|
timer.expires_from_now(boost::posix_time::seconds(1));
|
2014-09-06 19:30:53 +00:00
|
|
|
|
2014-08-17 09:35:21 +00:00
|
|
|
std::function<void(const boost::system::error_code& ec)> handler;
|
|
|
|
handler = [&](const boost::system::error_code& ec){
|
|
|
|
if (ec)
|
|
|
|
return;
|
2014-08-16 02:55:26 +00:00
|
|
|
timer_queue.process();
|
2014-08-17 09:35:21 +00:00
|
|
|
timer.expires_from_now(boost::posix_time::seconds(1));
|
|
|
|
timer.async_wait(handler);
|
|
|
|
};
|
|
|
|
timer.async_wait(handler);
|
2014-09-06 19:30:53 +00:00
|
|
|
|
2014-08-17 09:35:21 +00:00
|
|
|
io_service_pool_[i]->run();
|
2014-08-16 02:55:26 +00:00
|
|
|
}));
|
2014-05-20 22:30:51 +00:00
|
|
|
CROW_LOG_INFO << server_name_ << " server is running, local port " << port_;
|
2014-05-20 16:17:56 +00:00
|
|
|
|
2014-05-02 05:17:49 +00:00
|
|
|
signals_.async_wait(
|
|
|
|
[&](const boost::system::error_code& error, int signal_number){
|
2014-08-17 09:35:21 +00:00
|
|
|
stop();
|
2014-05-02 05:17:49 +00:00
|
|
|
});
|
2014-08-17 09:35:21 +00:00
|
|
|
|
|
|
|
do_accept();
|
|
|
|
|
|
|
|
v.push_back(std::async(std::launch::async, [this]{
|
|
|
|
io_service_.run();
|
|
|
|
CROW_LOG_INFO << "Exiting.";
|
|
|
|
}));
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 13:08:23 +00:00
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
io_service_.stop();
|
2014-08-17 09:35:21 +00:00
|
|
|
for(auto& io_service:io_service_pool_)
|
|
|
|
io_service->stop();
|
2014-04-15 13:08:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
private:
|
2014-08-17 09:35:21 +00:00
|
|
|
asio::io_service& pick_io_service()
|
|
|
|
{
|
|
|
|
// TODO load balancing
|
|
|
|
roundrobin_index_++;
|
|
|
|
if (roundrobin_index_ >= io_service_pool_.size())
|
|
|
|
roundrobin_index_ = 0;
|
|
|
|
return *io_service_pool_[roundrobin_index_];
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
void do_accept()
|
|
|
|
{
|
2014-09-06 19:30:53 +00:00
|
|
|
auto p = new Connection<Handler, Middlewares...>(pick_io_service(), handler_, server_name_, middlewares_);
|
2014-08-17 09:35:21 +00:00
|
|
|
acceptor_.async_accept(p->socket(),
|
|
|
|
[this, p](boost::system::error_code ec)
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
|
|
|
if (!ec)
|
2014-07-29 11:20:50 +00:00
|
|
|
{
|
|
|
|
p->start();
|
|
|
|
}
|
2014-03-31 16:51:50 +00:00
|
|
|
do_accept();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
asio::io_service io_service_;
|
2014-08-17 09:35:21 +00:00
|
|
|
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
|
2014-03-31 16:51:50 +00:00
|
|
|
tcp::acceptor acceptor_;
|
2014-05-02 05:17:49 +00:00
|
|
|
boost::asio::signal_set signals_;
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
Handler* handler_;
|
2014-05-02 09:22:02 +00:00
|
|
|
uint16_t concurrency_{1};
|
2014-05-02 05:17:49 +00:00
|
|
|
std::string server_name_ = "Crow/0.1";
|
2014-05-20 16:17:56 +00:00
|
|
|
uint16_t port_;
|
2014-08-17 09:35:21 +00:00
|
|
|
unsigned int roundrobin_index_{};
|
2014-09-06 19:30:53 +00:00
|
|
|
|
2014-10-23 17:33:03 +00:00
|
|
|
std::tuple<Middlewares...>* middlewares_;
|
2014-03-31 16:51:50 +00:00
|
|
|
};
|
|
|
|
}
|