Run internal http server in main loop to avoid requirement to thread synchronization.

This commit is contained in:
XMRig
2018-02-20 23:22:34 +07:00
parent 1d72acad97
commit bf9e84f4b7
4 changed files with 33 additions and 17 deletions

View File

@@ -4,7 +4,7 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
* Copyright 2016-2018 XMRig <support@xmrig.com>
*
*
* This program is free software: you can redistribute it and/or modify
@@ -36,6 +36,18 @@ Httpd::Httpd(int port, const char *accessToken) :
m_port(port),
m_daemon(nullptr)
{
uv_idle_init(uv_default_loop(), &m_idle);
m_idle.data = this;
}
Httpd::~Httpd()
{
uv_idle_stop(&m_idle);
if (m_daemon) {
MHD_stop_daemon(m_daemon);
}
}
@@ -45,12 +57,18 @@ bool Httpd::start()
return false;
}
m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END);
unsigned int flags = 0;
if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) {
flags |= MHD_USE_EPOLL_LINUX_ONLY;
}
m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END);
if (!m_daemon) {
LOG_ERR("HTTP Daemon failed to start.");
return false;
}
uv_idle_start(&m_idle, Httpd::onIdle);
return true;
}
@@ -114,3 +132,9 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url
MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE);
return done(connection, status, rsp);
}
void Httpd::onIdle(uv_idle_t *handle)
{
MHD_run(static_cast<Httpd*>(handle->data)->m_daemon);
}