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

@ -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__ */