Code cleanup.

This commit is contained in:
XMRig 2019-11-29 10:17:05 +07:00
parent 2b87a10cf2
commit 7d1be2d234
No known key found for this signature in database
GPG key ID: 446A53638BE94409
19 changed files with 75 additions and 66 deletions

View file

@ -51,10 +51,7 @@
xmrig::Api::Api(Base *base) :
m_base(base),
m_id(),
m_workerId(),
m_timestamp(Chrono::currentMSecsSinceEpoch()),
m_httpd(nullptr)
m_timestamp(Chrono::currentMSecsSinceEpoch())
{
base->addListener(this);
@ -180,9 +177,9 @@ void xmrig::Api::genId(const String &id)
uint8_t hash[200];
const size_t addrSize = sizeof(interfaces[i].phys_addr);
const size_t inSize = (sizeof(APP_KIND) - 1) + addrSize + sizeof(uint16_t);
const uint16_t port = static_cast<uint16_t>(m_base->config()->http().port());
const auto port = static_cast<uint16_t>(m_base->config()->http().port());
uint8_t *input = new uint8_t[inSize]();
auto*input = new uint8_t[inSize]();
memcpy(input, &port, sizeof(uint16_t));
memcpy(input + sizeof(uint16_t), interfaces[i].phys_addr, addrSize);
memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, (sizeof(APP_KIND) - 1));

View file

@ -70,10 +70,10 @@ private:
void genWorkerId(const String &id);
Base *m_base;
char m_id[32];
char m_workerId[128];
char m_id[32]{};
char m_workerId[128]{};
const uint64_t m_timestamp;
Httpd *m_httpd;
Httpd *m_httpd = nullptr;
std::vector<IApiListener *> m_listeners;
};

View file

@ -23,9 +23,9 @@
*/
#include "base/api/Httpd.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/api/Api.h"
#include "base/api/Httpd.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpApiResponse.h"
#include "base/net/http/HttpData.h"
@ -58,9 +58,7 @@ xmrig::Httpd::Httpd(Base *base) :
}
xmrig::Httpd::~Httpd()
{
}
xmrig::Httpd::~Httpd() = default;
bool xmrig::Httpd::start()

View file

@ -26,11 +26,12 @@
#define XMRIG_HTTPD_H
#include <stdint.h>
#include <cstdint>
#include "base/kernel/interfaces/IBaseListener.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -44,6 +45,8 @@ class TcpServer;
class Httpd : public IBaseListener, public IHttpListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Httpd)
Httpd(Base *base);
~Httpd() override;

View file

@ -33,6 +33,4 @@ xmrig::ApiRequest::ApiRequest(Source source, bool restricted) :
}
xmrig::ApiRequest::~ApiRequest()
{
}
xmrig::ApiRequest::~ApiRequest() = default;

View file

@ -29,6 +29,7 @@
#include "base/api/interfaces/IApiRequest.h"
#include "base/tools/String.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -37,6 +38,8 @@ namespace xmrig {
class ApiRequest : public IApiRequest
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(ApiRequest)
ApiRequest(Source source, bool restricted);
~ApiRequest() override;
@ -63,8 +66,8 @@ protected:
String m_rpcMethod;
private:
bool m_restricted;
Source m_source;
const bool m_restricted;
const Source m_source;
};

View file

@ -63,11 +63,6 @@
#include "version.h"
xmrig::BaseConfig::BaseConfig()
{
}
void xmrig::BaseConfig::printVersions()
{
char buf[256] = { 0 };

View file

@ -31,9 +31,6 @@
#include "base/net/stratum/Pools.h"
struct option;
namespace xmrig {
@ -43,7 +40,7 @@ class IJsonReader;
class BaseConfig : public IConfig
{
public:
BaseConfig();
BaseConfig() = default;
inline bool isAutoSave() const { return m_autoSave; }
inline bool isBackground() const { return m_background; }

View file

@ -125,6 +125,7 @@ public:
TlsProtocolsKey = 1114,
AlgoExtKey = 1115,
ProxyPasswordKey = 1116,
LoginFileKey = 'L',
// xmrig nvidia
CudaMaxThreadsKey = 1200,

View file

@ -26,7 +26,7 @@
#define XMRIG_ISTRATEGY_H
#include <stdint.h>
#include <cstdint>
namespace xmrig {

View file

@ -23,9 +23,9 @@
*/
#include "base/net/http/Http.h"
#include "3rdparty/rapidjson/document.h"
#include "base/io/json/Json.h"
#include "base/net/http/Http.h"
namespace xmrig {
@ -41,10 +41,7 @@ static const char *kToken = "access-token";
xmrig::Http::Http() :
m_enabled(false),
m_restricted(true),
m_host(kLocalhost),
m_port(0)
m_host(kLocalhost)
{
}

View file

@ -38,7 +38,7 @@ class Http
public:
Http();
inline bool isAuthRequired() const { return m_restricted == false || !m_token.isNull(); }
inline bool isAuthRequired() const { return !m_restricted || !m_token.isNull(); }
inline bool isEnabled() const { return m_enabled; }
inline bool isRestricted() const { return m_restricted; }
inline const String &host() const { return m_host; }
@ -58,11 +58,11 @@ public:
void setPort(int port);
private:
bool m_enabled;
bool m_restricted;
bool m_enabled = false;
bool m_restricted = true;
String m_host;
String m_token;
uint16_t m_port;
uint16_t m_port = 0;
};

View file

@ -66,7 +66,7 @@ public:
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<ClientWriteBaton *>(req->data); }
uv_buf_t bufs[2];
uv_buf_t bufs[2]{};
private:
std::string m_body;

View file

@ -24,27 +24,30 @@
*/
#include "base/net/http/HttpContext.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/tools/Chrono.h"
#include <algorithm>
#include <uv.h>
#include "3rdparty/http-parser/http_parser.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/net/http/HttpContext.h"
namespace xmrig {
static http_parser_settings http_settings;
static std::map<uint64_t, HttpContext *> storage;
static uint64_t SEQUENCE = 0;
} // namespace xmrig
xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) :
HttpData(SEQUENCE++),
m_wasHeaderValue(false),
m_timestamp(Chrono::steadyMSecs()),
m_listener(listener)
{
storage[id()] = this;
@ -96,6 +99,12 @@ std::string xmrig::HttpContext::ip() const
}
uint64_t xmrig::HttpContext::elapsed() const
{
return Chrono::steadyMSecs() - m_timestamp;
}
void xmrig::HttpContext::close(int status)
{
if (status < 0 && m_listener) {

View file

@ -59,6 +59,7 @@ public:
size_t parse(const char *data, size_t size);
std::string ip() const;
uint64_t elapsed() const;
void close(int status = 0);
static HttpContext *get(uint64_t id);
@ -74,7 +75,8 @@ private:
void setHeader();
bool m_wasHeaderValue;
bool m_wasHeaderValue = false;
const uint64_t m_timestamp;
http_parser *m_parser;
IHttpListener *m_listener;
std::string m_lastHeaderField;

View file

@ -38,12 +38,12 @@ namespace xmrig {
class HttpData
{
public:
inline HttpData(uint64_t id) : method(0), status(0), m_id(id) {}
inline HttpData(uint64_t id) : m_id(id) {}
inline uint64_t id() const { return m_id; }
int method;
int status;
int method = 0;
int status = 0;
std::map<const std::string, const std::string> headers;
std::string body;
std::string url;

View file

@ -24,20 +24,23 @@
*/
#include <sstream>
#include <string.h>
#include <uv.h>
#include "base/net/http/HttpResponse.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpContext.h"
#include "base/net/http/HttpResponse.h"
#include "base/tools/Baton.h"
#include "base/tools/Object.h"
#include <cinttypes>
#include <cstring>
#include <sstream>
#include <uv.h>
namespace xmrig {
static const char *kCRLF = "\r\n";
static const char *kUserAgent = "user-agent";
@ -45,6 +48,8 @@ static const char *kUserAgent = "user-agent";
class WriteBaton : public Baton<uv_write_t>
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(WriteBaton)
inline WriteBaton(const std::stringstream &ss, const char *data, size_t size, HttpContext *ctx) :
m_ctx(ctx),
m_header(ss.str())
@ -79,7 +84,7 @@ public:
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<WriteBaton *>(req->data); }
uv_buf_t bufs[2];
uv_buf_t bufs[2]{};
private:
HttpContext *m_ctx;
@ -98,7 +103,7 @@ xmrig::HttpResponse::HttpResponse(uint64_t id, int statusCode) :
bool xmrig::HttpResponse::isAlive() const
{
HttpContext *ctx = HttpContext::get(m_id);
auto ctx = HttpContext::get(m_id);
return ctx && uv_is_writable(ctx->stream());
}
@ -129,8 +134,8 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
ss << kCRLF;
HttpContext *ctx = HttpContext::get(m_id);
WriteBaton *baton = new WriteBaton(ss, data, size, ctx);
auto ctx = HttpContext::get(m_id);
auto baton = new WriteBaton(ss, data, size, ctx);
# ifndef APP_DEBUG
if (statusCode() >= 400)
@ -138,13 +143,14 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
{
const bool err = statusCode() >= 400;
Log::print(err ? Log::ERR : Log::INFO, CYAN("%s ") CLEAR MAGENTA_BOLD("%s") WHITE_BOLD(" %s ") CSI "1;%dm%d " CLEAR WHITE_BOLD("%zu ") BLACK_BOLD("\"%s\""),
Log::print(err ? Log::ERR : Log::INFO, CYAN("%s ") CLEAR MAGENTA_BOLD("%s") WHITE_BOLD(" %s ") CSI "1;%dm%d " CLEAR WHITE_BOLD("%zu ") CYAN_BOLD("%" PRIu64 "ms ") BLACK_BOLD("\"%s\""),
ctx->ip().c_str(),
http_method_str(static_cast<http_method>(ctx->method)),
ctx->url.c_str(),
err ? 31 : 32,
statusCode(),
baton->size(),
ctx->elapsed(),
ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr
);
}

View file

@ -49,7 +49,7 @@ xmrig::HttpServer::~HttpServer()
void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
{
HttpContext *ctx = new HttpContext(HTTP_REQUEST, m_listener);
auto ctx = new HttpContext(HTTP_REQUEST, m_listener);
uv_accept(stream, ctx->stream());
uv_read_start(ctx->stream(),
@ -65,11 +65,11 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
},
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
{
HttpContext *ctx = static_cast<HttpContext*>(tcp->data);
auto ctx = static_cast<HttpContext*>(tcp->data);
if (nread >= 0) {
const size_t size = static_cast<size_t>(nread);
const size_t parsed = ctx->parse(buf->base, size);
const auto size = static_cast<size_t>(nread);
const auto parsed = ctx->parse(buf->base, size);
if (parsed < size) {
ctx->close();

View file

@ -28,11 +28,12 @@
#define XMRIG_HTTPSERVER_H
typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings;
using http_parser = struct http_parser;
using http_parser_settings = struct http_parser_settings;
#include "base/kernel/interfaces/ITcpServerListener.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -44,6 +45,8 @@ class IHttpListener;
class HttpServer : public ITcpServerListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpServer)
HttpServer(IHttpListener *listener);
~HttpServer() override;