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
583
584
585
586
587
588
589
590
591
|
/*
Copyright (C) 2013 Andrey Nazarov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// gtv.c
//
#include "client.h"
#include "server/mvd/protocol.h"
static byte gtv_recv_buffer[MAX_GTC_MSGLEN];
static byte gtv_send_buffer[MAX_GTS_MSGLEN*2];
static byte gtv_message_buffer[MAX_MSGLEN];
static void build_gamestate(void)
{
centity_t *ent;
int i;
memset(cls.gtv.entities, 0, sizeof(cls.gtv.entities));
// set base player states
MSG_PackPlayer(&cls.gtv.ps, &cl.frame.ps);
// set base entity states
for (i = 1; i < MAX_EDICTS; i++) {
ent = &cl_entities[i];
if (ent->serverframe != cl.frame.number) {
continue;
}
MSG_PackEntity(&cls.gtv.entities[i], &ent->current, qfalse);
}
}
static void emit_gamestate(void)
{
char *string;
int i, j;
entity_packed_t *es;
size_t length;
int flags;
// send the serverdata
MSG_WriteByte(mvd_serverdata | (MVF_SINGLEPOV << SVCMD_BITS));
MSG_WriteLong(PROTOCOL_VERSION_MVD);
MSG_WriteShort(PROTOCOL_VERSION_MVD_CURRENT);
MSG_WriteLong(cl.servercount);
MSG_WriteString(cl.gamedir);
MSG_WriteShort(-1);
// send configstrings
for (i = 0; i < MAX_CONFIGSTRINGS; i++) {
string = cl.configstrings[i];
if (!string[0]) {
continue;
}
length = strlen(string);
if (length > MAX_QPATH) {
length = MAX_QPATH;
}
MSG_WriteShort(i);
MSG_WriteData(string, length);
MSG_WriteByte(0);
}
MSG_WriteShort(MAX_CONFIGSTRINGS);
// send portal bits
MSG_WriteByte(0);
// send player state
MSG_WriteDeltaPlayerstate_Packet(NULL, &cls.gtv.ps,
cl.clientNum, MSG_PS_FORCE);
MSG_WriteByte(CLIENTNUM_NONE);
// send entity states
for (i = 1, es = cls.gtv.entities + 1; i < MAX_EDICTS; i++, es++) {
flags = MSG_ES_UMASK;
if ((j = es->number) == 0) {
flags |= MSG_ES_REMOVE;
}
es->number = i;
MSG_WriteDeltaEntity(NULL, es, flags);
es->number = j;
}
MSG_WriteShort(0);
}
void CL_GTV_EmitFrame(void)
{
player_packed_t newps;
entity_packed_t *oldes, newes;
centity_t *ent;
int i, flags;
if (cls.gtv.state != ca_active)
return;
if (!CL_FRAMESYNC)
return;
if (!cl.frame.valid)
return;
MSG_WriteByte(mvd_frame);
// send portal bits
MSG_WriteByte(0);
// send player state
MSG_PackPlayer(&newps, &cl.frame.ps);
MSG_WriteDeltaPlayerstate_Packet(&cls.gtv.ps, &newps,
cl.clientNum, MSG_PS_FORCE);
// shuffle current state to previous
cls.gtv.ps = newps;
MSG_WriteByte(CLIENTNUM_NONE); // end of packetplayers
// send entity states
for (i = 1; i < MAX_EDICTS; i++) {
oldes = &cls.gtv.entities[i];
ent = &cl_entities[i];
if (ent->serverframe != cl.frame.number) {
if (oldes->number) {
// the old entity isn't present in the new message
MSG_WriteDeltaEntity(oldes, NULL, MSG_ES_FORCE);
oldes->number = 0;
}
continue;
}
// calculate flags
flags = MSG_ES_UMASK;
if (!oldes->number) {
// this is a new entity, send it from the last state
flags |= MSG_ES_FORCE | MSG_ES_NEWENTITY;
}
// quantize
MSG_PackEntity(&newes, &ent->current, qfalse);
MSG_WriteDeltaEntity(oldes, &newes, flags);
// shuffle current state to previous
*oldes = newes;
}
MSG_WriteShort(0); // end of packetentities
SZ_Write(&cls.gtv.message, msg_write.data, msg_write.cursize);
SZ_Clear(&msg_write);
}
static void drop_client(const char *reason)
{
if (reason)
Com_Printf("MVD client [%s] dropped: %s\n",
NET_AdrToString(&cls.gtv.stream.address), reason);
NET_UpdateStream(&cls.gtv.stream);
NET_Sleep(0);
NET_RunStream(&cls.gtv.stream);
NET_RunStream(&cls.gtv.stream);
NET_CloseStream(&cls.gtv.stream);
cls.gtv.state = ca_disconnected;
}
static void write_stream(void *data, size_t len)
{
if (cls.gtv.state <= ca_disconnected) {
return;
}
if (FIFO_Write(&cls.gtv.stream.send, data, len) != len) {
drop_client("overflowed");
}
}
static void write_message(gtv_serverop_t op)
{
byte header[3];
size_t len = msg_write.cursize + 1;
header[0] = len & 255;
header[1] = (len >> 8) & 255;
header[2] = op;
write_stream(header, sizeof(header));
write_stream(msg_write.data, msg_write.cursize);
}
void CL_GTV_WriteMessage(byte *data, size_t len)
{
int bits;
if (cls.gtv.state != ca_active)
return;
if (cls.state != ca_active)
return;
if (len == 0)
return;
switch (data[0]) {
case svc_configstring:
SZ_WriteByte(&cls.gtv.message, mvd_configstring);
SZ_Write(&cls.gtv.message, data + 1, len - 1);
break;
case svc_print:
SZ_WriteByte(&cls.gtv.message, mvd_print);
SZ_Write(&cls.gtv.message, data + 1, len - 1);
break;
case svc_layout:
case svc_stufftext:
bits = ((len >> 8) & 7) << SVCMD_BITS;
SZ_WriteByte(&cls.gtv.message, mvd_unicast | bits);
SZ_WriteByte(&cls.gtv.message, len & 255);
SZ_WriteByte(&cls.gtv.message, cl.clientNum);
SZ_Write(&cls.gtv.message, data, len);
break;
default:
bits = ((len >> 8) & 7) << SVCMD_BITS;
SZ_WriteByte(&cls.gtv.message, mvd_multicast_all | bits);
SZ_WriteByte(&cls.gtv.message, len & 255);
SZ_Write(&cls.gtv.message, data, len);
break;
}
}
void CL_GTV_Resume(void)
{
if (cls.gtv.state != ca_active)
return;
SZ_Init(&cls.gtv.message, gtv_message_buffer, sizeof(gtv_message_buffer));
build_gamestate();
emit_gamestate();
write_message(GTS_STREAM_DATA);
SZ_Clear(&msg_write);
}
void CL_GTV_Suspend(void)
{
if (cls.gtv.state != ca_active)
return;
// send stream suspend marker
write_message(GTS_STREAM_DATA);
}
void CL_GTV_Transmit(void)
{
byte header[3];
size_t total;
if (cls.gtv.state != ca_active)
return;
if (cls.state != ca_active)
return;
if (!CL_FRAMESYNC)
return;
if (cls.gtv.message.overflowed) {
Com_WPrintf("MVD message overflowed.\n");
goto clear;
}
if (!cls.gtv.message.cursize)
return;
// build message header
total = cls.gtv.message.cursize + 1;
header[0] = total & 255;
header[1] = (total >> 8) & 255;
header[2] = GTS_STREAM_DATA;
// send frame to client
write_stream(header, sizeof(header));
write_stream(cls.gtv.message.data, cls.gtv.message.cursize);
NET_UpdateStream(&cls.gtv.stream);
clear:
// clear datagram
SZ_Clear(&cls.gtv.message);
}
static void parse_hello(void)
{
int protocol;
if (cls.gtv.state >= ca_precached) {
drop_client("duplicated hello message");
return;
}
protocol = MSG_ReadWord();
if (protocol != GTV_PROTOCOL_VERSION) {
write_message(GTS_BADREQUEST);
drop_client("bad protocol version");
return;
}
MSG_ReadLong();
MSG_ReadLong();
MSG_ReadString(NULL, 0);
MSG_ReadString(NULL, 0);
MSG_ReadString(NULL, 0);
// authorize access
if (!NET_IsLanAddress(&cls.gtv.stream.address)) {
write_message(GTS_NOACCESS);
drop_client("not authorized");
return;
}
cls.gtv.state = ca_precached;
// send hello
MSG_WriteLong(0);
write_message(GTS_HELLO);
SZ_Clear(&msg_write);
Com_Printf("Accepted MVD client [%s]\n",
NET_AdrToString(&cls.gtv.stream.address));
}
static void parse_ping(void)
{
if (cls.gtv.state < ca_precached) {
return;
}
// send ping reply
write_message(GTS_PONG);
}
static void parse_stream_start(void)
{
if (cls.gtv.state != ca_precached) {
drop_client("unexpected stream start message");
return;
}
// skip maxbuf
MSG_ReadShort();
cls.gtv.state = ca_active;
// tell the server we are recording
CL_UpdateRecordingSetting();
// send ack to client
write_message(GTS_STREAM_START);
// send gamestate if active
if (cls.state == ca_active) {
CL_GTV_Resume();
} else {
// send stream suspend marker
write_message(GTS_STREAM_DATA);
}
}
static void parse_stream_stop(void)
{
if (cls.gtv.state != ca_active) {
drop_client("unexpected stream stop message");
return;
}
cls.gtv.state = ca_precached;
// tell the server we finished recording
CL_UpdateRecordingSetting();
// send ack to client
write_message(GTS_STREAM_STOP);
}
static qboolean parse_message(void)
{
uint32_t magic;
uint16_t msglen;
int cmd;
if (cls.gtv.state <= ca_disconnected) {
return qfalse;
}
// check magic
if (cls.gtv.state < ca_connected) {
if (!FIFO_TryRead(&cls.gtv.stream.recv, &magic, 4)) {
return qfalse;
}
if (magic != MVD_MAGIC) {
drop_client("not a MVD/GTV stream");
return qfalse;
}
cls.gtv.state = ca_connected;
// send it back
write_stream(&magic, 4);
return qfalse;
}
// parse msglen
if (!cls.gtv.msglen) {
if (!FIFO_TryRead(&cls.gtv.stream.recv, &msglen, 2)) {
return qfalse;
}
msglen = LittleShort(msglen);
if (!msglen) {
drop_client("end of stream");
return qfalse;
}
if (msglen > MAX_GTC_MSGLEN) {
drop_client("oversize message");
return qfalse;
}
cls.gtv.msglen = msglen;
}
// read this message
if (!FIFO_ReadMessage(&cls.gtv.stream.recv, cls.gtv.msglen)) {
return qfalse;
}
cls.gtv.msglen = 0;
cmd = MSG_ReadByte();
switch (cmd) {
case GTC_HELLO:
parse_hello();
break;
case GTC_PING:
parse_ping();
break;
case GTC_STREAM_START:
parse_stream_start();
break;
case GTC_STREAM_STOP:
parse_stream_stop();
break;
case GTC_STRINGCMD:
break;
default:
drop_client("unknown command byte");
return qfalse;
}
if (msg_read.readcount > msg_read.cursize) {
drop_client("read past end of message");
return qfalse;
}
return qtrue;
}
void CL_GTV_Run(void)
{
neterr_t ret;
if (!cls.gtv.state)
return;
if (cls.gtv.state == ca_disconnected) {
ret = NET_Accept(&cls.gtv.stream);
if (ret != NET_OK)
return;
Com_DPrintf("TCP client [%s] accepted\n",
NET_AdrToString(&cls.gtv.stream.address));
cls.gtv.state = ca_connecting;
cls.gtv.stream.recv.data = gtv_recv_buffer;
cls.gtv.stream.recv.size = sizeof(gtv_recv_buffer);
cls.gtv.stream.send.data = gtv_send_buffer;
cls.gtv.stream.send.size = sizeof(gtv_send_buffer);
}
ret = NET_RunStream(&cls.gtv.stream);
switch (ret) {
case NET_AGAIN:
break;
case NET_OK:
// parse the message
while (parse_message())
;
NET_UpdateStream(&cls.gtv.stream);
break;
case NET_CLOSED:
drop_client("EOF from client");
break;
case NET_ERROR:
drop_client("connection reset by peer");
break;
}
}
static void CL_GTV_Start_f(void)
{
neterr_t ret;
if (cls.gtv.state) {
Com_Printf("Client GTV already started.\n");
return;
}
ret = NET_Listen(qtrue);
if (ret == NET_OK) {
Com_Printf("Listening for GTV connections.\n");
cls.gtv.state = ca_disconnected;
} else if (ret == NET_ERROR) {
Com_EPrintf("%s while opening client TCP port.\n", NET_ErrorString());
} else {
Com_EPrintf("Client TCP port already in use.\n");
}
}
static void CL_GTV_Stop_f(void)
{
if (!cls.gtv.state) {
Com_Printf("Client GTV already stopped.\n");
return;
}
NET_Listen(qfalse);
write_message(GTS_DISCONNECT);
drop_client(NULL);
memset(&cls.gtv, 0, sizeof(cls.gtv));
}
static void CL_GTV_Status_f(void)
{
if (!cls.gtv.state) {
Com_Printf("Client GTV not running.\n");
return;
}
if (cls.gtv.state == ca_disconnected) {
Com_Printf("Listening for GTV connections.\n");
return;
}
Com_Printf("TCP client [%s] connected (state %d)\n",
NET_AdrToString(&cls.gtv.stream.address), cls.gtv.state);
}
void CL_GTV_Init(void)
{
Cmd_AddCommand("client_gtv_start", CL_GTV_Start_f);
Cmd_AddCommand("client_gtv_stop", CL_GTV_Stop_f);
Cmd_AddCommand("client_gtv_status", CL_GTV_Status_f);
}
void CL_GTV_Shutdown(void)
{
if (cls.gtv.state)
CL_GTV_Stop_f();
}
|