From 46e49cde0bb1cf65c3215a21de74ec86c65802ef Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 29 Apr 2020 14:17:33 +0700 Subject: [PATCH] Update base. --- src/App.cpp | 2 +- src/backend/cuda/wrappers/CudaLib.cpp | 2 +- src/backend/opencl/wrappers/OclLib.cpp | 6 +-- src/backend/opencl/wrappers/OclLib.h | 4 +- src/base/api/Api.cpp | 2 +- src/base/base.cmake | 8 ++-- src/base/{kernel => io}/Env.cpp | 4 +- src/base/{kernel => io}/Env.h | 0 src/base/{kernel => io}/Signals.cpp | 2 +- src/base/{kernel => io}/Signals.h | 0 src/base/io/log/FileLogWriter.cpp | 2 +- src/base/io/log/Log.h | 4 +- src/base/kernel/Platform.h | 2 - src/base/kernel/Platform_mac.cpp | 20 --------- src/base/kernel/Platform_unix.cpp | 20 --------- src/base/kernel/Platform_win.cpp | 50 +--------------------- src/base/kernel/Process.cpp | 22 +++++++++- src/base/kernel/Process.h | 2 + src/base/kernel/interfaces/IDnsListener.h | 4 +- src/base/kernel/interfaces/ILineListener.h | 8 +++- src/base/net/dns/Dns.cpp | 16 +++---- src/base/net/dns/Dns.h | 13 +++--- src/base/net/http/Fetch.cpp | 47 +++----------------- src/base/net/http/Fetch.h | 9 ++-- src/base/net/http/HttpData.cpp | 13 ++++++ src/base/net/http/HttpData.h | 5 ++- src/base/net/http/HttpListener.cpp | 8 ++-- src/base/net/http/HttpListener.h | 3 +- src/base/net/stratum/BaseClient.cpp | 8 ++-- src/base/net/stratum/DaemonClient.cpp | 4 +- src/base/net/tls/TlsConfig.cpp | 4 +- src/base/net/tls/TlsConfig.h | 2 +- src/base/net/tls/TlsContext.cpp | 2 +- src/base/net/tools/Storage.h | 25 ++++------- src/base/tools/Arguments.cpp | 4 +- src/base/tools/Arguments.h | 2 +- src/base/tools/String.cpp | 8 ++-- src/base/tools/String.h | 14 +++--- src/base/tools/Timer.cpp | 30 +++++++------ src/base/tools/Timer.h | 25 ++++++----- 40 files changed, 162 insertions(+), 244 deletions(-) rename src/base/{kernel => io}/Env.cpp (96%) rename src/base/{kernel => io}/Env.h (100%) rename src/base/{kernel => io}/Signals.cpp (98%) rename src/base/{kernel => io}/Signals.h (100%) diff --git a/src/App.cpp b/src/App.cpp index 106238c80..f57e658f7 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -32,7 +32,7 @@ #include "backend/cpu/Cpu.h" #include "base/io/Console.h" #include "base/io/log/Log.h" -#include "base/kernel/Signals.h" +#include "base/io/Signals.h" #include "base/kernel/Platform.h" #include "core/config/Config.h" #include "core/Controller.h" diff --git a/src/backend/cuda/wrappers/CudaLib.cpp b/src/backend/cuda/wrappers/CudaLib.cpp index db1ff9047..9436f760b 100644 --- a/src/backend/cuda/wrappers/CudaLib.cpp +++ b/src/backend/cuda/wrappers/CudaLib.cpp @@ -28,7 +28,7 @@ #include "backend/cuda/wrappers/CudaLib.h" -#include "base/kernel/Env.h" +#include "base/io/Env.h" #include "crypto/rx/RxAlgo.h" diff --git a/src/backend/opencl/wrappers/OclLib.cpp b/src/backend/opencl/wrappers/OclLib.cpp index d3329e730..b5b12c62f 100644 --- a/src/backend/opencl/wrappers/OclLib.cpp +++ b/src/backend/opencl/wrappers/OclLib.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,8 +31,8 @@ #include "backend/opencl/wrappers/OclLib.h" #include "backend/common/Tags.h" #include "backend/opencl/wrappers/OclError.h" +#include "base/io/Env.h" #include "base/io/log/Log.h" -#include "base/kernel/Env.h" #if defined(OCL_DEBUG_REFERENCE_COUNT) diff --git a/src/backend/opencl/wrappers/OclLib.h b/src/backend/opencl/wrappers/OclLib.h index 9b9e19a8b..b4db1d8d8 100644 --- a/src/backend/opencl/wrappers/OclLib.h +++ b/src/backend/opencl/wrappers/OclLib.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index 535875fb2..85c9dd609 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -31,9 +31,9 @@ #include "base/api/interfaces/IApiListener.h" #include "base/api/requests/HttpApiRequest.h" #include "base/crypto/keccak.h" +#include "base/io/Env.h" #include "base/io/json/Json.h" #include "base/kernel/Base.h" -#include "base/kernel/Env.h" #include "base/tools/Buffer.h" #include "base/tools/Chrono.h" #include "core/config/Config.h" diff --git a/src/base/base.cmake b/src/base/base.cmake index d32f723ea..2957a5652 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -4,6 +4,7 @@ set(HEADERS_BASE src/base/crypto/Coin.h src/base/crypto/keccak.h src/base/io/Console.h + src/base/io/Env.h src/base/io/json/Json.h src/base/io/json/JsonChain.h src/base/io/json/JsonRequest.h @@ -11,12 +12,12 @@ set(HEADERS_BASE src/base/io/log/backends/FileLog.h src/base/io/log/FileLogWriter.h src/base/io/log/Log.h + src/base/io/Signals.h src/base/io/Watcher.h src/base/kernel/Base.h src/base/kernel/config/BaseConfig.h src/base/kernel/config/BaseTransform.h src/base/kernel/Entry.h - src/base/kernel/Env.h src/base/kernel/interfaces/IBaseListener.h src/base/kernel/interfaces/IClient.h src/base/kernel/interfaces/IClientListener.h @@ -34,7 +35,6 @@ set(HEADERS_BASE src/base/kernel/interfaces/IWatcherListener.h src/base/kernel/Platform.h src/base/kernel/Process.h - src/base/kernel/Signals.h src/base/net/dns/Dns.h src/base/net/dns/DnsRecord.h src/base/net/http/Http.h @@ -70,6 +70,7 @@ set(SOURCES_BASE src/base/crypto/Coin.cpp src/base/crypto/keccak.cpp src/base/io/Console.cpp + src/base/io/Env.cpp src/base/io/json/Json.cpp src/base/io/json/JsonChain.cpp src/base/io/json/JsonRequest.cpp @@ -77,15 +78,14 @@ set(SOURCES_BASE src/base/io/log/backends/FileLog.cpp src/base/io/log/FileLogWriter.cpp src/base/io/log/Log.cpp + src/base/io/Signals.cpp src/base/io/Watcher.cpp src/base/kernel/Base.cpp src/base/kernel/config/BaseConfig.cpp src/base/kernel/config/BaseTransform.cpp src/base/kernel/Entry.cpp - src/base/kernel/Env.cpp src/base/kernel/Platform.cpp src/base/kernel/Process.cpp - src/base/kernel/Signals.cpp src/base/net/dns/Dns.cpp src/base/net/dns/DnsRecord.cpp src/base/net/http/Http.cpp diff --git a/src/base/kernel/Env.cpp b/src/base/io/Env.cpp similarity index 96% rename from src/base/kernel/Env.cpp rename to src/base/io/Env.cpp index 9b018e6ed..ae1c27b52 100644 --- a/src/base/kernel/Env.cpp +++ b/src/base/io/Env.cpp @@ -23,7 +23,7 @@ */ -#include "base/kernel/Env.h" +#include "base/io/Env.h" #include "base/kernel/Process.h" #include "version.h" @@ -57,6 +57,8 @@ static std::map variables; static void createVariables() { variables.insert({ "XMRIG_VERSION", APP_VERSION }); + variables.insert({ "XMRIG_KIND", APP_KIND }); + variables.insert({ "XMRIG_HOSTNAME", Env::hostname() }); variables.insert({ "XMRIG_EXE", Process::exepath() }); variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation) }); variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation) }); diff --git a/src/base/kernel/Env.h b/src/base/io/Env.h similarity index 100% rename from src/base/kernel/Env.h rename to src/base/io/Env.h diff --git a/src/base/kernel/Signals.cpp b/src/base/io/Signals.cpp similarity index 98% rename from src/base/kernel/Signals.cpp rename to src/base/io/Signals.cpp index 87825b832..00ec8c174 100644 --- a/src/base/kernel/Signals.cpp +++ b/src/base/io/Signals.cpp @@ -26,8 +26,8 @@ #include +#include "base/io/Signals.h" #include "base/kernel/interfaces/ISignalListener.h" -#include "base/kernel/Signals.h" #include "base/tools/Handle.h" diff --git a/src/base/kernel/Signals.h b/src/base/io/Signals.h similarity index 100% rename from src/base/kernel/Signals.h rename to src/base/io/Signals.h diff --git a/src/base/io/log/FileLogWriter.cpp b/src/base/io/log/FileLogWriter.cpp index 8ae31273c..921d93370 100644 --- a/src/base/io/log/FileLogWriter.cpp +++ b/src/base/io/log/FileLogWriter.cpp @@ -18,7 +18,7 @@ #include "base/io/log/FileLogWriter.h" -#include "base/kernel/Env.h" +#include "base/io/Env.h" #include diff --git a/src/base/io/log/Log.h b/src/base/io/log/Log.h index 45183d1ce..b4675757f 100644 --- a/src/base/io/log/Log.h +++ b/src/base/io/log/Log.h @@ -141,7 +141,9 @@ private: #define LOG_WARN(x, ...) xmrig::Log::print(xmrig::Log::WARNING, x, ##__VA_ARGS__) #define LOG_NOTICE(x, ...) xmrig::Log::print(xmrig::Log::NOTICE, x, ##__VA_ARGS__) #define LOG_INFO(x, ...) xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__) -#define LOG_VERBOSE(x, ...) if (xmrig::Log::isVerbose()) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); } +#define LOG_VERBOSE(x, ...) if (xmrig::Log::verbose() > 0) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); } +#define LOG_V(x, ...) if (xmrig::Log::verbose() > 0) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); } +#define LOG_VV(x, ...) if (xmrig::Log::verbose() > 1) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); } #ifdef APP_DEBUG # define LOG_DEBUG(x, ...) xmrig::Log::print(xmrig::Log::DEBUG, x, ##__VA_ARGS__) diff --git a/src/base/kernel/Platform.h b/src/base/kernel/Platform.h index 341b4dfd2..3fcf666be 100644 --- a/src/base/kernel/Platform.h +++ b/src/base/kernel/Platform.h @@ -48,9 +48,7 @@ public: } static bool setThreadAffinity(uint64_t cpu_id); - static uint32_t setTimerResolution(uint32_t resolution); static void init(const char *userAgent); - static void restoreTimerResolution(); static void setProcessPriority(int priority); static void setThreadPriority(int priority); diff --git a/src/base/kernel/Platform_mac.cpp b/src/base/kernel/Platform_mac.cpp index aab006759..5a5520eb2 100644 --- a/src/base/kernel/Platform_mac.cpp +++ b/src/base/kernel/Platform_mac.cpp @@ -34,10 +34,6 @@ #include "base/kernel/Platform.h" #include "version.h" -#ifdef XMRIG_NVIDIA_PROJECT -# include "nvidia/cryptonight.h" -#endif - char *xmrig::Platform::createUserAgent() { @@ -46,11 +42,6 @@ char *xmrig::Platform::createUserAgent() char *buf = new char[max](); int length = snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s", APP_NAME, APP_VERSION, uv_version_string()); -# ifdef XMRIG_NVIDIA_PROJECT - const int cudaVersion = cuda_get_runtime_version(); - length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); -# endif - # ifdef __clang__ length += snprintf(buf + length, max - length, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); # elif defined(__GNUC__) @@ -75,17 +66,6 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) #endif -uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) -{ - return resolution; -} - - -void xmrig::Platform::restoreTimerResolution() -{ -} - - void xmrig::Platform::setProcessPriority(int) { } diff --git a/src/base/kernel/Platform_unix.cpp b/src/base/kernel/Platform_unix.cpp index ba55ed77c..6d9576363 100644 --- a/src/base/kernel/Platform_unix.cpp +++ b/src/base/kernel/Platform_unix.cpp @@ -43,10 +43,6 @@ #include "base/kernel/Platform.h" #include "version.h" -#ifdef XMRIG_NVIDIA_PROJECT -# include "nvidia/cryptonight.h" -#endif - #ifdef __FreeBSD__ typedef cpuset_t cpu_set_t; @@ -70,11 +66,6 @@ char *xmrig::Platform::createUserAgent() length += snprintf(buf + length, max - length, "i686) libuv/%s", uv_version_string()); # endif -# ifdef XMRIG_NVIDIA_PROJECT - const int cudaVersion = cuda_get_runtime_version(); - length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); -# endif - # ifdef __clang__ length += snprintf(buf + length, max - length, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); # elif defined(__GNUC__) @@ -104,17 +95,6 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) #endif -uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) -{ - return resolution; -} - - -void xmrig::Platform::restoreTimerResolution() -{ -} - - void xmrig::Platform::setProcessPriority(int) { } diff --git a/src/base/kernel/Platform_win.cpp b/src/base/kernel/Platform_win.cpp index ce7e60e21..018dba31f 100644 --- a/src/base/kernel/Platform_win.cpp +++ b/src/base/kernel/Platform_win.cpp @@ -29,23 +29,12 @@ #include "base/kernel/Platform.h" -#include "base/io/log/Log.h" #include "version.h" -#ifdef XMRIG_NVIDIA_PROJECT -# include "nvidia/cryptonight.h" -#endif - - -#ifdef XMRIG_AMD_PROJECT -static uint32_t timerResolution = 0; -#endif - - static inline OSVERSIONINFOEX winOsVersion() { - typedef NTSTATUS (NTAPI *RtlGetVersionFunction)(LPOSVERSIONINFO); + using RtlGetVersionFunction = NTSTATUS (*)(LPOSVERSIONINFO); OSVERSIONINFOEX result = { sizeof(OSVERSIONINFOEX), 0, 0, 0, 0, {'\0'}, 0, 0, 0, 0, 0}; HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); @@ -75,11 +64,6 @@ char *xmrig::Platform::createUserAgent() length += snprintf(buf + length, max - length, ") libuv/%s", uv_version_string()); # endif -# ifdef XMRIG_NVIDIA_PROJECT - const int cudaVersion = cuda_get_runtime_version(); - length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); -# endif - # ifdef __GNUC__ length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); # elif _MSC_VER @@ -93,10 +77,6 @@ char *xmrig::Platform::createUserAgent() #ifndef XMRIG_FEATURE_HWLOC bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) { - if (cpu_id >= 64) { - LOG_ERR("Unable to set affinity. Windows supports only affinity up to 63."); - } - const bool result = (SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0); Sleep(1); return result; @@ -104,34 +84,6 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) #endif -uint32_t xmrig::Platform::setTimerResolution(uint32_t resolution) -{ -# ifdef XMRIG_AMD_PROJECT - TIMECAPS tc; - - if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) { - return 0; - } - - timerResolution = std::min(std::max(tc.wPeriodMin, resolution), tc.wPeriodMax); - - return timeBeginPeriod(timerResolution) == TIMERR_NOERROR ? timerResolution : 0; -# else - return resolution; -# endif -} - - -void xmrig::Platform::restoreTimerResolution() -{ -# ifdef XMRIG_AMD_PROJECT - if (timerResolution) { - timeEndPeriod(timerResolution); - } -# endif -} - - void xmrig::Platform::setProcessPriority(int priority) { if (priority == -1) { diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 34f5d02b1..36dcae78b 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.cpp @@ -115,7 +115,27 @@ xmrig::Process::Process(int argc, char **argv) : { srand(static_cast(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast(this))); - setDataDir(m_arguments.value("--data-dir")); + setDataDir(m_arguments.value("--data-dir", "-d")); +} + + +int xmrig::Process::pid() +{ +# if UV_VERSION_HEX >= 0x011200 + return uv_os_getpid(); +# else + return 0; +# endif +} + + +int xmrig::Process::ppid() +{ +# if UV_VERSION_HEX >= 0x011000 + return uv_os_getppid(); +# else + return 0; +# endif } diff --git a/src/base/kernel/Process.h b/src/base/kernel/Process.h index 29f247f2b..a97a3dccb 100644 --- a/src/base/kernel/Process.h +++ b/src/base/kernel/Process.h @@ -51,6 +51,8 @@ public: Process(int argc, char **argv); + static int pid(); + static int ppid(); static String exepath(); static String location(Location location, const char *fileName = nullptr); diff --git a/src/base/kernel/interfaces/IDnsListener.h b/src/base/kernel/interfaces/IDnsListener.h index 3683ea51d..4cbb3f7e4 100644 --- a/src/base/kernel/interfaces/IDnsListener.h +++ b/src/base/kernel/interfaces/IDnsListener.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/interfaces/ILineListener.h b/src/base/kernel/interfaces/ILineListener.h index 2759dcecb..a7d349012 100644 --- a/src/base/kernel/interfaces/ILineListener.h +++ b/src/base/kernel/interfaces/ILineListener.h @@ -26,6 +26,9 @@ #define XMRIG_ILINELISTENER_H +#include "base/tools/Object.h" + + #include @@ -35,7 +38,10 @@ namespace xmrig { class ILineListener { public: - virtual ~ILineListener() = default; + XMRIG_DISABLE_COPY_MOVE(ILineListener) + + ILineListener() = default; + virtual ~ILineListener() = default; virtual void onLine(char *line, size_t size) = 0; }; diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index 40d2a6e33..6246421c0 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,9 +23,8 @@ */ -#include "base/kernel/interfaces/IDnsListener.h" #include "base/net/dns/Dns.h" -#include "base/tools/Handle.h" +#include "base/kernel/interfaces/IDnsListener.h" namespace xmrig { @@ -35,10 +34,7 @@ namespace xmrig { xmrig::Dns::Dns(IDnsListener *listener) : - m_hints(), - m_listener(listener), - m_status(0), - m_resolver(nullptr) + m_listener(listener) { m_key = m_storage.add(this); @@ -134,11 +130,11 @@ void xmrig::Dns::onResolved(int status, addrinfo *res) addrinfo *ptr = res; while (ptr != nullptr) { if (ptr->ai_family == AF_INET) { - m_ipv4.push_back(ptr); + m_ipv4.emplace_back(ptr); } if (ptr->ai_family == AF_INET6) { - m_ipv6.push_back(ptr); + m_ipv6.emplace_back(ptr); } ptr = ptr->ai_next; diff --git a/src/base/net/dns/Dns.h b/src/base/net/dns/Dns.h index 11f5bf80a..068ab80f3 100644 --- a/src/base/net/dns/Dns.h +++ b/src/base/net/dns/Dns.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +32,7 @@ #include "base/net/dns/DnsRecord.h" #include "base/net/tools/Storage.h" +#include "base/tools/Object.h" #include "base/tools/String.h" @@ -44,6 +45,8 @@ class IDnsListener; class Dns { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Dns) + Dns(IDnsListener *listener); ~Dns(); @@ -62,14 +65,14 @@ private: static void onResolved(uv_getaddrinfo_t *req, int status, addrinfo *res); - addrinfo m_hints; + addrinfo m_hints{}; IDnsListener *m_listener; - int m_status; + int m_status = 0; std::vector m_ipv4; std::vector m_ipv6; String m_host; uintptr_t m_key; - uv_getaddrinfo_t *m_resolver; + uv_getaddrinfo_t *m_resolver = nullptr; static Storage m_storage; }; diff --git a/src/base/net/http/Fetch.cpp b/src/base/net/http/Fetch.cpp index a0eb2b555..4d18b71d0 100644 --- a/src/base/net/http/Fetch.cpp +++ b/src/base/net/http/Fetch.cpp @@ -18,12 +18,11 @@ #include "base/net/http/Fetch.h" +#include "3rdparty/rapidjson/document.h" +#include "3rdparty/rapidjson/stringbuffer.h" +#include "3rdparty/rapidjson/writer.h" #include "base/io/log/Log.h" #include "base/net/http/HttpClient.h" -#include "base/net/stratum/Pool.h" -#include "rapidjson/document.h" -#include "rapidjson/stringbuffer.h" -#include "rapidjson/writer.h" #ifdef XMRIG_FEATURE_TLS @@ -45,7 +44,7 @@ xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16 } -xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Document &doc, bool tls, bool quiet) : +xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Value &value, bool tls, bool quiet) : quiet(quiet), tls(tls), method(method), @@ -55,42 +54,10 @@ xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16 { assert(port > 0); - setBody(doc); + setBody(value); } -xmrig::FetchRequest::FetchRequest(int method, const Pool &pool, const String &path, bool quiet, const char *data, size_t size, const char *contentType) : - quiet(quiet), - tls(pool.isTLS()), - method(static_cast(method)), - fingerprint(pool.fingerprint()), - host(pool.host()), - path(path), - port(pool.port()) -{ - assert(pool.isValid()); - - setBody(data, size, contentType); -} - - - -xmrig::FetchRequest::FetchRequest(int method, const Pool &pool, const String &path, const rapidjson::Document &doc, bool quiet) : - quiet(quiet), - tls(pool.isTLS()), - method(static_cast(method)), - fingerprint(pool.fingerprint()), - host(pool.host()), - path(path), - port(pool.port()) -{ - assert(pool.isValid()); - - setBody(doc); -} - - - void xmrig::FetchRequest::setBody(const char *data, size_t size, const char *contentType) { if (!data) { @@ -110,7 +77,7 @@ void xmrig::FetchRequest::setBody(const char *data, size_t size, const char *con } -void xmrig::FetchRequest::setBody(const rapidjson::Document &doc) +void xmrig::FetchRequest::setBody(const rapidjson::Value &value) { assert(method != HTTP_GET && method != HTTP_HEAD); @@ -122,7 +89,7 @@ void xmrig::FetchRequest::setBody(const rapidjson::Document &doc) StringBuffer buffer(nullptr, 512); Writer writer(buffer); - doc.Accept(writer); + value.Accept(writer); setBody(buffer.GetString(), buffer.GetSize(), HttpData::kApplicationJson.c_str()); } diff --git a/src/base/net/http/Fetch.h b/src/base/net/http/Fetch.h index db1e899b6..b6fbf487d 100644 --- a/src/base/net/http/Fetch.h +++ b/src/base/net/http/Fetch.h @@ -22,8 +22,8 @@ #include "3rdparty/http-parser/http_parser.h" +#include "3rdparty/rapidjson/fwd.h" #include "base/tools/String.h" -#include "rapidjson/fwd.h" #include @@ -35,7 +35,6 @@ namespace xmrig { class IHttpListener; -class Pool; class FetchRequest @@ -43,12 +42,10 @@ class FetchRequest public: FetchRequest() = default; FetchRequest(http_method method, const String &host, uint16_t port, const String &path, bool tls = false, bool quiet = false, const char *data = nullptr, size_t size = 0, const char *contentType = nullptr); - FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Document &doc, bool tls = false, bool quiet = false); - FetchRequest(int method, const Pool &pool, const String &path, bool quiet = false, const char *data = nullptr, size_t size = 0, const char *contentType = nullptr); - FetchRequest(int method, const Pool &pool, const String &path, const rapidjson::Document &doc, bool quiet = false); + FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Value &value, bool tls = false, bool quiet = false); void setBody(const char *data, size_t size, const char *contentType = nullptr); - void setBody(const rapidjson::Document &doc); + void setBody(const rapidjson::Value &value); inline bool hasBody() const { return method != HTTP_GET && method != HTTP_HEAD && !body.empty(); } diff --git a/src/base/net/http/HttpData.cpp b/src/base/net/http/HttpData.cpp index 5aa575a80..6e75e2ac6 100644 --- a/src/base/net/http/HttpData.cpp +++ b/src/base/net/http/HttpData.cpp @@ -26,6 +26,19 @@ namespace xmrig { const std::string HttpData::kApplicationJson = "application/json"; const std::string HttpData::kContentType = "Content-Type"; const std::string HttpData::kContentTypeL = "content-type"; +const std::string HttpData::kTextPlain = "text/plain"; } // namespace xmrig + + +bool xmrig::HttpData::isJSON() const +{ + if (!headers.count(kContentTypeL)) { + return false; + } + + auto &type = headers.at(kContentTypeL); + + return type == kApplicationJson || type == kTextPlain; +} diff --git a/src/base/net/http/HttpData.h b/src/base/net/http/HttpData.h index 82a8ee8f8..2e8b78f8f 100644 --- a/src/base/net/http/HttpData.h +++ b/src/base/net/http/HttpData.h @@ -46,12 +46,13 @@ public: static const std::string kApplicationJson; static const std::string kContentType; static const std::string kContentTypeL; + static const std::string kTextPlain; inline HttpData(uint64_t id) : m_id(id) {} virtual ~HttpData() = default; - inline uint64_t id() const { return m_id; } + inline uint64_t id() const { return m_id; } virtual bool isRequest() const = 0; virtual const char *host() const = 0; @@ -61,6 +62,8 @@ public: virtual uint16_t port() const = 0; virtual void write(std::string &&data, bool close) = 0; + bool isJSON() const; + int method = 0; int status = 0; int userType = 0; diff --git a/src/base/net/http/HttpListener.cpp b/src/base/net/http/HttpListener.cpp index e58d5e55e..077184fcb 100644 --- a/src/base/net/http/HttpListener.cpp +++ b/src/base/net/http/HttpListener.cpp @@ -27,11 +27,11 @@ void xmrig::HttpListener::onHttpData(const HttpData &data) { # ifdef APP_DEBUG if (!data.isRequest()) { - LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\" ") CSI "1;%dm%d" CLEAR BLACK_BOLD(" received: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"), - data.tlsVersion() ? "s" : "", data.host(), data.port(), http_method_str(static_cast(data.method)), data.url.data(), - data.status >= 400 ? 31 : 32, data.status, data.body.size()); + LOG_DEBUG("%s " CYAN_BOLD("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\" ") CSI "1;%dm%d" CLEAR BLACK_BOLD(" received: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"), + m_tag, data.tlsVersion() ? "s" : "", data.host(), data.port(), http_method_str(static_cast(data.method)), data.url.data(), + (data.status >= 400 || data.status < 0) ? 31 : 32, data.status, data.body.size()); - if (data.body.size() < (Log::kMaxBufferSize - 1024) && data.headers.count(HttpData::kContentTypeL) && data.headers.at(HttpData::kContentTypeL) == HttpData::kApplicationJson) { + if (data.body.size() < (Log::kMaxBufferSize - 1024) && data.isJSON()) { Log::print(BLUE_BG_BOLD("%s:") BLACK_BOLD_S " %.*s", data.headers.at(HttpData::kContentTypeL).c_str(), static_cast(data.body.size()), data.body.c_str()); } } diff --git a/src/base/net/http/HttpListener.h b/src/base/net/http/HttpListener.h index 36975db48..ac302dab9 100644 --- a/src/base/net/http/HttpListener.h +++ b/src/base/net/http/HttpListener.h @@ -29,12 +29,13 @@ namespace xmrig { class HttpListener : public IHttpListener { public: - inline HttpListener(IHttpListener *listener) : m_listener(listener) {} + inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : m_tag(tag), m_listener(listener) {} protected: void onHttpData(const HttpData &data) override; private: + const char *m_tag; IHttpListener *m_listener; }; diff --git a/src/base/net/stratum/BaseClient.cpp b/src/base/net/stratum/BaseClient.cpp index 677b0e376..6072a666a 100644 --- a/src/base/net/stratum/BaseClient.cpp +++ b/src/base/net/stratum/BaseClient.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,10 +24,10 @@ #include "base/net/stratum/BaseClient.h" -#include "base/kernel/Env.h" +#include "3rdparty/rapidjson/document.h" +#include "base/io/Env.h" #include "base/kernel/interfaces/IClientListener.h" #include "base/net/stratum/SubmitResult.h" -#include "rapidjson/document.h" namespace xmrig { diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index bdf6d5ca1..89a8e67eb 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -328,7 +328,7 @@ int64_t xmrig::DaemonClient::getBlockTemplate() int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc) { - FetchRequest req(HTTP_POST, m_pool, kJsonRPC, doc, isQuiet()); + FetchRequest req(HTTP_POST, m_pool.host(), m_pool.port(), kJsonRPC, doc, m_pool.isTLS(), isQuiet()); fetch(std::move(req), m_httpListener); return m_sequence++; @@ -355,7 +355,7 @@ void xmrig::DaemonClient::retry() void xmrig::DaemonClient::send(const char *path) { - FetchRequest req(HTTP_GET, m_pool, path, isQuiet()); + FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet()); fetch(std::move(req), m_httpListener); } diff --git a/src/base/net/tls/TlsConfig.cpp b/src/base/net/tls/TlsConfig.cpp index 49b1970d0..2e3dc903b 100644 --- a/src/base/net/tls/TlsConfig.cpp +++ b/src/base/net/tls/TlsConfig.cpp @@ -25,10 +25,10 @@ #include "base/net/tls/TlsConfig.h" +#include "3rdparty/rapidjson/document.h" #include "base/io/json/Json.h" #include "base/io/log/Log.h" #include "base/net/tls/TlsGen.h" -#include "rapidjson/document.h" namespace xmrig { @@ -86,7 +86,7 @@ xmrig::TlsConfig::TlsConfig(const rapidjson::Value &value) generate(); } } -# ifdef XMRIG_PROXY_PROJECT +# ifdef XMRIG_FORCE_TLS else if (value.IsNull()) { generate(); } diff --git a/src/base/net/tls/TlsConfig.h b/src/base/net/tls/TlsConfig.h index 4e0fe7879..c5407f321 100644 --- a/src/base/net/tls/TlsConfig.h +++ b/src/base/net/tls/TlsConfig.h @@ -27,8 +27,8 @@ #define XMRIG_TLSCONFIG_H +#include "3rdparty/rapidjson/fwd.h" #include "base/tools/String.h" -#include "rapidjson/fwd.h" namespace xmrig { diff --git a/src/base/net/tls/TlsContext.cpp b/src/base/net/tls/TlsContext.cpp index d2ab71c5a..03c79e654 100644 --- a/src/base/net/tls/TlsContext.cpp +++ b/src/base/net/tls/TlsContext.cpp @@ -25,8 +25,8 @@ #include "base/net/tls/TlsContext.h" +#include "base/io/Env.h" #include "base/io/log/Log.h" -#include "base/kernel/Env.h" #include "base/net/tls/TlsConfig.h" diff --git a/src/base/net/tools/Storage.h b/src/base/net/tools/Storage.h index 0af4db263..e23e86665 100644 --- a/src/base/net/tools/Storage.h +++ b/src/base/net/tools/Storage.h @@ -1,10 +1,4 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , * Copyright 2018-2020 SChernykh * Copyright 2016-2020 XMRig , * @@ -48,10 +42,10 @@ public: } - inline static void *ptr(uintptr_t id) { return reinterpret_cast(id); } + inline void *ptr(uintptr_t id) { return reinterpret_cast(id); } - inline TYPE *get(const void *id) const { return get(reinterpret_cast(id)); } + inline TYPE *get(const void *id) const { return get(reinterpret_cast(id)); } inline TYPE *get(uintptr_t id) const { assert(m_data.count(id) > 0); @@ -63,19 +57,16 @@ public: } - inline void remove(const void *id) { delete release(reinterpret_cast(id)); } - inline void remove(uintptr_t id) { delete release(id); } + inline void remove(const void *id) { delete release(reinterpret_cast(id)); } + inline void remove(uintptr_t id) { delete release(id); } - inline TYPE *release(const void *id) { release(reinterpret_cast(id)); } + inline TYPE *release(const void *id) { return release(reinterpret_cast(id)); } inline TYPE *release(uintptr_t id) { - TYPE *obj = get(id); + auto obj = get(id); if (obj != nullptr) { - auto it = m_data.find(id); - if (it != m_data.end()) { - m_data.erase(it); - } + m_data.erase(id); } return obj; @@ -84,7 +75,7 @@ public: private: std::map m_data; - uint64_t m_counter = 0; + uintptr_t m_counter = 0; }; diff --git a/src/base/tools/Arguments.cpp b/src/base/tools/Arguments.cpp index d0352939b..eebe0e736 100644 --- a/src/base/tools/Arguments.cpp +++ b/src/base/tools/Arguments.cpp @@ -52,7 +52,7 @@ bool xmrig::Arguments::hasArg(const char *name) const } -const char *xmrig::Arguments::value(const char *key) const +const char *xmrig::Arguments::value(const char *key1, const char *key2) const { const size_t size = m_data.size(); if (size < 3) { @@ -60,7 +60,7 @@ const char *xmrig::Arguments::value(const char *key) const } for (size_t i = 1; i < size - 1; ++i) { - if (m_data[i] == key) { + if (m_data[i] == key1 || (key2 && m_data[i] == key2)) { return m_data[i + 1]; } } diff --git a/src/base/tools/Arguments.h b/src/base/tools/Arguments.h index 0016c519f..e7a105363 100644 --- a/src/base/tools/Arguments.h +++ b/src/base/tools/Arguments.h @@ -41,7 +41,7 @@ public: Arguments(int argc, char **argv); bool hasArg(const char *name) const; - const char *value(const char *key) const; + const char *value(const char *key1, const char *key2 = nullptr) const; inline char **argv() const { return m_argv; } inline const std::vector &data() const { return m_data; } diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp index b11d67745..f93222746 100644 --- a/src/base/tools/String.cpp +++ b/src/base/tools/String.cpp @@ -23,11 +23,11 @@ */ -#include - - #include "base/tools/String.h" -#include "rapidjson/document.h" +#include "3rdparty/rapidjson/document.h" + + +#include xmrig::String::String(const char *str) : diff --git a/src/base/tools/String.h b/src/base/tools/String.h index 0f3656410..df6495768 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,13 +26,13 @@ #define XMRIG_STRING_H +#include "3rdparty/rapidjson/fwd.h" + + #include #include -#include "rapidjson/fwd.h" - - namespace xmrig { @@ -85,11 +85,11 @@ public: rapidjson::Value toJSON() const; rapidjson::Value toJSON(rapidjson::Document &doc) const; - std::vector split(char sep) const; + std::vector split(char sep) const; String &toLower(); String &toUpper(); - static String join(const std::vector &vec, char sep); + static String join(const std::vector &vec, char sep); private: void copy(const char *str); diff --git a/src/base/tools/Timer.cpp b/src/base/tools/Timer.cpp index d06df1636..858d1219e 100644 --- a/src/base/tools/Timer.cpp +++ b/src/base/tools/Timer.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,22 +17,20 @@ */ +#include "base/tools/Timer.h" #include "base/kernel/interfaces/ITimerListener.h" #include "base/tools/Handle.h" -#include "base/tools/Timer.h" xmrig::Timer::Timer(ITimerListener *listener) : - m_listener(listener), - m_timer(nullptr) + m_listener(listener) { init(); } xmrig::Timer::Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat) : - m_listener(listener), - m_timer(nullptr) + m_listener(listener) { init(); start(timeout, repeat); @@ -63,6 +55,15 @@ void xmrig::Timer::setRepeat(uint64_t repeat) } +void xmrig::Timer::singleShot(uint64_t timeout, int id) +{ + m_id = id; + + stop(); + start(timeout, 0); +} + + void xmrig::Timer::start(uint64_t timeout, uint64_t repeat) { uv_timer_start(m_timer, onTimer, timeout, repeat); @@ -71,6 +72,7 @@ void xmrig::Timer::start(uint64_t timeout, uint64_t repeat) void xmrig::Timer::stop() { + setRepeat(0); uv_timer_stop(m_timer); } @@ -85,7 +87,7 @@ void xmrig::Timer::init() void xmrig::Timer::onTimer(uv_timer_t *handle) { - const Timer *timer = static_cast(handle->data); + const auto timer = static_cast(handle->data); timer->m_listener->onTimer(timer); } diff --git a/src/base/tools/Timer.h b/src/base/tools/Timer.h index e0e210f52..8fd040f83 100644 --- a/src/base/tools/Timer.h +++ b/src/base/tools/Timer.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,10 +20,13 @@ #define XMRIG_TIMER_H -#include +using uv_timer_t = struct uv_timer_s; -typedef struct uv_timer_s uv_timer_t; +#include "base/tools/Object.h" + + +#include namespace xmrig { @@ -41,12 +38,17 @@ class ITimerListener; class Timer { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Timer); + Timer(ITimerListener *listener); Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat); ~Timer(); + inline int id() const { return m_id; } + uint64_t repeat() const; void setRepeat(uint64_t repeat); + void singleShot(uint64_t timeout, int id = 0); void start(uint64_t timeout, uint64_t repeat); void stop(); @@ -55,8 +57,9 @@ private: static void onTimer(uv_timer_t *handle); + int m_id = 0; ITimerListener *m_listener; - uv_timer_t *m_timer; + uv_timer_t *m_timer = nullptr; };