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
|
// all server side masterserver and pinging functionality
#include "cube.h"
static ENetSocket mssock = ENET_SOCKET_NULL;
static void
httpgetsend(ENetAddress &ad, OFString *hostname, OFString *req, OFString *ref,
OFString *agent)
{
if (ad.host == ENET_HOST_ANY) {
[OFStdOut writeFormat:@"looking up %@...\n", hostname];
enet_address_set_host(&ad, hostname.UTF8String);
if (ad.host == ENET_HOST_ANY)
return;
}
if (mssock != ENET_SOCKET_NULL)
enet_socket_destroy(mssock);
mssock = enet_socket_create(ENET_SOCKET_TYPE_STREAM, NULL);
if (mssock == ENET_SOCKET_NULL) {
printf("could not open socket\n");
return;
}
if (enet_socket_connect(mssock, &ad) < 0) {
printf("could not connect\n");
return;
}
ENetBuffer buf;
OFString *httpget = [OFString stringWithFormat:@"GET %@ HTTP/1.0\n"
@"Host: %@\n"
@"Referer: %@\n"
@"User-Agent: %@\n\n",
req, hostname, ref, agent];
buf.data = (void *)httpget.UTF8String;
buf.dataLength = httpget.UTF8StringLength;
[OFStdOut writeFormat:@"sending request to %@...\n", hostname];
enet_socket_send(mssock, NULL, &buf, 1);
}
static void
httpgetrecieve(ENetBuffer &buf)
{
if (mssock == ENET_SOCKET_NULL)
return;
enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE;
if (enet_socket_wait(mssock, &events, 0) >= 0 && events) {
int len = enet_socket_receive(mssock, NULL, &buf, 1);
if (len <= 0) {
enet_socket_destroy(mssock);
mssock = ENET_SOCKET_NULL;
return;
}
buf.data = ((char *)buf.data) + len;
((char *)buf.data)[0] = 0;
buf.dataLength -= len;
}
}
static uchar *
stripheader(uchar *b)
{
char *s = strstr((char *)b, "\n\r\n");
|
|
|
|
|
|
|
|
|
|
|
|
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
|
// all server side masterserver and pinging functionality
#include "cube.h"
static ENetSocket mssock = ENET_SOCKET_NULL;
static void
httpgetsend(ENetAddress *ad, OFString *hostname, OFString *req, OFString *ref,
OFString *agent)
{
if (ad->host == ENET_HOST_ANY) {
[OFStdOut writeFormat:@"looking up %@...\n", hostname];
enet_address_set_host(ad, hostname.UTF8String);
if (ad->host == ENET_HOST_ANY)
return;
}
if (mssock != ENET_SOCKET_NULL)
enet_socket_destroy(mssock);
mssock = enet_socket_create(ENET_SOCKET_TYPE_STREAM, NULL);
if (mssock == ENET_SOCKET_NULL) {
printf("could not open socket\n");
return;
}
if (enet_socket_connect(mssock, ad) < 0) {
printf("could not connect\n");
return;
}
ENetBuffer buf;
OFString *httpget = [OFString stringWithFormat:@"GET %@ HTTP/1.0\n"
@"Host: %@\n"
@"Referer: %@\n"
@"User-Agent: %@\n\n",
req, hostname, ref, agent];
buf.data = (void *)httpget.UTF8String;
buf.dataLength = httpget.UTF8StringLength;
[OFStdOut writeFormat:@"sending request to %@...\n", hostname];
enet_socket_send(mssock, NULL, &buf, 1);
}
static void
httpgetrecieve(ENetBuffer *buf)
{
if (mssock == ENET_SOCKET_NULL)
return;
enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE;
if (enet_socket_wait(mssock, &events, 0) >= 0 && events) {
int len = enet_socket_receive(mssock, NULL, buf, 1);
if (len <= 0) {
enet_socket_destroy(mssock);
mssock = ENET_SOCKET_NULL;
return;
}
buf->data = ((char *)buf->data) + len;
((char *)buf->data)[0] = 0;
buf->dataLength -= len;
}
}
static uchar *
stripheader(uchar *b)
{
char *s = strstr((char *)b, "\n\r\n");
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
static void
updatemasterserver(int seconds)
{
// send alive signal to masterserver every hour of uptime
if (seconds > updmaster) {
OFString *path = [OFString
stringWithFormat:@"%@register.do?action=add", masterpath];
httpgetsend(masterserver, masterbase, path, @"cubeserver",
@"Cube Server");
masterrep[0] = 0;
masterb.data = masterrep;
masterb.dataLength = MAXTRANS - 1;
updmaster = seconds + 60 * 60;
}
}
static void
checkmasterreply()
{
bool busy = mssock != ENET_SOCKET_NULL;
httpgetrecieve(masterb);
if (busy && mssock == ENET_SOCKET_NULL)
printf("masterserver reply: %s\n", stripheader(masterrep));
}
uchar *
retrieveservers(uchar *buf, int buflen)
{
OFString *path =
[OFString stringWithFormat:@"%@retrieve.do?item=list", masterpath];
httpgetsend(
masterserver, masterbase, path, @"cubeserver", @"Cube Server");
ENetBuffer eb;
buf[0] = 0;
eb.data = buf;
eb.dataLength = buflen - 1;
while (mssock != ENET_SOCKET_NULL)
httpgetrecieve(eb);
return stripheader(buf);
}
static ENetSocket pongsock = ENET_SOCKET_NULL;
static OFString *serverdesc;
void
|
|
|
|
|
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
static void
updatemasterserver(int seconds)
{
// send alive signal to masterserver every hour of uptime
if (seconds > updmaster) {
OFString *path = [OFString
stringWithFormat:@"%@register.do?action=add", masterpath];
httpgetsend(&masterserver, masterbase, path, @"cubeserver",
@"Cube Server");
masterrep[0] = 0;
masterb.data = masterrep;
masterb.dataLength = MAXTRANS - 1;
updmaster = seconds + 60 * 60;
}
}
static void
checkmasterreply()
{
bool busy = mssock != ENET_SOCKET_NULL;
httpgetrecieve(&masterb);
if (busy && mssock == ENET_SOCKET_NULL)
printf("masterserver reply: %s\n", stripheader(masterrep));
}
uchar *
retrieveservers(uchar *buf, int buflen)
{
OFString *path =
[OFString stringWithFormat:@"%@retrieve.do?item=list", masterpath];
httpgetsend(
&masterserver, masterbase, path, @"cubeserver", @"Cube Server");
ENetBuffer eb;
buf[0] = 0;
eb.data = buf;
eb.dataLength = buflen - 1;
while (mssock != ENET_SOCKET_NULL)
httpgetrecieve(&eb);
return stripheader(buf);
}
static ENetSocket pongsock = ENET_SOCKET_NULL;
static OFString *serverdesc;
void
|