This commit is contained in:
MoneroOcean 2019-12-21 18:18:26 -08:00
commit eb58aea9c7
48 changed files with 970 additions and 454 deletions

View file

@ -79,7 +79,8 @@ static const char *states[] = {
xmrig::Client::Client(int id, const char *agent, IClientListener *listener) :
BaseClient(id, listener),
m_agent(agent)
m_agent(agent),
m_sendBuf(1024)
{
m_key = m_storage.add(this);
m_dns = new Dns(this);
@ -158,13 +159,18 @@ int64_t xmrig::Client::send(const rapidjson::Value &obj)
obj.Accept(writer);
const size_t size = buffer.GetSize();
if (size > (sizeof(m_sendBuf) - 2)) {
LOG_ERR("[%s] send failed: \"send buffer overflow: %zu > %zu\"", url(), size, (sizeof(m_sendBuf) - 2));
if (size > kMaxSendBufferSize) {
LOG_ERR("[%s] send failed: \"max send buffer size exceeded: %zu\"", url(), size);
close();
return -1;
}
memcpy(m_sendBuf, buffer.GetString(), size);
if (size > (m_sendBuf.size() - 2)) {
m_sendBuf.resize(((size + 1) / 1024 + 1) * 1024);
}
memcpy(m_sendBuf.data(), buffer.GetString(), size);
m_sendBuf[size] = '\n';
m_sendBuf[size + 1] = '\0';
@ -186,8 +192,8 @@ int64_t xmrig::Client::submit(const JobResult &result)
const char *nonce = result.nonce;
const char *data = result.result;
# else
char *nonce = m_sendBuf;
char *data = m_sendBuf + 16;
char *nonce = m_sendBuf.data();
char *data = m_sendBuf.data() + 16;
Buffer::toHex(reinterpret_cast<const char*>(&result.nonce), 4, nonce);
nonce[8] = '\0';
@ -460,11 +466,7 @@ bool xmrig::Client::send(BIO *bio)
bool result = false;
if (state() == ConnectedState && uv_is_writable(m_stream)) {
result = uv_try_write(m_stream, &buf, 1) > 0;
if (!result) {
close();
}
result = write(buf);
}
else {
LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", url(), m_state);
@ -505,6 +507,23 @@ bool xmrig::Client::verifyAlgorithm(const Algorithm &algorithm, const char *algo
}
bool xmrig::Client::write(const uv_buf_t &buf)
{
const int rc = uv_try_write(m_stream, &buf, 1);
if (static_cast<size_t>(rc) == buf.len) {
return true;
}
if (!isQuiet()) {
LOG_ERR("[%s] write error: \"%s\"", url(), uv_strerror(rc));
}
close();
return false;
}
int xmrig::Client::resolve(const String &host)
{
setState(HostLookupState);
@ -529,11 +548,11 @@ int xmrig::Client::resolve(const String &host)
int64_t xmrig::Client::send(size_t size)
{
LOG_DEBUG("[%s] send (%d bytes): \"%.*s\"", url(), size, static_cast<int>(size) - 1, m_sendBuf);
LOG_DEBUG("[%s] send (%d bytes): \"%.*s\"", url(), size, static_cast<int>(size) - 1, m_sendBuf.data());
# ifdef XMRIG_FEATURE_TLS
if (isTLS()) {
if (!m_tls->send(m_sendBuf, size)) {
if (!m_tls->send(m_sendBuf.data(), size)) {
return -1;
}
}
@ -545,10 +564,9 @@ int64_t xmrig::Client::send(size_t size)
return -1;
}
uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size);
uv_buf_t buf = uv_buf_init(m_sendBuf.data(), (unsigned int) size);
if (uv_try_write(m_stream, &buf, 1) < 0) {
close();
if (!write(buf)) {
return -1;
}
}
@ -795,7 +813,7 @@ void xmrig::Client::parseResponse(int64_t id, const rapidjson::Value &result, co
void xmrig::Client::ping()
{
send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId.data()));
send(snprintf(m_sendBuf.data(), m_sendBuf.size(), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId.data()));
m_keepAlive = 0;
}

View file

@ -60,14 +60,10 @@ class Client : public BaseClient, public IDnsListener, public ILineListener
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Client)
constexpr static uint64_t kConnectTimeout = 20 * 1000;
constexpr static uint64_t kResponseTimeout = 20 * 1000;
# ifdef XMRIG_FEATURE_TLS
constexpr static size_t kInputBufferSize = 1024 * 16;
# else
constexpr static size_t kInputBufferSize = 1024 * 2;
# endif
constexpr static uint64_t kConnectTimeout = 20 * 1000;
constexpr static uint64_t kResponseTimeout = 20 * 1000;
constexpr static size_t kInputBufferSize = 1024 * 16;
constexpr static size_t kMaxSendBufferSize = 1024 * 16;
Client(int id, const char *agent, IClientListener *listener);
~Client() override;
@ -100,6 +96,7 @@ private:
bool parseLogin(const rapidjson::Value &result, int *code);
bool send(BIO *bio);
bool verifyAlgorithm(const Algorithm &algorithm, const char *algo) const;
bool write(const uv_buf_t &buf);
int resolve(const String &host);
int64_t send(size_t size);
void connect(sockaddr *addr);
@ -128,11 +125,11 @@ private:
static inline Client *getClient(void *data) { return m_storage.get(data); }
char m_sendBuf[4096] = { 0 };
const char *m_agent;
Dns *m_dns;
RecvBuf<kInputBufferSize> m_recvBuf;
std::bitset<EXT_MAX> m_extensions;
std::vector<char> m_sendBuf;
String m_rpcId;
Tls *m_tls = nullptr;
uint64_t m_expire = 0;