mirror of
https://github.com/xmrig/xmrig.git
synced 2025-01-21 18:24:40 +00:00
Update Console class.
This commit is contained in:
parent
45084cb718
commit
75283445fd
4 changed files with 151 additions and 44 deletions
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -14,16 +14,98 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Additional permission under GNU GPL version 3 section 7
|
||||||
|
*
|
||||||
|
* If you modify this Program, or any covered work, by linking or combining
|
||||||
|
* it with OpenSSL (or a modified version of that library), containing parts
|
||||||
|
* covered by the terms of OpenSSL License and SSLeay License, the licensors
|
||||||
|
* of this Program grant you additional permission to convey the resulting work.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "base/io/Console.h"
|
#include "base/io/Console.h"
|
||||||
|
#include "base/io/log/Log.h"
|
||||||
|
#include "base/io/Signals.h"
|
||||||
#include "base/kernel/interfaces/IConsoleListener.h"
|
#include "base/kernel/interfaces/IConsoleListener.h"
|
||||||
|
#include "base/kernel/private/Title.h"
|
||||||
|
#include "base/kernel/Process.h"
|
||||||
|
#include "base/tools/Cvt.h"
|
||||||
#include "base/tools/Handle.h"
|
#include "base/tools/Handle.h"
|
||||||
|
|
||||||
|
|
||||||
xmrig::Console::Console(IConsoleListener *listener)
|
#ifdef XMRIG_FEATURE_EVENTS
|
||||||
: m_listener(listener)
|
# include "base/kernel/Events.h"
|
||||||
|
# include "base/kernel/events/ConsoleEvent.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
|
class Console::Private : public IConsoleListener
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
XMRIG_DISABLE_COPY_MOVE(Private)
|
||||||
|
|
||||||
|
Private();
|
||||||
|
~Private() override;
|
||||||
|
|
||||||
|
# ifdef XMRIG_OS_WIN
|
||||||
|
std::wstring title;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
IConsoleListener *listener = nullptr;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onConsoleCommand(char command) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static bool isSupported();
|
||||||
|
|
||||||
|
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
|
||||||
|
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
|
||||||
|
|
||||||
|
char m_buf[1] = { 0 };
|
||||||
|
uv_tty_t *m_tty = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
|
xmrig::Console::Console() :
|
||||||
|
d(std::make_shared<Private>())
|
||||||
|
{
|
||||||
|
d->listener = d.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
xmrig::Console::Console(IConsoleListener *listener) :
|
||||||
|
d(std::make_shared<Private>())
|
||||||
|
{
|
||||||
|
d->listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef XMRIG_OS_WIN
|
||||||
|
void xmrig::Console::setTitle(const Title &title) const
|
||||||
|
{
|
||||||
|
SetConsoleTitleW(title.isEnabled() ? Cvt::toUtf16(title.value()).c_str() : d->title.c_str());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
xmrig::Console::Private::Private()
|
||||||
|
{
|
||||||
|
# ifdef XMRIG_OS_WIN
|
||||||
|
{
|
||||||
|
constexpr size_t kMaxTitleLength = 8192;
|
||||||
|
|
||||||
|
wchar_t title_w[kMaxTitleLength]{};
|
||||||
|
title = { title_w, GetConsoleTitleW(title_w, kMaxTitleLength) };
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
if (!isSupported()) {
|
if (!isSupported()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -37,11 +119,11 @@ xmrig::Console::Console(IConsoleListener *listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_tty_set_mode(m_tty, UV_TTY_MODE_RAW);
|
uv_tty_set_mode(m_tty, UV_TTY_MODE_RAW);
|
||||||
uv_read_start(reinterpret_cast<uv_stream_t*>(m_tty), Console::onAllocBuffer, Console::onRead);
|
uv_read_start(reinterpret_cast<uv_stream_t*>(m_tty), onAllocBuffer, onRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
xmrig::Console::~Console()
|
xmrig::Console::Private::~Private()
|
||||||
{
|
{
|
||||||
uv_tty_reset_mode();
|
uv_tty_reset_mode();
|
||||||
|
|
||||||
|
@ -49,28 +131,45 @@ xmrig::Console::~Console()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool xmrig::Console::isSupported()
|
void xmrig::Console::Private::onConsoleCommand(char command)
|
||||||
|
{
|
||||||
|
# ifdef XMRIG_FEATURE_EVENTS
|
||||||
|
if (command == 3) {
|
||||||
|
LOG_WARN("%s " YELLOW_BOLD("Ctrl+C ") YELLOW("received, exiting"), Signals::tag());
|
||||||
|
|
||||||
|
Process::exit(0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Process::events().send<ConsoleEvent>(command);
|
||||||
|
}
|
||||||
|
# else
|
||||||
|
assert(!command);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool xmrig::Console::Private::isSupported()
|
||||||
{
|
{
|
||||||
const uv_handle_type type = uv_guess_handle(0);
|
const uv_handle_type type = uv_guess_handle(0);
|
||||||
return type == UV_TTY || type == UV_NAMED_PIPE;
|
return type == UV_TTY || type == UV_NAMED_PIPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::Console::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
|
void xmrig::Console::Private::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
|
||||||
{
|
{
|
||||||
auto console = static_cast<Console*>(handle->data);
|
auto console = static_cast<Private *>(handle->data);
|
||||||
buf->len = 1;
|
buf->len = 1;
|
||||||
buf->base = console->m_buf;
|
buf->base = console->m_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
|
void xmrig::Console::Private::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
|
||||||
{
|
{
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
return uv_close(reinterpret_cast<uv_handle_t*>(stream), nullptr);
|
return uv_close(reinterpret_cast<uv_handle_t*>(stream), nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nread == 1) {
|
if (nread == 1) {
|
||||||
static_cast<Console*>(stream->data)->m_listener->onConsoleCommand(buf->base[0]);
|
static_cast<Private *>(stream->data)->listener->onConsoleCommand(*buf->base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -14,6 +14,13 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Additional permission under GNU GPL version 3 section 7
|
||||||
|
*
|
||||||
|
* If you modify this Program, or any covered work, by linking or combining
|
||||||
|
* it with OpenSSL (or a modified version of that library), containing parts
|
||||||
|
* covered by the terms of OpenSSL License and SSLeay License, the licensors
|
||||||
|
* of this Program grant you additional permission to convey the resulting work.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef XMRIG_CONSOLE_H
|
#ifndef XMRIG_CONSOLE_H
|
||||||
|
@ -23,45 +30,32 @@
|
||||||
#include "base/tools/Object.h"
|
#include "base/tools/Object.h"
|
||||||
|
|
||||||
|
|
||||||
using uv_buf_t = struct uv_buf_t;
|
|
||||||
using uv_handle_t = struct uv_handle_s;
|
|
||||||
using uv_stream_t = struct uv_stream_s;
|
|
||||||
using uv_tty_t = struct uv_tty_s;
|
|
||||||
|
|
||||||
#ifdef XMRIG_OS_WIN
|
|
||||||
using ssize_t = intptr_t;
|
|
||||||
#else
|
|
||||||
# include <sys/types.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
|
class Title;
|
||||||
class IConsoleListener;
|
class IConsoleListener;
|
||||||
|
|
||||||
|
|
||||||
class Console
|
class Console
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Console)
|
XMRIG_DISABLE_COPY_MOVE(Console)
|
||||||
|
|
||||||
Console(IConsoleListener *listener);
|
Console();
|
||||||
~Console();
|
explicit Console(IConsoleListener *listener);
|
||||||
|
~Console() = default;
|
||||||
|
|
||||||
|
# ifdef XMRIG_OS_WIN
|
||||||
|
void setTitle(const Title &title) const;
|
||||||
|
# endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool isSupported();
|
XMRIG_DECL_PRIVATE()
|
||||||
|
|
||||||
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
|
|
||||||
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
|
|
||||||
|
|
||||||
char m_buf[1] = { 0 };
|
|
||||||
IConsoleListener *m_listener;
|
|
||||||
uv_tty_t *m_tty = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} /* namespace xmrig */
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
#endif /* XMRIG_CONSOLE_H */
|
#endif // XMRIG_CONSOLE_H
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -14,6 +14,13 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Additional permission under GNU GPL version 3 section 7
|
||||||
|
*
|
||||||
|
* If you modify this Program, or any covered work, by linking or combining
|
||||||
|
* it with OpenSSL (or a modified version of that library), containing parts
|
||||||
|
* covered by the terms of OpenSSL License and SSLeay License, the licensors
|
||||||
|
* of this Program grant you additional permission to convey the resulting work.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef XMRIG_ICONSOLELISTENER_H
|
#ifndef XMRIG_ICONSOLELISTENER_H
|
||||||
|
@ -38,7 +45,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} /* namespace xmrig */
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
#endif // XMRIG_ICONSOLELISTENER_H
|
#endif // XMRIG_ICONSOLELISTENER_H
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* XMRig
|
/* XMRig
|
||||||
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
|
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -14,6 +14,13 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Additional permission under GNU GPL version 3 section 7
|
||||||
|
*
|
||||||
|
* If you modify this Program, or any covered work, by linking or combining
|
||||||
|
* it with OpenSSL (or a modified version of that library), containing parts
|
||||||
|
* covered by the terms of OpenSSL License and SSLeay License, the licensors
|
||||||
|
* of this Program grant you additional permission to convey the resulting work.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef XMRIG_ISIGNALLISTENER_H
|
#ifndef XMRIG_ISIGNALLISTENER_H
|
||||||
|
@ -41,7 +48,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} /* namespace xmrig */
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
#endif // XMRIG_ISIGNALLISTENER_H
|
#endif // XMRIG_ISIGNALLISTENER_H
|
||||||
|
|
Loading…
Reference in a new issue