Code cleanup.

This commit is contained in:
XMRig 2020-03-04 21:00:49 +07:00
parent b0dda2b5b3
commit 5486300db7
No known key found for this signature in database
GPG key ID: 446A53638BE94409
5 changed files with 39 additions and 52 deletions

View file

@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -50,13 +50,14 @@ xmrig::DnsRecord::DnsRecord(const addrinfo *addr) :
sockaddr *xmrig::DnsRecord::addr(uint16_t port) const
{
if (m_type == A) {
sockaddr_in *addr = new sockaddr_in();
auto addr = new sockaddr_in();
uv_ip4_addr(m_ip.data(), port, addr);
return reinterpret_cast<sockaddr *>(addr);
}
else if (m_type == AAAA) {
sockaddr_in6 *addr = new sockaddr_in6();
if (m_type == AAAA) {
auto addr = new sockaddr_in6();
uv_ip6_addr(m_ip.data(), port, addr);
return reinterpret_cast<sockaddr *>(addr);

View file

@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -45,7 +45,7 @@ public:
AAAA
};
inline DnsRecord() : m_type(Unknown) {}
DnsRecord() = default;
DnsRecord(const addrinfo *addr);
sockaddr *addr(uint16_t port = 0) const;
@ -55,7 +55,7 @@ public:
inline Type type() const { return m_type; }
private:
Type m_type;
Type m_type = Unknown;
String m_ip;
};

View file

@ -24,53 +24,45 @@
*/
#include <sstream>
#include "base/net/http/HttpClient.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/io/log/Log.h"
#include "base/kernel/Platform.h"
#include "base/net/dns/Dns.h"
#include "base/net/http/HttpClient.h"
#include "base/tools/Baton.h"
#include <sstream>
namespace xmrig {
static const char *kCRLF = "\r\n";
class ClientWriteBaton : public Baton<uv_write_t>
class HttpClientWriteBaton : public Baton<uv_write_t>
{
public:
inline ClientWriteBaton(const std::string &header, std::string &&body) :
inline HttpClientWriteBaton(const std::string &header, std::string &&body) :
m_body(std::move(body)),
m_header(header)
{
bufs[0].len = m_header.size();
bufs[0].base = const_cast<char *>(m_header.c_str());
if (!m_body.empty()) {
bufs[1].len = m_body.size();
bufs[1].base = const_cast<char *>(m_body.c_str());
}
else {
bufs[1].base = nullptr;
bufs[1].len = 0;
}
m_bufs[0] = uv_buf_init(const_cast<char *>(m_header.c_str()), m_header.size());
m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast<char *>(m_body.c_str()), m_body.size());
}
inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; }
inline size_t size() const { return bufs[0].len + bufs[1].len; }
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<ClientWriteBaton *>(req->data); }
uv_buf_t bufs[2]{};
void write(uv_stream_t *stream)
{
uv_write(&req, stream, m_bufs, nbufs(), [](uv_write_t *req, int) { delete reinterpret_cast<HttpClientWriteBaton *>(req->data); });
}
private:
inline size_t nbufs() const { return m_bufs[1].len > 0 ? 2 : 1; }
std::string m_body;
std::string m_header;
uv_buf_t m_bufs[2]{};
};
@ -129,6 +121,8 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
req->data = this;
uv_tcp_connect(req, m_tcp, addr, onConnect);
delete addr;
}
@ -167,8 +161,8 @@ void xmrig::HttpClient::read(const char *data, size_t size)
void xmrig::HttpClient::write(const std::string &header)
{
auto baton = new ClientWriteBaton(header, std::move(body));
uv_write(&baton->req, stream(), baton->bufs, baton->count(), ClientWriteBaton::onWrite);
auto baton = new HttpClientWriteBaton(header, std::move(body));
baton->write(stream());
}
@ -194,12 +188,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
{
buf->base = new char[suggested_size];
# ifdef _WIN32
buf->len = static_cast<unsigned int>(suggested_size);
# else
buf->len = suggested_size;
# endif
buf->len = suggested_size;
},
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
{

View file

@ -29,8 +29,8 @@
#include <uv.h>
#include "base/io/log/Log.h"
#include "base/net/http/HttpsClient.h"
#include "base/io/log/Log.h"
#include "base/tools/Buffer.h"
@ -41,9 +41,6 @@
xmrig::HttpsClient::HttpsClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size, const String &fingerprint) :
HttpClient(method, url, listener, data, size),
m_ready(false),
m_buf(),
m_ssl(nullptr),
m_fp(fingerprint)
{
m_ctx = SSL_CTX_new(SSLv23_method());

View file

@ -62,13 +62,13 @@ private:
bool verifyFingerprint(X509 *cert);
void flush();
BIO *m_readBio;
BIO *m_writeBio;
bool m_ready;
char m_buf[1024 * 2];
char m_fingerprint[32 * 2 + 8];
SSL *m_ssl;
SSL_CTX *m_ctx;
BIO *m_readBio = nullptr;
BIO *m_writeBio = nullptr;
bool m_ready = false;
char m_buf[1024 * 2]{};
char m_fingerprint[32 * 2 + 8]{};
SSL *m_ssl = nullptr;
SSL_CTX *m_ctx = nullptr;
String m_fp;
};