Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1d2aea

Browse files
authored
Merge pull request #2 from basiliscos/ivan/cmake
Make it win32 and cmake friendly
2 parents efad48e + be07266 commit f1d2aea

File tree

6 files changed

+85
-63
lines changed

6 files changed

+85
-63
lines changed

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required (VERSION 3.2)
2+
project (StatsdClient)
3+
4+
set (StatsdClient_VERSION_MAJOR 1)
5+
set (StatsdClient_VERSION_MINOR 0)
6+
7+
include_directories ("${PROJECT_SOURCE_DIR}/src")
8+
add_subdirectory (src)
9+
10+
configure_file (
11+
"${PROJECT_SOURCE_DIR}/src/StatsdClientConfig.h.in"
12+
"${PROJECT_BINARY_DIR}/StatsdClientConfig.h"
13+
)
14+
15+
target_compile_features(StatsdClient PRIVATE cxx_nullptr)
16+
17+
include_directories("${PROJECT_BINARY_DIR}")
18+
19+
install (FILES "${PROJECT_BINARY_DIR}/StatsdClientConfig.h"
20+
DESTINATION include)
21+

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_library(StatsdClient statsd_client.cpp)
2+
3+
install (TARGETS StatsdClient DESTINATION lib)
4+
install (FILES statsd_client.h DESTINATION include)

src/Makefile

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/StatsdClientConfig.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define StatsdClient_VERSION_MAJOR @StatsdClient_VERSION_MAJOR@
2+
#define StatsdClient_VERSION_MINOR @StatsdClient_VERSION_MINOR@

src/statsd_client.cpp

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
21
#include <math.h>
3-
#include <netdb.h>
42
#include <time.h>
5-
#include <unistd.h>
63
#include <stdlib.h>
74
#include <string.h>
85
#include <stdio.h>
96
#include <sys/types.h>
10-
#include <netinet/in.h>
7+
#include <random>
118
#include "statsd_client.h"
12-
#include <fcntl.h>
9+
10+
11+
/* platform-specific headers */
12+
#ifdef _WIN32
13+
#include <winsock2.h>
14+
#include <ws2tcpip.h>
15+
#define CLOSE_SOCKET(s) closesocket(s)
16+
#else
17+
#include <sys/socket.h>
18+
#include <arpa/inet.h>
19+
#include <netdb.h> /* Needed for getaddrinfo() and freeaddrinfo() */
20+
#include <unistd.h> /* Needed for close() */
21+
22+
#define CLOSE_SOCKET(s) close(s)
23+
#endif
1324

1425
using namespace std;
1526
namespace statsd {
@@ -20,17 +31,6 @@ inline bool fequal(float a, float b)
2031
return ( fabs(a - b) < epsilon );
2132
}
2233

23-
inline bool should_send(float sample_rate)
24-
{
25-
if ( fequal(sample_rate, 1.0) )
26-
{
27-
return true;
28-
}
29-
30-
float p = ((float)random() / RAND_MAX);
31-
return sample_rate > p;
32-
}
33-
3434
struct _StatsdClientData {
3535
int sock;
3636
struct sockaddr_in server;
@@ -40,25 +40,44 @@ struct _StatsdClientData {
4040
short port;
4141
bool init;
4242

43+
std::default_random_engine rng_engine;
44+
std::uniform_real_distribution<> rng_dist;
45+
46+
4347
char errmsg[1024];
4448
};
4549

50+
inline bool should_send(_StatsdClientData* d, float sample_rate)
51+
{
52+
if ( fequal(sample_rate, 1.0) )
53+
{
54+
return true;
55+
}
56+
57+
float p = d->rng_dist(d->rng_engine);
58+
return sample_rate > p;
59+
}
60+
4661
StatsdClient::StatsdClient(const string& host, int port, const string& ns)
4762
{
4863
d = new _StatsdClientData;
64+
4965
d->sock = -1;
66+
std::random_device rd;
67+
d->rng_engine = std::default_random_engine(rd () );
68+
d->rng_dist = std::uniform_real_distribution<>(0.0f, 1.0f);
69+
5070
config(host, port, ns);
51-
srandom(time(NULL));
5271
}
5372

5473
StatsdClient::~StatsdClient()
5574
{
5675
// close socket
5776
if (d->sock >= 0) {
58-
close(d->sock);
77+
CLOSE_SOCKET(d->sock);
5978
d->sock = -1;
6079
delete d;
61-
d = NULL;
80+
d = nullptr;
6281
}
6382
}
6483

@@ -69,7 +88,7 @@ void StatsdClient::config(const string& host, int port, const string& ns)
6988
d->port = port;
7089
d->init = false;
7190
if ( d->sock >= 0 ) {
72-
close(d->sock);
91+
CLOSE_SOCKET(d->sock);
7392
}
7493
d->sock = -1;
7594
}
@@ -88,25 +107,22 @@ int StatsdClient::init()
88107
d->server.sin_family = AF_INET;
89108
d->server.sin_port = htons(d->port);
90109

91-
int ret = inet_aton(d->host.c_str(), &d->server.sin_addr);
92-
if ( ret == 0 )
93-
{
94-
// host must be a domain, get it from internet
95-
struct addrinfo hints, *result = NULL;
96-
memset(&hints, 0, sizeof(hints));
97-
hints.ai_family = AF_INET;
98-
hints.ai_socktype = SOCK_DGRAM;
99-
100-
ret = getaddrinfo(d->host.c_str(), NULL, &hints, &result);
101-
if ( ret ) {
102-
snprintf(d->errmsg, sizeof(d->errmsg),
103-
"getaddrinfo fail, error=%d, msg=%s", ret, gai_strerror(ret) );
104-
return -2;
105-
}
106-
struct sockaddr_in* host_addr = (struct sockaddr_in*)result->ai_addr;
107-
memcpy(&d->server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
108-
freeaddrinfo(result);
110+
// host must be a domain, get it from internet
111+
struct addrinfo hints, *result = NULL;
112+
memset(&hints, 0, sizeof(hints));
113+
hints.ai_family = AF_INET;
114+
hints.ai_socktype = SOCK_DGRAM;
115+
116+
// looks up IPv4/IPv6 address by host name or stringized IP address
117+
int ret = getaddrinfo(d->host.c_str(), NULL, &hints, &result);
118+
if ( ret ) {
119+
snprintf(d->errmsg, sizeof(d->errmsg),
120+
"getaddrinfo fail, error=%d, msg=%s", ret, gai_strerror(ret) );
121+
return -2;
109122
}
123+
struct sockaddr_in* host_addr = (struct sockaddr_in*)result->ai_addr;
124+
memcpy(&d->server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
125+
freeaddrinfo(result);
110126

111127
d->init = true;
112128
return 0;
@@ -150,7 +166,7 @@ int StatsdClient::timing(const string& key, size_t ms, float sample_rate)
150166

151167
int StatsdClient::send(string key, size_t value, const string &type, float sample_rate)
152168
{
153-
if (!should_send(sample_rate)) {
169+
if (!should_send(this->d, sample_rate)) {
154170
return 0;
155171
}
156172

src/statsd_client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#ifndef STATSD_CLIENT_H
33
#define STATSD_CLIENT_H
44

5-
#include <sys/types.h>
6-
#include <arpa/inet.h>
7-
#include <sys/socket.h>
5+
#include "../StatsdClientConfig.h"
6+
87
#include <string>
98

9+
1010
namespace statsd {
1111

1212
struct _StatsdClientData;

0 commit comments

Comments
 (0)