Skip to content

Commit

Permalink
Added Windows Support
Browse files Browse the repository at this point in the history
Added support for sending metrics on Windows
  • Loading branch information
Chris Board committed Nov 22, 2018
1 parent 5357a29 commit e594659
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
38 changes: 24 additions & 14 deletions DataDogStatsD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,33 +435,43 @@ size_t DataDogStatsD::curlResponseWriteCallback(void *contents, size_t size, siz

void DataDogStatsD::flush(string& udp_message)
{
#ifndef _WIN32
#ifdef _WIN32
WSAData wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result == 0)
{
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(this->host.c_str());
service.sin_port = htons(this->port);
//result = ::bind(sock, (SOCKADDR *)&service, sizeof(service));
result = ::connect(sock, (SOCKADDR *)&service, sizeof(service));
if (result != SOCKET_ERROR)
{
::send(sock, udp_message.c_str(), udp_message.length(), 0);
closesocket(sock);
WSACleanup();
}
}
else
{
WSACleanup();
}
#else

int udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/*struct sockaddr_in sin;
sin.sin_family = AF_INET;
inet_pton(AF_INET, this->host.c_str(), &sin.sin_addr);
// sin.sin_addr.s_addr = htonl(this->host.c_str());
sin.sin_port = htons(this->port);*/

struct sockaddr_in serv_addr;
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
//serv_addr.sin_addr.s_addr = htonl(this->host.c_str());
inet_pton(AF_INET, this->host.c_str(), &serv_addr.sin_addr);
serv_addr.sin_port = htons(this->port);

/*if (!bind(udp_socket, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
cout << "Failed to bind UDP port" << endl;
}*/

connect(udp_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

write(udp_socket, udp_message.c_str(), udp_message.length());


close(udp_socket);
#endif
}
Expand Down
14 changes: 13 additions & 1 deletion DataDogStatsD.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@
#include "DDEvent.h"
#include <thread>

#ifndef _WIN32
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <Ws2tcpip.h>
#include <inaddr.h>
#pragma comment (lib, "Ws2_32.lib")

#else
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SOURCES = DataDogStatsD.cpp DDEvent.cpp Helpers.cpp

lib_name = libDataDogStatsD.so.1.1.0.0
lib_name = libDataDogStatsD.so.1.1.0.4

curl_include = /usr/include/curl
rapidjson_inc_path = /usr/include/rapidjson
Expand Down

0 comments on commit e594659

Please sign in to comment.