mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
stats: use Socks
wrapper, use CService
to generate our sockaddr
This commit is contained in:
parent
2def905044
commit
dbbfc8d766
@ -70,20 +70,25 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_sock = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
SOCKET hSocket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (m_sock == INVALID_SOCKET) {
|
if (hSocket == INVALID_SOCKET) {
|
||||||
LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n",
|
LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n",
|
||||||
NetworkErrorString(WSAGetLastError()));
|
NetworkErrorString(WSAGetLastError()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
m_sock = std::make_unique<Sock>(hSocket);
|
||||||
|
|
||||||
memset(&m_server, 0, sizeof(m_server));
|
CNetAddr netaddr;
|
||||||
m_server.sin_family = AF_INET;
|
if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) {
|
||||||
m_server.sin_port = htons(m_port);
|
LogPrintf("ERROR: Unable to lookup host %s, cannot init StatsdClient\n", m_host);
|
||||||
|
return;
|
||||||
CNetAddr netaddr(m_server.sin_addr);
|
}
|
||||||
if (!LookupHost(m_host, netaddr, true) || !netaddr.GetInAddr(&m_server.sin_addr)) {
|
if (!netaddr.IsIPv4()) {
|
||||||
LogPrintf("ERROR: LookupHost or GetInAddr failed, cannot init StatsdClient\n");
|
LogPrintf("ERROR: Host %s on unsupported network, cannot init StatsdClient\n", m_host);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!CService(netaddr, port).GetSockAddr(reinterpret_cast<struct sockaddr*>(&m_server.first), &m_server.second)) {
|
||||||
|
LogPrintf("ERROR: Cannot get socket address for %s, cannot init StatsdClient\n", m_host);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,11 +97,6 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
|
|||||||
LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port);
|
LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
StatsdClient::~StatsdClient()
|
|
||||||
{
|
|
||||||
CloseSocket(m_sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* will change the original string */
|
/* will change the original string */
|
||||||
void StatsdClient::cleanup(std::string& key)
|
void StatsdClient::cleanup(std::string& key)
|
||||||
{
|
{
|
||||||
@ -183,12 +183,11 @@ int StatsdClient::send(const std::string& message)
|
|||||||
if (!m_init)
|
if (!m_init)
|
||||||
return -3;
|
return -3;
|
||||||
|
|
||||||
int ret = ::sendto(m_sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&m_server),
|
if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0,
|
||||||
sizeof(m_server));
|
reinterpret_cast<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) {
|
||||||
if (ret == -1) {
|
|
||||||
LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n",
|
LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n",
|
||||||
NetworkErrorString(WSAGetLastError()), m_host, m_port);
|
NetworkErrorString(WSAGetLastError()), m_host, m_port);
|
||||||
return ret;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -29,7 +29,6 @@ class StatsdClient {
|
|||||||
public:
|
public:
|
||||||
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
|
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
|
||||||
bool enabled);
|
bool enabled);
|
||||||
~StatsdClient();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int inc(const std::string& key, float sample_rate = 1.f);
|
int inc(const std::string& key, float sample_rate = 1.f);
|
||||||
@ -65,8 +64,8 @@ class StatsdClient {
|
|||||||
mutable FastRandomContext insecure_rand GUARDED_BY(cs);
|
mutable FastRandomContext insecure_rand GUARDED_BY(cs);
|
||||||
|
|
||||||
bool m_init{false};
|
bool m_init{false};
|
||||||
SOCKET m_sock{INVALID_SOCKET};
|
std::unique_ptr<Sock> m_sock{nullptr};
|
||||||
struct sockaddr_in m_server;
|
std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)};
|
||||||
|
|
||||||
const uint16_t m_port;
|
const uint16_t m_port;
|
||||||
const std::string m_host;
|
const std::string m_host;
|
||||||
|
Loading…
Reference in New Issue
Block a user