- put all stats related code into one file.

This commit is contained in:
Christoph Oelckers 2018-03-04 17:51:07 +01:00
parent 033a11a028
commit f2f649bf77
3 changed files with 114 additions and 117 deletions

View file

@ -2,8 +2,13 @@
#define _WIN32_WINNT 0x0501 #define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <winsock2.h>
extern int sys_ostype; extern int sys_ostype;
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#endif #endif
#include <thread> #include <thread>
@ -18,13 +23,114 @@ CVAR(String, sys_statshost, "gzstats.drdteam.org", CVAR_ARCHIVE|CVAR_GLOBALCONFI
CVAR(Int, sys_statsport, 80, CVAR_ARCHIVE|CVAR_GLOBALCONFIG|CVAR_NOSET) CVAR(Int, sys_statsport, 80, CVAR_ARCHIVE|CVAR_GLOBALCONFIG|CVAR_NOSET)
// Each machine will only send two reports, one when started with hardware rendering and one when started with software rendering. // Each machine will only send two reports, one when started with hardware rendering and one when started with software rendering.
CVAR(Bool, sentstats_swr, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOSET) #define CHECKVERSION 330
CVAR(Bool, sentstats_hwr, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOSET) #define CHECKVERSIONSTR "330"
CVAR(Int, sentstats_swr_done, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOSET)
CVAR(Int, sentstats_hwr_done, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOSET)
std::pair<double, bool> gl_getInfo(); std::pair<double, bool> gl_getInfo();
bool I_HTTPRequest(const char* request);
#ifdef _WIN32
bool I_HTTPRequest(const char* request)
{
if (sys_statshost.GetHumanString() == NULL)
return false; // no host, disable
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
DPrintf(DMSG_ERROR, "WSAStartup failed.\n");
return false;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(sys_statshost.GetHumanString());
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(sys_statsport);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((uint32_t*)host->h_addr);
DPrintf(DMSG_NOTIFY, "Connecting to host %s\n", sys_statshost.GetHumanString());
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
{
DPrintf(DMSG_ERROR, "Connection to host %s failed!\n", sys_statshost.GetHumanString());
return false;
}
send(Socket, request, (int)strlen(request), 0);
char buffer[1024];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 1024, 0)) > 0)
{
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
{
i++;
}
}
closesocket(Socket);
WSACleanup();
DPrintf(DMSG_NOTIFY, "Stats send successful.\n");
return true;
}
#else
bool I_HTTPRequest(const char* request)
{
if (sys_statshost.GetHumanString() == NULL || sys_statshost.GetHumanString()[0] == 0)
return false; // no host, disable
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
portno = sys_statsport;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
DPrintf(DMSG_ERROR, "Error opening TCP socket.\n");
return false;
}
server = gethostbyname(sys_statshost.GetHumanString());
if (server == NULL)
{
DPrintf(DMSG_ERROR, "Error looking up hostname.\n");
return false;
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
DPrintf(DMSG_NOTIFY, "Connecting to host %s\n", sys_statshost.GetHumanString());
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
DPrintf(DMSG_ERROR, "Connection to host %s failed!\n", sys_statshost.GetHumanString());
return false;
}
char buffer[1024];
sprintf(buffer, "%s", request);
Printf("Buffer: %s", buffer);
n = write(sockfd, (char*)buffer, (int)strlen(request));
if (n<0)
{
DPrintf(DMSG_ERROR, "Error writing to socket.\n");
close(sockfd);
return false;
}
bzero(buffer, 1024);
n = read(sockfd, buffer, 1023);
close(sockfd);
DPrintf(DMSG_NOTIFY, "Stats send successful.\n");
return true;
}
#endif
static int GetOSVersion() static int GetOSVersion()
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -109,11 +215,11 @@ static void D_DoHTTPRequest(const char *request)
{ {
if (currentrenderer == 0) if (currentrenderer == 0)
{ {
cvar_forceset("sentstats_swr", "1"); cvar_forceset("sentstats_swr_done", CHECKVERSIONSTR);
} }
else else
{ {
cvar_forceset("sentstats_hwr", "1"); cvar_forceset("sentstats_hwr_done", CHECKVERSIONSTR);
} }
} }
} }
@ -125,8 +231,8 @@ void D_DoAnonStats()
done = true; done = true;
// Do not repeat if already sent. // Do not repeat if already sent.
if (currentrenderer == 0 && sentstats_swr) return; if (currentrenderer == 0 && sentstats_swr_done >= CHECKVERSION) return;
if (currentrenderer == 1 && sentstats_hwr) return; if (currentrenderer == 1 && sentstats_hwr_done >= CHECKVERSION) return;
static char requeststring[1024]; static char requeststring[1024];
sprintf(requeststring, "GET /stats.php?render=%i&cores=%i&os=%i HTTP/1.1\nHost: %s\nConnection: close\nUser-Agent: %s %s\n\n", sprintf(requeststring, "GET /stats.php?render=%i&cores=%i&os=%i HTTP/1.1\nHost: %s\nConnection: close\nUser-Agent: %s %s\n\n",

View file

@ -36,11 +36,6 @@
#include <SDL.h> #include <SDL.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include "doomerrors.h" #include "doomerrors.h"
#include <math.h> #include <math.h>
@ -74,8 +69,6 @@
#include "g_level.h" #include "g_level.h"
EXTERN_CVAR (String, language) EXTERN_CVAR (String, language)
EXTERN_CVAR(String, sys_statshost)
EXTERN_CVAR(Int, sys_statsport)
extern "C" extern "C"
{ {
@ -497,59 +490,3 @@ TArray<FString> I_GetGogPaths()
return TArray<FString>(); return TArray<FString>();
} }
bool I_HTTPRequest(const char* request)
{
if (sys_statshost.GetHumanString() == NULL || sys_statshost.GetHumanString()[0] == 0)
return false; // no host, disable
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
portno = sys_statsport;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
DPrintf(DMSG_ERROR, "Error opening TCP socket.\n");
return false;
}
server = gethostbyname(sys_statshost.GetHumanString());
if (server == NULL)
{
DPrintf(DMSG_ERROR, "Error looking up hostname.\n");
return false;
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
DPrintf(DMSG_NOTIFY, "Connecting to host %s\n", sys_statshost.GetHumanString());
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
DPrintf(DMSG_ERROR, "Connection to host %s failed!\n", sys_statshost.GetHumanString());
return false;
}
char buffer[1024];
sprintf(buffer, "%s", request);
Printf("Buffer: %s", buffer);
n = write(sockfd, (char*)buffer, (int)strlen(request));
if (n<0)
{
DPrintf(DMSG_ERROR, "Error writing to socket.\n");
close(sockfd);
return false;
}
bzero(buffer, 1024);
n = read(sockfd, buffer, 1023);
close(sockfd);
DPrintf(DMSG_NOTIFY, "Stats send successful.\n");
return true;
}

View file

@ -509,49 +509,3 @@ CCMD (vid_currentmode)
{ {
Printf ("%dx%dx%d\n", DisplayWidth, DisplayHeight, DisplayBits); Printf ("%dx%dx%d\n", DisplayWidth, DisplayHeight, DisplayBits);
} }
#include <winsock2.h>
EXTERN_CVAR(String, sys_statshost)
EXTERN_CVAR(Int, sys_statsport)
bool I_HTTPRequest(const char* request)
{
if (sys_statshost.GetHumanString() == NULL)
return false; // no host, disable
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
DPrintf(DMSG_ERROR, "WSAStartup failed.\n");
return false;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(sys_statshost.GetHumanString());
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(sys_statsport);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((uint32_t*)host->h_addr);
DPrintf(DMSG_NOTIFY, "Connecting to host %s\n", sys_statshost.GetHumanString());
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
{
DPrintf(DMSG_ERROR, "Connection to host %s failed!\n", sys_statshost.GetHumanString());
return false;
}
send(Socket, request, (int)strlen(request), 0);
char buffer[1024];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 1024, 0)) > 0)
{
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
{
i++;
}
}
closesocket(Socket);
WSACleanup();
DPrintf(DMSG_NOTIFY, "Stats send successful.\n");
return true;
}