-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjoynet.c
executable file
·87 lines (76 loc) · 1.58 KB
/
joynet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "enet/include/enet/enet.h"
#include "joynet.h"
#include "serialize.h"
static long joynet_random_seed = 0;
int joynet_swap_endianess = 0;
static void joynet_detect_endianess(void)
{
unsigned long Value32;
unsigned char * VPtr = (unsigned char *)&Value32;
VPtr[0] = VPtr[1] = VPtr[2] = 0; VPtr[3] = 1;
if(Value32 == 1)
{
joynet_swap_endianess = 1; // big endian
}
else
{
joynet_swap_endianess = 0; // little endian
}
}
int joynet_init(void)
{
if(enet_initialize() != 0)
{
return 0;
}
joynet_detect_endianess();
return 1;
}
void joynet_exit(void)
{
enet_deinitialize();
}
void joynet_ping(const char * url, int port)
{
ENetAddress address;
ENetHost * host;
ENetPeer * peer;
if(enet_address_set_host(&address, url) < 0)
{
return;
}
address.port = port;
host = enet_host_create(NULL, 1, 0, 0, 0);
if(!host)
{
return;
}
peer = enet_host_connect(host, &address, 4, 0);
if(!peer)
{
return;
}
enet_host_destroy(host);
}
void joynet_srand(unsigned int seed)
{
joynet_random_seed = (long)seed;
}
#define JOYNET_RAND_MAX 0xFFFF
int joynet_rand(void)
{
return (((joynet_random_seed = joynet_random_seed * 214013L + 2531011L) >> 16) & JOYNET_RAND_MAX);
}
#define JOYNET_RS_SCALE (1.0 / (1.0 + JOYNET_RAND_MAX))
double joynet_drand(void)
{
double d;
do
{
d = (((joynet_rand() * JOYNET_RS_SCALE) + joynet_rand()) * JOYNET_RS_SCALE + joynet_rand()) * JOYNET_RS_SCALE;
} while (d >= 1); /* round off */
return d;
}