Refactoring.

This commit is contained in:
XMRig 2018-04-11 03:52:23 +07:00
parent 3924a16048
commit ad7545d149
10 changed files with 197 additions and 190 deletions

View file

@ -22,6 +22,7 @@ set(HEADERS
src/core/ConfigLoader_platform.h
src/core/ConfigWatcher.cpp
src/core/Controller.h
src/core/utils/c_str.h
src/Cpu.h
src/interfaces/IClientListener.h
src/interfaces/IConfig.h

View file

@ -73,11 +73,6 @@ xmrig::CommonConfig::CommonConfig() :
m_watch(false), // TODO: enable config file watch by default when this feature propertly handled and tested.
# endif
m_apiToken(nullptr),
m_apiWorkerId(nullptr),
m_fileName(nullptr),
m_logFile(nullptr),
m_userAgent(nullptr),
m_apiPort(0),
m_donateLevel(kDefaultDonateLevel),
m_printTime(60),
@ -100,12 +95,6 @@ xmrig::CommonConfig::~CommonConfig()
}
m_pools.clear();
free(m_fileName);
free(m_apiToken);
free(m_apiWorkerId);
free(m_logFile);
free(m_userAgent);
}
@ -223,23 +212,19 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg)
break;
case LogFileKey: /* --log-file */
free(m_logFile);
m_logFile = strdup(arg);
m_logFile = arg;
break;
case ApiAccessTokenKey: /* --api-access-token */
free(m_apiToken);
m_apiToken = strdup(arg);
m_apiToken = arg;
break;
case ApiWorkerIdKey: /* --api-worker-id */
free(m_apiWorkerId);
m_apiWorkerId = strdup(arg);
m_apiWorkerId = arg;
break;
case UserAgentKey: /* --user-agent */
free(m_userAgent);
m_userAgent = strdup(arg);
m_userAgent = arg;
break;
case RetriesKey: /* --retries */
@ -286,12 +271,12 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg)
bool xmrig::CommonConfig::save()
{
if (!m_fileName) {
if (m_fileName.isNull()) {
return false;
}
uv_fs_t req;
const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName, O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr);
const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr);
if (fd < 0) {
return false;
}
@ -320,8 +305,7 @@ bool xmrig::CommonConfig::save()
void xmrig::CommonConfig::setFileName(const char *fileName)
{
free(m_fileName);
m_fileName = fileName ? strdup(fileName) : nullptr;
m_fileName = fileName;
}

View file

@ -28,6 +28,7 @@
#include <vector>
#include "core/utils/c_str.h"
#include "interfaces/IConfig.h"
#include "xmrig.h"
@ -53,10 +54,10 @@ public:
inline bool isColors() const { return m_colors; }
inline bool isSyslog() const { return m_syslog; }
inline const char *algoName() const { return algoName(m_algorithm); }
inline const char *apiToken() const { return m_apiToken; }
inline const char *apiWorkerId() const { return m_apiWorkerId; }
inline const char *logFile() const { return m_logFile; }
inline const char *userAgent() const { return m_userAgent; }
inline const char *apiToken() const { return m_apiToken.data(); }
inline const char *apiWorkerId() const { return m_apiWorkerId.data(); }
inline const char *logFile() const { return m_logFile.data(); }
inline const char *userAgent() const { return m_userAgent.data(); }
inline const std::vector<Url*> &pools() const { return m_pools; }
inline int apiPort() const { return m_apiPort; }
inline int donateLevel() const { return m_donateLevel; }
@ -65,8 +66,8 @@ public:
inline int retryPause() const { return m_retryPause; }
inline void setColors(bool colors) { m_colors = colors; }
inline bool isWatch() const override { return m_watch && m_fileName; }
inline const char *fileName() const override { return m_fileName; }
inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); }
inline const char *fileName() const override { return m_fileName.data(); }
protected:
bool adjust() override;
@ -85,17 +86,17 @@ protected:
bool m_colors;
bool m_syslog;
bool m_watch;
char *m_apiToken;
char *m_apiWorkerId;
char *m_fileName;
char *m_logFile;
char *m_userAgent;
int m_apiPort;
int m_donateLevel;
int m_printTime;
int m_retries;
int m_retryPause;
std::vector<Url*> m_pools;
xmrig::c_str m_apiToken;
xmrig::c_str m_apiWorkerId;
xmrig::c_str m_fileName;
xmrig::c_str m_logFile;
xmrig::c_str m_userAgent;
private:
bool parseInt(int key, int arg);

View file

@ -33,9 +33,9 @@
xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) :
m_path(strdup(path)),
m_creator(creator),
m_listener(listener)
m_listener(listener),
m_path(path)
{
uv_fs_event_init(uv_default_loop(), &m_fsEvent);
uv_timer_init(uv_default_loop(), &m_timer);
@ -50,8 +50,6 @@ xmrig::ConfigWatcher::~ConfigWatcher()
{
uv_timer_stop(&m_timer);
uv_fs_event_stop(&m_fsEvent);
free(m_path);
}
@ -80,10 +78,10 @@ void xmrig::ConfigWatcher::queueUpdate()
void xmrig::ConfigWatcher::reload()
{
LOG_WARN("\"%s\" was changed, reloading configuration", m_path);
LOG_WARN("\"%s\" was changed, reloading configuration", m_path.data());
IConfig *config = m_creator->create();
ConfigLoader::loadFromFile(config, m_path);
ConfigLoader::loadFromFile(config, m_path.data());
if (!config->isValid()) {
LOG_ERR("reloading failed");
@ -103,5 +101,5 @@ void xmrig::ConfigWatcher::reload()
void xmrig::ConfigWatcher::start()
{
uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path, 0);
uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path.data(), 0);
}

View file

@ -28,6 +28,8 @@
#include <stdint.h>
#include <uv.h>
#include "core/utils/c_str.h"
#include "rapidjson/fwd.h"
@ -56,11 +58,11 @@ private:
void reload();
void start();
char *m_path;
IConfigCreator *m_creator;
IWatcherListener *m_listener;
uv_fs_event_t m_fsEvent;
uv_timer_t m_timer;
xmrig::c_str m_path;
};

94
src/core/utils/c_str.h Normal file
View file

@ -0,0 +1,94 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __C_STR_H__
#define __C_STR_H__
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
namespace xmrig {
/**
* @brief Simple C string wrapper.
*
* 1. I know about std:string.
* 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds.
*/
class c_str
{
public:
inline c_str() : m_data(nullptr) {}
inline c_str(const char *str) : m_data(nullptr) { set(str); }
inline ~c_str() { free(m_data); }
inline void set(const char *str)
{
free(m_data);
m_data = str != nullptr ? strdup(str) : nullptr;
}
inline void set(char *str)
{
free(m_data);
m_data = str;
}
inline bool isEqual(const char *str) const
{
return (m_data != nullptr && str != nullptr && strcmp(m_data, str)) || (m_data == nullptr && m_data == nullptr);
}
inline bool isNull() const { return m_data == nullptr; }
inline const char *data() const { return m_data; }
inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); }
inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); }
inline bool operator!=(const char *str) const { return !isEqual(str); }
inline bool operator==(const c_str &str) const { return isEqual(str.data()); }
inline bool operator==(const char *str) const { return isEqual(str); }
inline c_str &operator=(char *str) { set(str); return *this; }
inline c_str &operator=(const c_str &str) { set(str.data()); return *this; }
inline c_str &operator=(const char *str) { set(str); return *this; }
private:
char *m_data;
};
} /* namespace xmrig */
#endif /* __C_STR_H__ */

View file

@ -138,7 +138,7 @@ void Client::setUrl(const Url *url)
return;
}
m_url = url;
m_url = *url;
}

View file

@ -30,6 +30,7 @@
#include <vector>
#include "core/utils/c_str.h"
#include "net/Id.h"
#include "net/Job.h"
#include "net/Storage.h"

View file

@ -22,13 +22,13 @@
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "net/Url.h"
#include "xmrig.h"
#ifdef _MSC_VER
@ -38,14 +38,10 @@
Url::Url() :
m_nicehash(false),
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_algo(xmrig::CRYPTONIGHT),
m_keepAlive(0),
m_variant(xmrig::VARIANT_AUTO),
m_url(nullptr),
m_port(kDefaultPort)
m_port(kDefaultPort),
m_algo(xmrig::CRYPTONIGHT),
m_variant(xmrig::VARIANT_AUTO)
{
}
@ -63,47 +59,40 @@ Url::Url() :
*/
Url::Url(const char *url) :
m_nicehash(false),
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_algo(xmrig::CRYPTONIGHT),
m_keepAlive(0),
m_variant(xmrig::VARIANT_AUTO),
m_url(nullptr),
m_port(kDefaultPort)
m_port(kDefaultPort),
m_algo(xmrig::CRYPTONIGHT),
m_url(url),
m_variant(xmrig::VARIANT_AUTO)
{
parse(url);
}
Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, int variant) :
Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) :
m_nicehash(nicehash),
m_password(password ? strdup(password) : nullptr),
m_user(user ? strdup(user) : nullptr),
m_algo(xmrig::CRYPTONIGHT),
m_keepAlive(keepAlive),
m_variant(variant),
m_url(nullptr),
m_port(port)
m_port(port),
m_algo(xmrig::CRYPTONIGHT),
m_host(host),
m_password(password),
m_user(user),
m_variant(variant)
{
m_host = strdup(host);
}
const size_t size = m_host.size() + 8;
assert(size > 8);
char *url = new char[size]();
snprintf(url, size - 1, "%s:%d", m_host.data(), m_port);
Url::~Url()
{
free(m_host);
free(m_password);
free(m_user);
if (m_url) {
delete [] m_url;
}
m_url = url;
}
bool Url::parse(const char *url)
{
assert(url != nullptr);
const char *p = strstr(url, "://");
const char *base = url;
@ -130,10 +119,12 @@ bool Url::parse(const char *url)
}
const size_t size = port++ - base + 1;
m_host = new char[size]();
memcpy(m_host, base, size - 1);
char *host = new char[size]();
memcpy(host, base, size - 1);
m_host = host;
m_port = static_cast<uint16_t>(strtol(port, nullptr, 10));
m_port = (uint16_t) strtol(port, nullptr, 10);
return true;
}
@ -145,31 +136,17 @@ bool Url::setUserpass(const char *userpass)
return false;
}
free(m_user);
free(m_password);
char *user = new char[p - userpass + 1]();
strncpy(user, userpass, p - userpass);
m_user = static_cast<char*>(calloc(p - userpass + 1, 1));
strncpy(m_user, userpass, p - userpass);
m_password = strdup(p + 1);
m_user = user;
m_password = p + 1;
return true;
}
const char *Url::url() const
{
if (!m_url) {
const size_t size = strlen(m_host) + 8;
m_url = new char[size];
snprintf(m_url, size - 1, "%s:%d", m_host, m_port);
}
return m_url;
}
void Url::adjust(int algo)
void Url::adjust(xmrig::Algo algo)
{
if (!isValid()) {
return;
@ -177,91 +154,33 @@ void Url::adjust(int algo)
m_algo = algo;
if (strstr(m_host, ".nicehash.com")) {
if (strstr(m_host.data(), ".nicehash.com")) {
m_keepAlive = false;
m_nicehash = true;
}
if (strstr(m_host, ".minergate.com")) {
if (strstr(m_host.data(), ".minergate.com")) {
m_keepAlive = false;
}
}
void Url::setPassword(const char *password)
{
if (!password) {
return;
}
free(m_password);
m_password = strdup(password);
}
void Url::setUser(const char *user)
{
if (!user) {
return;
}
free(m_user);
m_user = strdup(user);
}
void Url::setVariant(int variant)
{
switch (variant) {
case xmrig::VARIANT_AUTO:
case xmrig::VARIANT_NONE:
case xmrig::VARIANT_V1:
m_variant = variant;
m_variant = static_cast<xmrig::Variant>(variant);
break;
default:
assert(false);
break;
}
}
bool Url::operator==(const Url &other) const
{
if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) {
return false;
}
if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) {
return false;
}
return true;
}
Url &Url::operator=(const Url *other)
{
m_keepAlive = other->m_keepAlive;
m_algo = other->m_algo;
m_variant = other->m_variant;
m_nicehash = other->m_nicehash;
m_port = other->m_port;
free(m_host);
m_host = strdup(other->m_host);
setPassword(other->m_password);
setUser(other->m_user);
if (m_url) {
delete [] m_url;
m_url = nullptr;
}
return *this;
}
bool Url::parseIPv6(const char *addr)
{
const char *end = strchr(addr, ']');
@ -275,10 +194,11 @@ bool Url::parseIPv6(const char *addr)
}
const size_t size = end - addr;
m_host = new char[size]();
memcpy(m_host, addr + 1, size - 1);
char *host = new char[size]();
memcpy(host, addr + 1, size - 1);
m_port = (uint16_t) strtol(port + 1, nullptr, 10);
m_host = host;
m_port = static_cast<uint16_t>(strtol(port + 1, nullptr, 10));
return true;
}

View file

@ -28,6 +28,10 @@
#include <stdint.h>
#include "core/utils/c_str.h"
#include "xmrig.h"
class Url
{
public:
@ -38,45 +42,47 @@ public:
Url();
Url(const char *url);
Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, int keepAlive = 0, bool nicehash = false, int variant = -1);
~Url();
Url(const char *host,
uint16_t port,
const char *user = nullptr,
const char *password = nullptr,
int keepAlive = 0,
bool nicehash = false,
xmrig::Variant variant = xmrig::VARIANT_AUTO
);
inline bool isNicehash() const { return m_nicehash; }
inline bool isValid() const { return m_host && m_port > 0; }
inline const char *host() const { return m_host; }
inline const char *password() const { return m_password ? m_password : kDefaultPassword; }
inline const char *user() const { return m_user ? m_user : kDefaultUser; }
inline int algo() const { return m_algo; }
inline int keepAlive() const { return m_keepAlive; }
inline int variant() const { return m_variant; }
inline uint16_t port() const { return m_port; }
inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setVariant(bool monero) { m_variant = monero; }
inline bool isNicehash() const { return m_nicehash; }
inline bool isValid() const { return !m_host.isNull() && m_port > 0; }
inline const char *host() const { return m_host.data(); }
inline const char *password() const { return !m_password.isNull() ? m_password.data() : kDefaultPassword; }
inline const char *url() const { return m_url.data(); }
inline const char *user() const { return !m_user.isNull() ? m_user.data() : kDefaultUser; }
inline int keepAlive() const { return m_keepAlive; }
inline uint16_t port() const { return m_port; }
inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setPassword(const char *password) { m_password = password; }
inline void setUser(const char *user) { m_user = user; }
inline xmrig::Algo algo() const { return m_algo; }
inline xmrig::Variant variant() const { return m_variant; }
bool parse(const char *url);
bool setUserpass(const char *userpass);
const char *url() const;
void adjust(int algo);
void setPassword(const char *password);
void setUser(const char *user);
void adjust(xmrig::Algo algo);
void setVariant(int variant);
bool operator==(const Url &other) const;
Url &operator=(const Url *other);
private:
bool parseIPv6(const char *addr);
bool m_nicehash;
char *m_host;
char *m_password;
char *m_user;
int m_algo;
int m_keepAlive;
int m_variant;
mutable char *m_url;
uint16_t m_port;
xmrig::Algo m_algo;
xmrig::c_str m_host;
xmrig::c_str m_password;
xmrig::c_str m_url;
xmrig::c_str m_user;
xmrig::Variant m_variant;
};
#endif /* __URL_H__ */