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

Skip to content

Make it win32 and cmake friendly #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required (VERSION 3.2)
project (StatsdClient)

set (StatsdClient_VERSION_MAJOR 1)
set (StatsdClient_VERSION_MINOR 0)

include_directories ("${PROJECT_SOURCE_DIR}/src")
add_subdirectory (src)

configure_file (
"${PROJECT_SOURCE_DIR}/src/StatsdClientConfig.h.in"
"${PROJECT_BINARY_DIR}/StatsdClientConfig.h"
)

target_compile_features(StatsdClient PRIVATE cxx_nullptr)

include_directories("${PROJECT_BINARY_DIR}")

install (FILES "${PROJECT_BINARY_DIR}/StatsdClientConfig.h"
DESTINATION include)

4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_library(StatsdClient statsd_client.cpp)

install (TARGETS StatsdClient DESTINATION lib)
install (FILES statsd_client.h DESTINATION include)
21 changes: 0 additions & 21 deletions src/Makefile

This file was deleted.

2 changes: 2 additions & 0 deletions src/StatsdClientConfig.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define StatsdClient_VERSION_MAJOR @StatsdClient_VERSION_MAJOR@
#define StatsdClient_VERSION_MINOR @StatsdClient_VERSION_MINOR@
94 changes: 55 additions & 39 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@

#include <math.h>
#include <netdb.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <random>
#include "statsd_client.h"
#include <fcntl.h>


/* platform-specific headers */
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define CLOSE_SOCKET(s) closesocket(s)
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h> /* Needed for getaddrinfo() and freeaddrinfo() */
#include <unistd.h> /* Needed for close() */

#define CLOSE_SOCKET(s) close(s)
#endif

using namespace std;
namespace statsd {
Expand All @@ -20,17 +31,6 @@ inline bool fequal(float a, float b)
return ( fabs(a - b) < epsilon );
}

inline bool should_send(float sample_rate)
{
if ( fequal(sample_rate, 1.0) )
{
return true;
}

float p = ((float)random() / RAND_MAX);
return sample_rate > p;
}

struct _StatsdClientData {
int sock;
struct sockaddr_in server;
Expand All @@ -40,25 +40,44 @@ struct _StatsdClientData {
short port;
bool init;

std::default_random_engine rng_engine;
std::uniform_real_distribution<> rng_dist;


char errmsg[1024];
};

inline bool should_send(_StatsdClientData* d, float sample_rate)
{
if ( fequal(sample_rate, 1.0) )
{
return true;
}

float p = d->rng_dist(d->rng_engine);
return sample_rate > p;
}

StatsdClient::StatsdClient(const string& host, int port, const string& ns)
{
d = new _StatsdClientData;

d->sock = -1;
std::random_device rd;
d->rng_engine = std::default_random_engine(rd () );
d->rng_dist = std::uniform_real_distribution<>(0.0f, 1.0f);

config(host, port, ns);
srandom(time(NULL));
}

StatsdClient::~StatsdClient()
{
// close socket
if (d->sock >= 0) {
close(d->sock);
CLOSE_SOCKET(d->sock);
d->sock = -1;
delete d;
d = NULL;
d = nullptr;
}
}

Expand All @@ -69,7 +88,7 @@ void StatsdClient::config(const string& host, int port, const string& ns)
d->port = port;
d->init = false;
if ( d->sock >= 0 ) {
close(d->sock);
CLOSE_SOCKET(d->sock);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason for removing this? doesn't that take away the ability to pass an IP address?

}
d->sock = -1;
}
Expand All @@ -88,25 +107,22 @@ int StatsdClient::init()
d->server.sin_family = AF_INET;
d->server.sin_port = htons(d->port);

int ret = inet_aton(d->host.c_str(), &d->server.sin_addr);
if ( ret == 0 )
{
// host must be a domain, get it from internet
struct addrinfo hints, *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;

ret = getaddrinfo(d->host.c_str(), NULL, &hints, &result);
if ( ret ) {
snprintf(d->errmsg, sizeof(d->errmsg),
"getaddrinfo fail, error=%d, msg=%s", ret, gai_strerror(ret) );
return -2;
}
struct sockaddr_in* host_addr = (struct sockaddr_in*)result->ai_addr;
memcpy(&d->server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
freeaddrinfo(result);
// host must be a domain, get it from internet
struct addrinfo hints, *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;

// looks up IPv4/IPv6 address by host name or stringized IP address
int ret = getaddrinfo(d->host.c_str(), NULL, &hints, &result);
if ( ret ) {
snprintf(d->errmsg, sizeof(d->errmsg),
"getaddrinfo fail, error=%d, msg=%s", ret, gai_strerror(ret) );
return -2;
}
struct sockaddr_in* host_addr = (struct sockaddr_in*)result->ai_addr;
memcpy(&d->server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
freeaddrinfo(result);

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

int StatsdClient::send(string key, size_t value, const string &type, float sample_rate)
{
if (!should_send(sample_rate)) {
if (!should_send(this->d, sample_rate)) {
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#ifndef STATSD_CLIENT_H
#define STATSD_CLIENT_H

#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "../StatsdClientConfig.h"

#include <string>


namespace statsd {

struct _StatsdClientData;
Expand Down