Reimplemented run().

This commit is contained in:
Relintai 2021-07-06 15:03:46 +02:00
parent 3f45e92835
commit eab0802df2
8 changed files with 133 additions and 78 deletions

View File

@ -97,8 +97,8 @@ env_base.Prepend(CPPPATH=["#"])
env_base.Prepend(CPPPATH=["#libs"])
env_base.Prepend(LINKFLAGS=["-lpthread"])
env_base.Append(CXX=["-o3"])
#env_base.Append(CXX=["-g"])
#env_base.Append(CXX=["-o3"])
env_base.Append(CXX=["-g"])
#env_base.Append(CXX=["-g2"])
#env_base.Append(CXX=["-fno-rtti"])

View File

@ -82,6 +82,7 @@ HttpAppFrameworkImpl::HttpAppFrameworkImpl() :
uploadPath_(rootPath_ + "uploads") {
}
/*
static std::function<void()> f = [] {
LOG_TRACE << "Initialize the main event loop in the main thread";
};
@ -90,6 +91,7 @@ static std::function<void()> f = [] {
drogon::InitBeforeMainFunction drogon::HttpAppFrameworkImpl::initFirst_([]() {
HttpAppFrameworkImpl::instance().getLoop()->runInLoop(f);
});
*/
namespace drogon {
std::string getVersion() {
@ -462,7 +464,7 @@ void HttpAppFrameworkImpl::run() {
}
#endif
// Create all listeners.
auto ioLoops = listenerManagerPtr_->createListeners(
auto ioLoops = listenerManagerPtr_->createListeners(getLoop(),
std::bind(&HttpAppFrameworkImpl::onAsyncRequest, this, _1, _2),
std::bind(&HttpAppFrameworkImpl::onNewWebsockRequest, this, _1, _2, _3),
std::bind(&HttpAppFrameworkImpl::onConnection, this, _1),
@ -736,8 +738,9 @@ void HttpAppFrameworkImpl::onAsyncRequest(
}
trantor::EventLoop *HttpAppFrameworkImpl::getLoop() const {
static trantor::EventLoop loop;
return &loop;
//static trantor::EventLoop loop;
//return &loop;]
return nullptr;
}
trantor::EventLoop *HttpAppFrameworkImpl::getIOLoop(size_t id) const {

View File

@ -32,12 +32,13 @@ void defaultExceptionHandler(const std::exception &,
const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&);
/*
struct InitBeforeMainFunction {
explicit InitBeforeMainFunction(const std::function<void()> &func) {
func();
}
};
*/
class HttpAppFrameworkImpl final : public HttpAppFramework {
public:
HttpAppFrameworkImpl();
@ -533,7 +534,7 @@ private:
HttpResponsePtr custom404_;
std::function<HttpResponsePtr(HttpStatusCode)> customErrorHandler_ =
&defaultErrorHandler;
static InitBeforeMainFunction initFirst_;
//static InitBeforeMainFunction initFirst_;
bool enableServerHeader_{ true };
bool enableDateHeader_{ true };
bool reusePort_{ false };

View File

@ -159,6 +159,7 @@ HttpServer::HttpServer(
}
HttpServer::~HttpServer() {
LOG_WARN << "HttpServer desxtr";
}
void HttpServer::start() {

View File

@ -65,7 +65,7 @@ void ListenerManager::addListener(
ip, port, useSSL, certFile, keyFile, useOldTLS, sslConfCmds);
}
std::vector<trantor::EventLoop *> ListenerManager::createListeners(
std::vector<trantor::EventLoop *> ListenerManager::createListeners(trantor::EventLoop *event_loop,
const HttpAsyncCallback &httpCallback,
const WebSocketNewAsyncCallback &webSocketCallback,
const ConnectionCallback &connectionCallback,
@ -86,43 +86,53 @@ std::vector<trantor::EventLoop *> ListenerManager::createListeners(
std::function<void(const HttpRequestPtr &, const HttpResponsePtr &)> >
&preSendingAdvices) {
#ifdef __linux__
for (size_t i = 0; i < threadNum; ++i) {
LOG_TRACE << "thread num=" << threadNum;
auto loopThreadPtr = std::make_shared<EventLoopThread>("DrogonIoLoop");
listeningloopThreads_.push_back(loopThreadPtr);
ioLoops_.push_back(loopThreadPtr->getLoop());
for (auto const &listener : listeners_) {
auto const &ip = listener.ip_;
bool isIpv6 = ip.find(':') == std::string::npos ? false : true;
std::shared_ptr<HttpServer> serverPtr;
InetAddress listenAddress(ip, listener.port_, isIpv6);
if (listenAddress.isUnspecified()) {
LOG_FATAL << "Failed to parse IP address '" << ip
<< "'. (Note: FQDN/domain names/hostnames are not "
"supported. Including 'localhost')";
abort();
}
if (i == 0 && !app().reusePort()) {
DrogonFileLocker lock;
// Check whether the port is in use.
TcpServer server(HttpAppFrameworkImpl::instance().getLoop(),
TcpServer server(event_loop,
std::move(listenAddress),
"drogonPortTest",
true,
false);
serverPtr =
std::make_shared<HttpServer>(loopThreadPtr->getLoop(),
serverPtr = std::make_shared<HttpServer>(loopThreadPtr->getLoop(),
std::move(listenAddress),
"drogon",
syncAdvices,
preSendingAdvices);
} else {
serverPtr =
std::make_shared<HttpServer>(loopThreadPtr->getLoop(),
serverPtr = std::make_shared<HttpServer>(loopThreadPtr->getLoop(),
std::move(listenAddress),
"drogon",
syncAdvices,
preSendingAdvices);
}
if (listener.useSSL_) {

View File

@ -35,7 +35,7 @@ public:
bool useOldTLS = false,
const std::vector<std::pair<std::string, std::string> >
&sslConfCmds = {});
std::vector<trantor::EventLoop *> createListeners(
std::vector<trantor::EventLoop *> createListeners(trantor::EventLoop *event_loop,
const HttpAsyncCallback &httpCallback,
const WebSocketNewAsyncCallback &webSocketCallback,
const trantor::ConnectionCallback &connectionCallback,

View File

@ -12,6 +12,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <trantor/utils/AsyncFileLogger.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -142,6 +144,9 @@ void DWebApplication::update() {
}
void DWebApplication::add_listener(const std::string &ip, uint16_t port, bool useSSL, const std::string &certFile, const std::string &keyFile, bool useOldTLS, const std::vector<std::pair<std::string, std::string> > &sslConfCmds) {
assert(!_running);
_listener_manager->addListener(ip, port, useSSL, certFile, keyFile, useOldTLS, sslConfCmds);
}
void DWebApplication::set_thread_num(size_t thread_num) {
@ -164,27 +169,34 @@ void DWebApplication::set_ssl_files(const std::string &certPath, const std::stri
}
void DWebApplication::run() {
/*
if (!getLoop()->isInLoopThread()) {
getLoop()->moveToCurrentThread();
if (!get_loop()->isInLoopThread()) {
get_loop()->moveToCurrentThread();
}
LOG_TRACE << "Start to run...";
trantor::AsyncFileLogger asyncFileLogger;
// Create dirs for cache files
for (int i = 0; i < 256; ++i) {
char dirName[4];
snprintf(dirName, sizeof(dirName), "%02x", i);
std::transform(dirName, dirName + 2, dirName, toupper);
utils::createPath(getUploadPath() + "/tmp/" + dirName);
utils::createPath(get_upload_path() + "/tmp/" + dirName);
}
/*
if (runAsDaemon_) {
// go daemon!
godaemon();
#ifdef __linux__
getLoop()->resetTimerQueue();
get_loop()->resetTimerQueue();
#endif
getLoop()->resetAfterFork();
get_loop()->resetAfterFork();
}
// set relaunching
if (relaunchOnError_) {
#ifndef _WIN32
@ -202,28 +214,33 @@ void DWebApplication::run() {
sleep(1);
LOG_INFO << "start new process";
}
getLoop()->resetAfterFork();
get_loop()->resetAfterFork();
#endif
}
if (handleSigterm_) {
signal(SIGTERM, TERMFunction);
}
*/
// set logger
if (!logPath_.empty()) {
if (!_log_path.empty()) {
#ifdef _WIN32
if (_access(logPath_.c_str(), 06) != 0)
if (_access(_log_path.c_str(), 06) != 0)
#else
if (access(logPath_.c_str(), R_OK | W_OK) != 0)
if (access(_log_path.c_str(), R_OK | W_OK) != 0)
#endif
{
LOG_ERROR << "log file path not exist";
abort();
} else {
std::string baseName = logfileBaseName_;
std::string baseName = _logfile_base_name;
if (baseName.empty()) {
baseName = "drogon";
baseName = "rcpp_fw";
}
asyncFileLogger.setFileName(baseName, ".log", logPath_);
asyncFileLogger.setFileName(baseName, ".log", _log_path);
asyncFileLogger.startLogging();
trantor::Logger::setOutputFunction(
@ -232,13 +249,17 @@ void DWebApplication::run() {
},
[&]() { asyncFileLogger.flush(); });
asyncFileLogger.setFileSizeLimit(logfileSize_);
asyncFileLogger.setFileSizeLimit(_logfile_size);
}
}
/*
if (relaunchOnError_) {
LOG_INFO << "Start child process";
}
*/
/*
#ifndef _WIN32
if (!libFilePaths_.empty()) {
sharedLibManagerPtr_ =
@ -246,33 +267,37 @@ void DWebApplication::run() {
libFileOutputPath_);
}
#endif
*/
// Create all listeners.
auto ioLoops = listenerManagerPtr_->createListeners(
std::bind(&HttpAppFrameworkImpl::onAsyncRequest, this, _1, _2),
std::bind(&HttpAppFrameworkImpl::onNewWebsockRequest, this, _1, _2, _3),
std::bind(&HttpAppFrameworkImpl::onConnection, this, _1),
idleConnectionTimeout_,
sslCertPath_,
sslKeyPath_,
sslConfCmds_,
threadNum_,
syncAdvices_,
preSendingAdvices_);
assert(ioLoops.size() == threadNum_);
for (size_t i = 0; i < threadNum_; ++i) {
auto ioLoops = _listener_manager->createListeners(get_loop(),
std::bind(&DWebApplication::on_async_request, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&DWebApplication::on_new_websock_request, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3),
std::bind(&DWebApplication::on_connection, this, std::placeholders::_1),
_idle_connection_timeout, _ssl_cert_path, _ssl_key_path, _ssl_conf_cmds, _thread_num, _sync_advices, _pre_sending_advices);
assert(ioLoops.size() == _thread_num);
for (size_t i = 0; i < _thread_num; ++i) {
ioLoops[i]->setIndex(i);
}
getLoop()->setIndex(threadNum_);
get_loop()->setIndex(_thread_num);
// A fast database client instance should be created in the main event
// loop, so put the main loop into ioLoops.
ioLoops.push_back(getLoop());
ioLoops.push_back(get_loop());
if (useSession_) {
sessionManagerPtr_ =
std::make_unique<SessionManager>(getLoop(), sessionTimeout_);
if (_use_session) {
_session_manager = std::make_unique<SessionManager>(get_loop(), _session_timeout);
}
// now start runing!!
running_ = true;
_running = true;
/*
// Initialize plugins
const auto &pluginConfig = jsonConfig_["plugins"];
if (!pluginConfig.isNull()) {
@ -283,23 +308,26 @@ void DWebApplication::run() {
// << plugin->className();
// TODO: new plugin
});
}
}*/
//httpCtrlsRouterPtr_->init(ioLoops);
//httpSimpleCtrlsRouterPtr_->init(ioLoops);
staticFileRouterPtr_->init(ioLoops);
websockCtrlsRouterPtr_->init();
getLoop()->queueInLoop([this]() {
//staticFileRouterPtr_->init(ioLoops);
//websockCtrlsRouterPtr_->init();
get_loop()->queueInLoop([this]() {
// Let listener event loops run when everything is ready.
listenerManagerPtr_->startListening();
for (auto &adv : beginningAdvices_) {
_listener_manager->startListening();
for (auto &adv : _beginning_advices) {
adv();
}
beginningAdvices_.clear();
_beginning_advices.clear();
});
getLoop()->loop();
*/
get_loop()->loop();
}
void DWebApplication::stop() {
@ -448,6 +476,7 @@ void DWebApplication::set_implicit_page_enable(bool useImplicitPage) {
}
bool DWebApplication::is_implicit_page_enabled() const {
//return staticFileRouterPtr_->isImplicitPageEnabled();
return false;
}
void DWebApplication::set_implicit_page(const std::string &implicitPageFile) {
//staticFileRouterPtr_->setImplicitPage(implicitPageFile);
@ -455,6 +484,8 @@ void DWebApplication::set_implicit_page(const std::string &implicitPageFile) {
const std::string &DWebApplication::get_implicit_page() const {
//return staticFileRouterPtr_->getImplicitPage();
static std::string s = "";
return s;
}
size_t DWebApplication::get_client_max_body_size() const {
return _client_max_body_size;
@ -493,9 +524,7 @@ const std::pair<unsigned int, std::string> &DWebApplication::get_float_precision
}
trantor::EventLoop *DWebApplication::get_loop() const {
static trantor::EventLoop loop;
return &loop;
return _loop;
}
trantor::EventLoop *DWebApplication::get_io_loop(size_t id) const {
assert(_listener_manager);
@ -635,6 +664,8 @@ void DWebApplication::on_new_websock_request(const HttpRequestImplPtr &req, std:
}*/
}
void DWebApplication::on_connection(const trantor::TcpConnectionPtr &conn) {
LOG_INFO << "on_connection";
/*
static std::mutex mtx;
LOG_TRACE << "connect!!!" << maxConnectionNum_
@ -652,7 +683,7 @@ void DWebApplication::on_connection(const trantor::TcpConnectionPtr &conn) {
if (iter == connectionsNumMap_.end()) {
connectionsNumMap_[conn->peerAddr().toIp()] = 1;
} else if (iter->second++ > maxConnectionNumPerIP_) {
conn->getLoop()->queueInLoop(
conn->get_loop()->queueInLoop(
[conn]() { conn->forceClose(); });
return;
}
@ -703,9 +734,10 @@ void DWebApplication::find_session_for_request(const HttpRequestImplPtr &req) {
//void forward(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,const std::string &hostString,double timeout);
//void forward(const HttpRequestImplPtr &req,std::function<void(const HttpResponsePtr &)> &&callback,const std::string &hostString,double timeout = 0);
DWebApplication::DWebApplication() : _listener_manager(new ListenerManager()) {
//staticFileRouterPtr_(new StaticFileRouter{}),
/*
DWebApplication::DWebApplication() :
_listener_manager(new ListenerManager()) {
//staticFileRouterPtr_(new StaticFileRouter{}),
/*
httpCtrlsRouterPtr_(new HttpControllersRouter(*staticFileRouterPtr_,
postRoutingAdvices_,
postRoutingObservers_,
@ -719,19 +751,19 @@ DWebApplication::DWebApplication() : _listener_manager(new ListenerManager()) {
preHandlingAdvices_,
preHandlingObservers_,
postHandlingAdvices_)),*/
//websockCtrlsRouterPtr_(
// new WebsocketControllersRouter(postRoutingAdvices_,
// postRoutingObservers_)),
//listenerManagerPtr_(new ListenerManager),
//pluginsManagerPtr_(new PluginsManager),
//uploadPath_(rootPath_ + "uploads") {
//websockCtrlsRouterPtr_(
// new WebsocketControllersRouter(postRoutingAdvices_,
// postRoutingObservers_)),
//listenerManagerPtr_(new ListenerManager),
//pluginsManagerPtr_(new PluginsManager),
//uploadPath_(rootPath_ + "uploads") {
_loop = new trantor::EventLoop();
//_listener_manager = new ListenerManager();
_upload_path = _root_path + "uploads";
}
DWebApplication::~DWebApplication() {
main_route_map.clear();
error_handler_map.clear();

View File

@ -11,15 +11,15 @@
#include "handler_instance.h"
#include <trantor/utils/Logger.h>
#include <trantor/net/InetAddress.h>
#include <trantor/net/Resolver.h>
#include <trantor/utils/Logger.h>
#include "http/HttpRequestImpl.h"
#include "http/HttpResponse.h"
#include "./drogon/lib/src/impl_forwards.h"
#include "./drogon/lib/src/ListenerManager.h"
#include "./drogon/lib/src/impl_forwards.h"
#include "http/SessionManager.h"
@ -51,9 +51,11 @@ public:
void unregister_request_update(DRequest *request);
void update();
//-------
//-------
void add_listener(const std::string &ip, uint16_t port, bool useSSL, const std::string &certFile, const std::string &keyFile, bool useOldTLS, const std::vector<std::pair<std::string, std::string> > &sslConfCmds);
void add_listener(const std::string &ip, uint16_t port,
bool useSSL = false, const std::string &certFile = "", const std::string &keyFile = "", bool useOldTLS = false,
const std::vector<std::pair<std::string, std::string> > &sslConfCmds = {});
void set_thread_num(size_t thread_num);
size_t get_thread_num() const;
@ -185,11 +187,11 @@ protected:
size_t _session_timeout{ 0 };
size_t _idle_connection_timeout{ 60 };
bool _use_session{ false };
std::string _server_header { "server: rcpp_framework\r\n" };
std::string _server_header{ "server: rcpp_framework\r\n" };
std::string _root_path { "./" };
std::string _root_path{ "./" };
std::string _upload_path;
std::atomic_bool _running { false };
std::atomic_bool _running{ false };
size_t _thread_num{ 1 };
@ -213,7 +215,7 @@ protected:
bool _use_brotli{ false };
bool _using_unicode_escaping{ true };
std::pair<unsigned int, std::string> _float_precision_in_json { 0, "significant" };
std::pair<unsigned int, std::string> _float_precision_in_json{ 0, "significant" };
size_t _client_max_body_size{ 1024 * 1024 };
size_t _client_max_memory_body_size{ 64 * 1024 };
@ -226,6 +228,12 @@ protected:
bool _enable_server_header{ true };
bool _enable_date_header{ true };
bool _reuse_port{ false };
trantor::EventLoop *_loop;
std::vector<std::function<void()> > _beginning_advices;
std::vector<std::function<HttpResponsePtr(const HttpRequestPtr &)> > _sync_advices;
std::vector<std::function<void(const HttpRequestPtr &, const HttpResponsePtr &)> > _pre_sending_advices;
};
#endif