Code cleanup.

This commit is contained in:
XMRig 2020-04-02 21:19:39 +07:00
parent 4dddd3a44f
commit 914b7023a2
No known key found for this signature in database
GPG key ID: 446A53638BE94409
3 changed files with 12 additions and 18 deletions

View file

@ -52,7 +52,7 @@ public:
m_ctx(ctx), m_ctx(ctx),
m_body(std::move(body)) m_body(std::move(body))
{ {
m_buf = uv_buf_init(const_cast<char *>(m_body.c_str()), m_body.size()); m_buf = uv_buf_init(&m_body.front(), m_body.size());
} }
inline ~HttpWriteBaton() inline ~HttpWriteBaton()

View file

@ -155,8 +155,6 @@ int64_t xmrig::Client::send(const rapidjson::Value &obj)
{ {
using namespace rapidjson; using namespace rapidjson;
Value value;
StringBuffer buffer(nullptr, 512); StringBuffer buffer(nullptr, 512);
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
obj.Accept(writer); obj.Accept(writer);
@ -481,7 +479,7 @@ bool xmrig::Client::send(BIO *bio)
LOG_DEBUG("[%s] TLS send (%d bytes)", url(), static_cast<int>(buf.len)); LOG_DEBUG("[%s] TLS send (%d bytes)", url(), static_cast<int>(buf.len));
bool result = false; bool result = false;
if (state() == ConnectedState && uv_is_writable(m_stream)) { if (state() == ConnectedState && uv_is_writable(stream())) {
result = write(buf); result = write(buf);
} }
else { else {
@ -525,7 +523,7 @@ bool xmrig::Client::verifyAlgorithm(const Algorithm &algorithm, const char *algo
bool xmrig::Client::write(const uv_buf_t &buf) 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<size_t>(rc) == buf.len) { if (static_cast<size_t>(rc) == buf.len) {
return true; return true;
} }
@ -575,7 +573,7 @@ int64_t xmrig::Client::send(size_t size)
else else
# endif # 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); LOG_DEBUG_ERR("[%s] send failed, invalid state: %d", url(), m_state);
return -1; return -1;
} }
@ -664,7 +662,6 @@ void xmrig::Client::onClose()
{ {
delete m_socket; delete m_socket;
m_stream = nullptr;
m_socket = nullptr; m_socket = nullptr;
setState(UnconnectedState); setState(UnconnectedState);
@ -966,8 +963,9 @@ void xmrig::Client::onClose(uv_handle_t *handle)
void xmrig::Client::onConnect(uv_connect_t *req, int status) void xmrig::Client::onConnect(uv_connect_t *req, int status)
{ {
auto client = getClient(req->data); auto client = getClient(req->data);
delete req;
if (!client) { if (!client) {
delete req;
return; return;
} }
@ -988,7 +986,6 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status)
return; return;
} }
delete req;
client->close(); client->close();
return; return;
} }
@ -999,12 +996,9 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status)
return; return;
} }
client->m_stream = static_cast<uv_stream_t*>(req->handle);
client->m_stream->data = req->data;
client->setState(ConnectedState); client->setState(ConnectedState);
uv_read_start(client->m_stream, NetBuffer::onAlloc, onRead); uv_read_start(client->stream(), NetBuffer::onAlloc, onRead);
delete req;
client->handshake(); client->handshake();
} }

View file

@ -112,10 +112,11 @@ private:
void setState(SocketState state); void setState(SocketState state);
void startTimeout(); void startTimeout();
inline const char *url() const { return m_pool.url(); } inline const char *url() const { return m_pool.url(); }
inline SocketState state() const { return m_state; } inline SocketState state() const { return m_state; }
inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); } inline uv_stream_t *stream() const { return reinterpret_cast<uv_stream_t *>(m_socket); }
template<Extension ext> inline bool has() const noexcept { return m_extensions.test(ext); } inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); }
template<Extension ext> inline bool has() const noexcept { return m_extensions.test(ext); }
static void onClose(uv_handle_t *handle); static void onClose(uv_handle_t *handle);
static void onConnect(uv_connect_t *req, int status); static void onConnect(uv_connect_t *req, int status);
@ -135,7 +136,6 @@ private:
uint64_t m_jobs = 0; uint64_t m_jobs = 0;
uint64_t m_keepAlive = 0; uint64_t m_keepAlive = 0;
uintptr_t m_key = 0; uintptr_t m_key = 0;
uv_stream_t *m_stream = nullptr;
uv_tcp_t *m_socket = nullptr; uv_tcp_t *m_socket = nullptr;
static Storage<Client> m_storage; static Storage<Client> m_storage;