-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi_main.c
583 lines (519 loc) · 10.5 KB
/
i_main.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#ifndef LINUX
#include <libtransistor/err.h>
#endif
#include "doomdef.h"
#include "doomstat.h"
#include "m_argv.h"
#include "d_main.h"
#include "d_net.h"
#include "i_video.h"
#include "z_zone.h"
#include "g_game.h"
#include "i_system.h"
#include "i_sound.h"
#include <setjmp.h>
#ifdef LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#else
#include <sys/fcntl.h>
#endif
#include "network.h"
#include "cl_cmds.h"
// for exit
jmp_buf exitenv;
extern int key_use;
static struct sockaddr_in client_addr =
{
.sin_family = AF_INET,
};
int client_socket;
int keep_alive;
int last_packet_tic;
#ifndef LINUX
static FILE http_stdout;
static const char http_get_template[] = "GET /files/%s HTTP/1.1\r\nHost: pegaswitch.local\r\nUser-Agent: kgDOOM\r\nAccept-Encoding: none\r\nConnection: close\r\n\r\n";
static struct sockaddr_in server_addr =
{
.sin_family = AF_INET,
.sin_port = htons(80)
};
// +temporary
struct bsd_pollfd
{
int fd;
short events;
short revents;
};
#define BSD_POLLIN 0x0001
// -temporary
static int http_socket;
static int stdout_http(struct _reent *reent, void *v, const char *ptr, int len)
{
bsd_send(http_socket, ptr, len, 0);
return len;
}
static int parse_header(char *buf, int len, int *offset, int *got)
{
char *ptr = buf;
char *pptr = buf;
int ret;
int state = 0;
int content_length = 0;
while(len)
{
// get some bytes
ret = bsd_recv(http_socket, ptr, len, 0);
if(ret <= 0)
return -1;
ptr += ret;
// parse line(s)
while(1)
{
char *tptr = pptr;
char *eptr = pptr + ret;
// get line end
while(tptr < eptr)
{
if(*tptr == '\n')
{
if(tptr - pptr <= 1)
{
// empty line, header ending
if(state)
{
*offset = (int)((tptr + 1) - buf);
*got = (int)((ptr - buf) - *offset);
return content_length;
} else
return -2;
}
// got one line, parse it
if(state)
{
if(!strncmp(pptr, "Content-Length:", 15))
sscanf(pptr + 15, "%d", &content_length);
} else
{
int v1, v2, res;
// HTTP response
state = 1;
if(sscanf(pptr, "HTTP/%d.%d %d", &v1, &v2, &res) != 3 || !res)
return -1;
if(res != 200)
return -res;
}
// go next
pptr = tptr + 1;
break;
}
tptr++;
}
if(tptr == pptr)
// no more lines left
break;
}
// go next
len -= ret;
}
return -1;
}
int http_get_file(const char *path, void **buff)
{
char temp[1024];
int ret, offs, got;
void *ptr;
int size;
http_socket = bsd_socket(2, 1, 6); // AF_INET, SOCK_STREAM, PROTO_TCP
if(http_socket < 0)
return -1;
if(bsd_connect(http_socket, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)
{
bsd_close(http_socket);
return -2;
}
// make a request
fprintf(&http_stdout, http_get_template, path);
// get an answer
ret = parse_header(temp, sizeof(temp), &offs, &got);
// load it now
if(ret > 0)
{
printf("- HTTP file size: %iB\n", ret);
*buff = Z_Malloc(ret, PU_STATIC, NULL);
ptr = *buff;
size = ret;
if(got)
{
memcpy(ptr, temp + offs, got);
ptr += got;
size -= got;
}
while(size)
{
got = bsd_recv(http_socket, ptr, size, 0);
if(got <= 0)
{
bsd_close(http_socket);
printf("- read error\n");
return -4;
}
size -= got;
ptr += got;
}
printf("- file loaded\n");
}
bsd_close(http_socket);
return ret;
}
char *test_argv[] =
{
"doom",
"-warp",
"1",
"2",
NULL
};
// +temporary
int bsd_poll(struct bsd_pollfd *fds, int nfds, int timeout)
{
result_t r;
ipc_object_t bsd_object = bsd_get_object();
int32_t raw[] = {nfds, timeout};
ipc_buffer_t fds_in = {
.addr = fds,
.size = sizeof(struct bsd_pollfd) * nfds,
.type = 0x21
};
ipc_buffer_t fds_out = {
.addr = fds,
.size = sizeof(struct bsd_pollfd) * nfds,
.type = 0x22
};
ipc_buffer_t *buffers[] = {
&fds_in,
&fds_out,
};
ipc_request_t rq = ipc_default_request;
rq.request_id = 6;
rq.raw_data = (uint32_t*)raw;
rq.raw_data_size = sizeof(raw);
rq.num_buffers = 2;
rq.buffers = buffers;
int32_t response[2]; // ret, errno
ipc_response_fmt_t rs = ipc_default_response_fmt;
rs.raw_data_size = sizeof(response);
rs.raw_data = (uint32_t*) response;
r = ipc_send(bsd_object, &rq, &rs);
if(r) {
bsd_result = r;
return -1;
}
if(response[0] < 0) {
bsd_result = LIBTRANSISTOR_ERR_BSD_ERRNO_SET;
bsd_errno = response[1];
return -1;
}
return response[0];
}
// -temporary
#endif
void main_finish()
{
if(netgame)
CL_Disconnect();
I_ShutdownSound();
I_ShutdownGraphics();
#ifndef LINUX
display_finalize();
vi_finalize();
gpu_finalize();
hid_finalize();
bsd_finalize();
sm_finalize();
#endif
}
void I_Error (char *error, ...)
{
va_list vl;
int ret;
va_start(vl, error);
ret = vprintf(error, vl);
va_end(vl);
printf("\n");
longjmp(exitenv, 2);
}
//
// I_Quit
//
void I_Quit (void)
{
longjmp(exitenv, 1);
}
int main(int argc, char **argv)
{
int ret;
myargc = argc;
myargv = argv;
#ifndef LINUX
result_t r;
if((r = sm_init()) != RESULT_OK)
{
printf("- failed to init sm: 0x%x\n", r);
return 1;
}
if((r = bsd_init()) != RESULT_OK)
{
printf("- failed to init bsd: 0x%x, %d\n", r, bsd_errno);
sm_finalize();
return 2;
}
if((r = hid_init()) != RESULT_OK)
{
printf("- failed to init hid: 0x%x\n", r);
bsd_finalize();
sm_finalize();
return 3;
}
// prepare HTTP stdout
if(libtransistor_context.workstation_addr)
server_addr.sin_addr.s_addr = libtransistor_context.workstation_addr;
else
{
server_addr.sin_addr.s_addr = make_ip(192,168,1,47);
server_addr.sin_port = htons(8001);
myargv = test_argv;
myargc = (sizeof(test_argv) / sizeof(char*)) - 1;
}
http_stdout._write = stdout_http;
http_stdout._flags = __SWR | __SNBF;
http_stdout._bf._base = (void*)1;
#endif
srand(time(NULL));
ret = setjmp(exitenv);
if(ret)
{
main_finish();
return ret - 1;
}
D_DoomMain();
return 0;
}
//
// client functions
void I_InitNetwork()
{
int ip[4];
int port;
int p = M_CheckParm ("-connect");
if(p && p < myargc-1 && sscanf(myargv[p+1], "%u.%u.%u.%u:%u", &ip[0], &ip[1], &ip[2], &ip[3], &port) == 5)
{
netgame = 1;
strcpy(network_message, "connecting to server\n....");
client_addr.sin_addr.s_addr = make_ip(ip[0],ip[1],ip[2],ip[3]);
client_addr.sin_port = htons(port);
client_socket = bsd_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(client_socket < 0)
I_Error("I_InitNetwork: failed to create socket");
if(bsd_connect(client_socket, (struct sockaddr*)&client_addr, sizeof(client_addr)))
I_Error("I_InitNetwork: failed to connect socket");
// local spectator player
consoleplayer = MAXPLAYERS;
} else
{
// singleplayer game
consoleplayer = 0;
playercount = 1;
}
displayplayer = consoleplayer;
playeringame[consoleplayer] = true;
}
void I_NetUpdate()
{
static int animate;
int length;
uint8_t cmd;
uint32_t packet;
static uint32_t packetnum;
#ifndef LINUX
static struct bsd_pollfd pfd;
#endif
if(!netgame)
return;
// read and parse all packets
while(1)
{
#ifdef LINUX
length = 0;
ioctl(client_socket, FIONREAD, &length);
if(length <= 0)
break;
if(length > MAX_PACKET_LEN) // should never happen with normal clients
length = MAX_PACKET_LEN;
recvlen = recv(client_socket, recvbuf, length, 0);
#else
pfd.fd = client_socket;
pfd.events = BSD_POLLIN;
pfd.revents = 0;
length = bsd_poll(&pfd, 1, 0);
if(length != 1 || !(pfd.revents & BSD_POLLIN))
break;
length = MAX_PACKET_LEN;
recvlen = bsd_recv(client_socket, recvbuf, length, 0);
if(recvlen <= 0)
break;
#endif
recvpos = 0;
if(!NET_GetUint32(&packet))
{
if(packet == 0xFFFFFFFF)
{
// unreliable; get current reliable packet number, just to check for any losses
NET_GetUint32(&packet);
// check for eventual packet loss, do not advance anything
if(packet >= packetnum)
{
// missed at least one packet, request
CL_RequestResend(packetnum);
// ignore rest of this packet
continue;
}
} else
{
if(packet != packetnum)
{
if(packet > packetnum)
{
// request resend
CL_RequestResend(packetnum);
}
// ignore rest of this packet
continue;
} else
// advance counter
packetnum = packet + 1;
}
while(!NET_GetByte(&cmd))
{
last_packet_tic = gametic;
switch(cmd)
{
case SVM_CONNECT:
CL_CmdConnected();
break;
case SVM_DISCONNECT:
CL_CmdDisconnected();
break;
case SVM_KEEP_ALIVE:
// do nothing
break;
case SVM_CHANGE_SECTOR:
CL_CmdChangeSector();
break;
case SVM_CHANGE_LSIDE:
CL_CmdChangeSidedef();
break;
case SVM_SPAWN_MOBJ:
CL_CmdSpawnMobj();
break;
case SVM_SPAWN_PLAYER:
CL_CmdSpawnPlayer();
break;
case SVM_CEILING:
CL_CmdCeiling();
break;
case SVM_FLOOR:
CL_CmdFloor();
break;
case SVM_PLAYER_INFO:
CL_CmdPlayerInfo();
break;
case SVM_UPDATE_MOBJ:
CL_CmdUpdateMobj();
break;
case SVM_REMOVE_MOBJ:
CL_CmdRemoveMobj();
break;
case SVM_PLAYER_PICKUP:
CL_CmdPlayerPickup();
break;
case SVM_PLAYER_INVENTORY:
CL_CmdPlayerInventory();
break;
case SVM_PLAYER_MESSAGE:
CL_CmdPlayerMessage();
break;
case SVM_SOUND:
CL_CmdSound();
break;
default:
// something is wrong, ignore packet
recvpos = recvlen;
break;
}
}
}
}
if(netgame == 1)
{
// connecting
if(net_timeout < gametic)
{
// send request
CL_Connect();
// reset timeout
net_timeout = gametic + TICRATE;
network_message[22+animate] = '.';
animate = (animate + 1) & 3;
network_message[22+animate] = 0;
}
} else
if(netgame == 2)
{
if(gamestate == GS_LEVEL)
{
if(net_timeout < gametic)
{
net_timeout = gametic + CLIENT_MOTDTIME;
netgame = 3;
CL_Loaded();
printf("- level loaded\n");
}
}
} else
{
if(net_timeout && net_timeout < gametic)
{
// remove MOTD
net_timeout = 0;
network_message[0] = 0;
}
if(netgame == 3)
{
if(last_packet_tic + 2 * SERVER_CLIENT_KEEPALIVE < gametic)
{
netgame = 4;
strcpy(network_message, "Connection lost ...");
} else
if(keep_alive < gametic)
CL_KeepAlive();
} else
{
if(last_packet_tic + 2 * SERVER_CLIENT_KEEPALIVE > gametic)
{
netgame = 3;
network_message[0] = 0;
}
}
}
// spectator join
if(players[consoleplayer].cheats & CF_SPECTATOR && (gamekeydown[key_use] || joybuttons[joybuse]))
{
gamekeydown[key_use] = 0;
joybuttons[joybuse] = 0;
CL_Join(0);
}
// send out all buffered commands
NET_SendCommand();
}