#1421 Use dynamic size send buffer.

This commit is contained in:
XMRig 2019-12-16 14:09:03 +07:00
parent 1d4c8dda96
commit 33e7a54c29
No known key found for this signature in database
GPG key ID: 446A53638BE94409
2 changed files with 12 additions and 13 deletions

View file

@ -79,7 +79,8 @@ static const char *states[] = {
xmrig::Client::Client(int id, const char *agent, IClientListener *listener) : xmrig::Client::Client(int id, const char *agent, IClientListener *listener) :
BaseClient(id, listener), BaseClient(id, listener),
m_agent(agent) m_agent(agent),
m_sendBuf(1024)
{ {
m_key = m_storage.add(this); m_key = m_storage.add(this);
m_dns = new Dns(this); m_dns = new Dns(this);
@ -158,13 +159,11 @@ int64_t xmrig::Client::send(const rapidjson::Value &obj)
obj.Accept(writer); obj.Accept(writer);
const size_t size = buffer.GetSize(); const size_t size = buffer.GetSize();
if (size > (sizeof(m_sendBuf) - 2)) { if (size > (m_sendBuf.size() - 2)) {
LOG_ERR("[%s] send failed: \"send buffer overflow: %zu > %zu\"", url(), size, (sizeof(m_sendBuf) - 2)); m_sendBuf.resize(((size + 1) / 1024 + 1) * 1024);
close();
return -1;
} }
memcpy(m_sendBuf, buffer.GetString(), size); memcpy(m_sendBuf.data(), buffer.GetString(), size);
m_sendBuf[size] = '\n'; m_sendBuf[size] = '\n';
m_sendBuf[size + 1] = '\0'; m_sendBuf[size + 1] = '\0';
@ -186,8 +185,8 @@ int64_t xmrig::Client::submit(const JobResult &result)
const char *nonce = result.nonce; const char *nonce = result.nonce;
const char *data = result.result; const char *data = result.result;
# else # else
char *nonce = m_sendBuf; char *nonce = m_sendBuf.data();
char *data = m_sendBuf + 16; char *data = m_sendBuf.data() + 16;
Buffer::toHex(reinterpret_cast<const char*>(&result.nonce), 4, nonce); Buffer::toHex(reinterpret_cast<const char*>(&result.nonce), 4, nonce);
nonce[8] = '\0'; nonce[8] = '\0';
@ -529,11 +528,11 @@ int xmrig::Client::resolve(const String &host)
int64_t xmrig::Client::send(size_t size) 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 # ifdef XMRIG_FEATURE_TLS
if (isTLS()) { if (isTLS()) {
if (!m_tls->send(m_sendBuf, size)) { if (!m_tls->send(m_sendBuf.data(), size)) {
return -1; return -1;
} }
} }
@ -545,7 +544,7 @@ int64_t xmrig::Client::send(size_t size)
return -1; 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) { if (uv_try_write(m_stream, &buf, 1) < 0) {
close(); close();
@ -795,7 +794,7 @@ void xmrig::Client::parseResponse(int64_t id, const rapidjson::Value &result, co
void xmrig::Client::ping() 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; m_keepAlive = 0;
} }

View file

@ -128,11 +128,11 @@ private:
static inline Client *getClient(void *data) { return m_storage.get(data); } static inline Client *getClient(void *data) { return m_storage.get(data); }
char m_sendBuf[4096] = { 0 };
const char *m_agent; const char *m_agent;
Dns *m_dns; Dns *m_dns;
RecvBuf<kInputBufferSize> m_recvBuf; RecvBuf<kInputBufferSize> m_recvBuf;
std::bitset<EXT_MAX> m_extensions; std::bitset<EXT_MAX> m_extensions;
std::vector<char> m_sendBuf;
String m_rpcId; String m_rpcId;
Tls *m_tls = nullptr; Tls *m_tls = nullptr;
uint64_t m_expire = 0; uint64_t m_expire = 0;