From 914b7023a2fb9851d52f586d97df11535000da41 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 2 Apr 2020 21:19:39 +0700 Subject: [PATCH] Code cleanup. --- src/base/net/http/HttpContext.cpp | 2 +- src/base/net/stratum/Client.cpp | 18 ++++++------------ src/base/net/stratum/Client.h | 10 +++++----- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index 441f3b825..37d8ed426 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -52,7 +52,7 @@ public: m_ctx(ctx), m_body(std::move(body)) { - m_buf = uv_buf_init(const_cast(m_body.c_str()), m_body.size()); + m_buf = uv_buf_init(&m_body.front(), m_body.size()); } inline ~HttpWriteBaton() diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index 16a5f79c9..5e442bd6e 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -155,8 +155,6 @@ int64_t xmrig::Client::send(const rapidjson::Value &obj) { using namespace rapidjson; - Value value; - StringBuffer buffer(nullptr, 512); Writer writer(buffer); obj.Accept(writer); @@ -481,7 +479,7 @@ bool xmrig::Client::send(BIO *bio) LOG_DEBUG("[%s] TLS send (%d bytes)", url(), static_cast(buf.len)); bool result = false; - if (state() == ConnectedState && uv_is_writable(m_stream)) { + if (state() == ConnectedState && uv_is_writable(stream())) { result = write(buf); } else { @@ -525,7 +523,7 @@ 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); + const int rc = uv_try_write(stream(), &buf, 1); if (static_cast(rc) == buf.len) { return true; } @@ -575,7 +573,7 @@ int64_t xmrig::Client::send(size_t size) else # endif { - if (state() != ConnectedState || !uv_is_writable(m_stream)) { + if (state() != ConnectedState || !uv_is_writable(stream())) { LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", url(), m_state); return -1; } @@ -664,7 +662,6 @@ void xmrig::Client::onClose() { delete m_socket; - m_stream = nullptr; m_socket = nullptr; setState(UnconnectedState); @@ -966,8 +963,9 @@ void xmrig::Client::onClose(uv_handle_t *handle) void xmrig::Client::onConnect(uv_connect_t *req, int status) { auto client = getClient(req->data); + delete req; + if (!client) { - delete req; return; } @@ -988,7 +986,6 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status) return; } - delete req; client->close(); return; } @@ -999,12 +996,9 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status) return; } - client->m_stream = static_cast(req->handle); - client->m_stream->data = req->data; client->setState(ConnectedState); - uv_read_start(client->m_stream, NetBuffer::onAlloc, onRead); - delete req; + uv_read_start(client->stream(), NetBuffer::onAlloc, onRead); client->handshake(); } diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index d6ebf396d..dfee56d27 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -112,10 +112,11 @@ private: void setState(SocketState state); void startTimeout(); - inline const char *url() const { return m_pool.url(); } - inline SocketState state() const { return m_state; } - inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); } - template inline bool has() const noexcept { return m_extensions.test(ext); } + inline const char *url() const { return m_pool.url(); } + inline SocketState state() const { return m_state; } + inline uv_stream_t *stream() const { return reinterpret_cast(m_socket); } + inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); } + template inline bool has() const noexcept { return m_extensions.test(ext); } static void onClose(uv_handle_t *handle); static void onConnect(uv_connect_t *req, int status); @@ -135,7 +136,6 @@ private: uint64_t m_jobs = 0; uint64_t m_keepAlive = 0; uintptr_t m_key = 0; - uv_stream_t *m_stream = nullptr; uv_tcp_t *m_socket = nullptr; static Storage m_storage;