Linux Compatibility
This commit is contained in:
parent
e465875622
commit
40d2e5f6ce
|
@ -1,5 +1,5 @@
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
#include "../multiplatform.h"
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
|
@ -39,10 +39,14 @@ int main(void)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
system("pause");
|
// system("pause");
|
||||||
|
printf("Press Any key to exit...\n");
|
||||||
|
int i;
|
||||||
|
for (i = 0;i < usi.thread_count;i++)
|
||||||
|
pthread_join (usi.threads[i], NULL);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
UDPTracker_destroy(&usi);
|
// UDPTracker_destroy(&usi);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
|
|
|
@ -8,9 +8,14 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#define SOCKET int
|
#define SOCKET int
|
||||||
|
#define INVALID_SOCKET 0
|
||||||
|
#define SOCKET_ERROR -1
|
||||||
#define DWORD uint64_t
|
#define DWORD uint64_t
|
||||||
typedef struct hostent HOSTENT;
|
typedef struct hostent HOSTENT;
|
||||||
typedef struct sockaddr SOCKADDR;
|
typedef struct sockaddr SOCKADDR;
|
||||||
|
@ -19,8 +24,10 @@ typedef struct in_addr IN_ADDR;
|
||||||
typedef struct hostent HOSTENT;
|
typedef struct hostent HOSTENT;
|
||||||
typedef void* LPVOID;
|
typedef void* LPVOID;
|
||||||
typedef void (LPTHREAD_START_ROUTINE)(LPVOID);
|
typedef void (LPTHREAD_START_ROUTINE)(LPVOID);
|
||||||
typedef void* HANDLE;
|
typedef pthread_t HANDLE;
|
||||||
#define IPPROTO_UDP 0 // no protocol set.. SOCK_DGRAM is enough.
|
//#define IPPROTO_UDP 0 // no protocol set.. SOCK_DGRAM is enough.
|
||||||
|
|
||||||
|
#define min(a,b) (a > b ? b : a)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,22 @@
|
||||||
|
|
||||||
#include <winsock2.h>
|
//#include <winsock2.h>
|
||||||
#include <windows.h>
|
//#include <windows.h>
|
||||||
|
#include "multiplatform.h"
|
||||||
#include "udpTracker.h"
|
#include "udpTracker.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <winerror.h>
|
|
||||||
|
|
||||||
#define FLAG_RUNNING 0x01
|
#define FLAG_RUNNING 0x01
|
||||||
#define UDP_BUFFER_SIZE 256
|
#define UDP_BUFFER_SIZE 256
|
||||||
|
|
||||||
static DWORD _thread_start (LPVOID);
|
#ifdef WIN32
|
||||||
|
static DWORD _thread_start (LPVOID arg);
|
||||||
|
#elif defined (linux)
|
||||||
|
static void* _thread_start (void *arg);
|
||||||
|
#endif
|
||||||
|
|
||||||
void UDPTracker_init (udpServerInstance *usi, uint16_t port, uint8_t threads)
|
void UDPTracker_init (udpServerInstance *usi, uint16_t port, uint8_t threads)
|
||||||
{
|
{
|
||||||
|
@ -36,19 +41,26 @@ int UDPTracker_start (udpServerInstance *usi)
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
SOCKADDR_IN recvAddr;
|
SOCKADDR_IN recvAddr;
|
||||||
|
#ifdef WIN32
|
||||||
recvAddr.sin_addr.S_un.S_addr = 0L;
|
recvAddr.sin_addr.S_un.S_addr = 0L;
|
||||||
|
#elif defined (linux)
|
||||||
|
recvAddr.sin_addr.s_addr = 0L;
|
||||||
|
#endif
|
||||||
recvAddr.sin_family = AF_INET;
|
recvAddr.sin_family = AF_INET;
|
||||||
recvAddr.sin_port = htons (usi->port);
|
recvAddr.sin_port = htons (usi->port);
|
||||||
|
|
||||||
BOOL yup = TRUE;
|
int yup = 1;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&yup, sizeof(BOOL));
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&yup, 1);
|
||||||
|
|
||||||
r = bind (sock, (SOCKADDR*)&recvAddr, sizeof(SOCKADDR_IN));
|
r = bind (sock, (SOCKADDR*)&recvAddr, sizeof(SOCKADDR_IN));
|
||||||
|
|
||||||
if (r == SOCKET_ERROR)
|
if (r == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
|
#ifdef WIN32
|
||||||
closesocket (sock);
|
closesocket (sock);
|
||||||
|
#elif defined (linux)
|
||||||
|
close (sock);
|
||||||
|
#endif
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +74,11 @@ int UDPTracker_start (udpServerInstance *usi)
|
||||||
for (i = 0;i < usi->thread_count; i++)
|
for (i = 0;i < usi->thread_count; i++)
|
||||||
{
|
{
|
||||||
printf("Starting Thread %d of %u\n", (i + 1), usi->thread_count);
|
printf("Starting Thread %d of %u\n", (i + 1), usi->thread_count);
|
||||||
|
#ifdef WIN32
|
||||||
usi->threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_thread_start, (LPVOID)usi, 0, NULL);
|
usi->threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_thread_start, (LPVOID)usi, 0, NULL);
|
||||||
|
#elif defined (linux)
|
||||||
|
pthread_create (&usi->threads[i], NULL, _thread_start, usi);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -74,7 +90,11 @@ static uint64_t _get_connID (SOCKADDR_IN *remote)
|
||||||
base /= 3600; // changes every day.
|
base /= 3600; // changes every day.
|
||||||
|
|
||||||
uint64_t x = base;
|
uint64_t x = base;
|
||||||
|
#ifdef WIN32
|
||||||
x += remote->sin_addr.S_un.S_addr;
|
x += remote->sin_addr.S_un.S_addr;
|
||||||
|
#elif defined (linux)
|
||||||
|
x += remote->sin_addr.s_addr;
|
||||||
|
#endif
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,7 +224,11 @@ static int _resolve_request (udpServerInstance *usi, SOCKADDR_IN *remote, char *
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
static DWORD _thread_start (LPVOID arg)
|
static DWORD _thread_start (LPVOID arg)
|
||||||
|
#elif defined (linux)
|
||||||
|
static void* _thread_start (void *arg)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
udpServerInstance *usi = arg;
|
udpServerInstance *usi = arg;
|
||||||
|
|
||||||
|
@ -218,7 +242,7 @@ static DWORD _thread_start (LPVOID arg)
|
||||||
{
|
{
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
// peek into the first 12 bytes of data; determine if connection request or announce request.
|
// peek into the first 12 bytes of data; determine if connection request or announce request.
|
||||||
r = recvfrom(usi->sock, tmpBuff, UDP_BUFFER_SIZE, 0, (SOCKADDR*)&remoteAddr, &addrSz);
|
r = recvfrom(usi->sock, tmpBuff, UDP_BUFFER_SIZE, 0, (SOCKADDR*)&remoteAddr, (unsigned*)&addrSz);
|
||||||
printf("RECV:%d\n", r);
|
printf("RECV:%d\n", r);
|
||||||
r = _resolve_request(usi, &remoteAddr, tmpBuff);
|
r = _resolve_request(usi, &remoteAddr, tmpBuff);
|
||||||
printf("R=%d\n", r);
|
printf("R=%d\n", r);
|
||||||
|
|
Loading…
Reference in a new issue