Compiled for windows and fixed stuff accordingly.

This commit is contained in:
irungentoo 2013-07-03 16:35:44 -04:00
parent 1e17492b78
commit ccb270add2
4 changed files with 31 additions and 9 deletions

View File

@ -40,10 +40,10 @@
#define CONNEXION_TIMEOUT 10 #define CONNEXION_TIMEOUT 10
//initial amount of sync/hanshake packets to send per second. //initial amount of sync/hanshake packets to send per second.
#define SYNC_RATE 50 #define SYNC_RATE 10
//initial send rate of sync packets when data is being sent/recieved. //initial send rate of data.
#define DATA_SYNC_RATE 200 #define DATA_SYNC_RATE 30
typedef struct typedef struct
{ {

View File

@ -25,26 +25,34 @@
#include "network.h" #include "network.h"
//returns current time in milliseconds since the epoch. //returns current UNIX time in microseconds (us).
uint64_t current_time() uint64_t current_time()
{ {
uint64_t time; uint64_t time;
#ifdef WIN32 #ifdef WIN32
//TODO: windows version //This probably works fine
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
time = ft.dwHighDateTime;
time <<=32;
time |= ft.dwLowDateTime;
time -= 116444736000000000UL;
return time/10;
#else #else
struct timeval a; struct timeval a;
gettimeofday(&a, NULL); gettimeofday(&a, NULL);
time = 1000000UL*a.tv_sec + a.tv_usec; time = 1000000UL*a.tv_sec + a.tv_usec;
#endif
return time; return time;
#endif
} }
uint32_t random_int() uint32_t random_int()
{ {
#ifdef WIN32 #ifdef WIN32
//TODO replace rand with something cryptograhically secure //NOTE: this function comes from libsodium
return rand(); return randombytes_random();
#else #else
return random(); return random();
#endif #endif
@ -69,7 +77,11 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) int recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
{ {
ADDR addr; ADDR addr;
#ifdef WIN32
int addrlen = sizeof(addr);
#else
uint32_t addrlen = sizeof(addr); uint32_t addrlen = sizeof(addr);
#endif
(*(int32_t *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); (*(int32_t *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if(*(int32_t *)length <= 0) if(*(int32_t *)length <= 0)
{ {

View File

@ -33,11 +33,15 @@
#include <time.h> #include <time.h>
#ifdef WIN32 //Put win32 includes here #ifdef WIN32 //Put win32 includes here
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
//we use libsodium (Portable version of NaCl) because stock NaCl doesn't compile on windows.
#include <sodium.h>
#else //Linux includes #else //Linux includes
#include <fcntl.h> #include <fcntl.h>
@ -45,6 +49,12 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
//TODO: Including stuff like this is bad. This needs fixing.
//We keep support for the original NaCl for now on UNIX like Operating Systems.
//Commented out for now
//#include "../nacl/build/Linux/include/amd64/crypto_box.h"
#endif #endif
#define MAX_UDP_PACKET_SIZE 65507 #define MAX_UDP_PACKET_SIZE 65507

View File

@ -15,7 +15,7 @@ Lossless UDP:
Alice puts it in the handshake packet (handshake_id1). Alice puts it in the handshake packet (handshake_id1).
Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.) Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.)
Bob receives the packet. Bob receives the packet.
Bob copies the handshake packet he got from alice but caternates a random 4 byte number to it (handshake_id2) Bob copies the handshake packet he got from alice but concatenates a random 4 byte number to it (handshake_id2)
Alice receives the packet, checks if handshake_id1 matches the one she sent. Alice receives the packet, checks if handshake_id1 matches the one she sent.
If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 and recv_packetnum = handshake_id1. If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 and recv_packetnum = handshake_id1.
Bob receives the packet, Bob receives the packet,