Browse Source

moving webserver to background thread

master
Volker Birk 2 years ago
parent
commit
f92957a2e5
3 changed files with 19 additions and 9 deletions
  1. +3
    -0
      test_webserver.cc
  2. +13
    -9
      webserver.cc
  3. +3
    -0
      webserver.hh

+ 3
- 0
test_webserver.cc View File

@ -1,5 +1,6 @@
#include <iostream>
#include <thread>
#include <unistd.h>
#include "webserver.hh"
@ -51,6 +52,8 @@ int main()
);
web->run();
std::cin.get();
web->shutdown();
return 0;
}


+ 13
- 9
webserver.cc View File

@ -6,7 +6,6 @@
#include <iostream>
#include <memory>
#include <string>
#include <thread>
namespace fs = boost::filesystem;
@ -223,25 +222,30 @@ void Webserver::do_session(tcp::socket *socket)
}
void Webserver::run()
void Webserver::runner(Webserver *me)
{
_running = true;
while (_running)
while (me->_running)
{
tcp::socket* socket = new tcp::socket{_ioc};
_acceptor.accept(*socket);
tcp::socket* socket = new tcp::socket{me->_ioc};
me->_acceptor.accept(*socket);
std::function< void() > tf = [=]()
{
thread_init();
do_session(socket);
thread_done();
me->thread_init();
me->do_session(socket);
me->thread_done();
};
std::thread{tf}.detach();
}
}
void Webserver::run()
{
_running = true;
_runner = std::thread(runner, this);
}
void Webserver::shutdown()
{
_running = false;


+ 3
- 0
webserver.hh View File

@ -2,6 +2,7 @@
#include <string>
#include <unordered_map>
#include <thread>
#include <boost/asio/ip/tcp.hpp>
#include <boost/regex.hpp>
#include <boost/beast/http.hpp>
@ -46,6 +47,7 @@ namespace pEp {
unsigned short _port;
bool _running;
std::mutex _mtx;
std::thread _runner;
public:
@ -79,6 +81,7 @@ namespace pEp {
port, const std::string& doc_root = "");
protected:
static void runner(Webserver *me);
void deliver_status(tcp::socket *socket, const request& req, http::status status);
void deliver_file (tcp::socket *socket, const request& req);
handler_t find_handler(const request& req, boost::cmatch& m);


Loading…
Cancel
Save