-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.c
569 lines (485 loc) · 16.3 KB
/
net.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
/***************************************************************
* net.c ####### *
* chaos-eng(c) int13h 2005-2010 #.....# *
* #..@..# *
* Network routines #.....# *
* ####### *
* *
***************************************************************/
/*
############
#..Globals.#
############
*/
#include "globals.h"
#include "structs.h"
#include "main.h"
#include "log.h"
#include "chaos.h"
#include "generate.h"
#include "input_common.h"
#include "chat.h"
#include "net.h"
#include "net_client.h"
#include "net_server.h"
extern int frontend_mode;
extern bool forceupdate;
extern char infobar_text[255];
extern bool beepmsg;
extern int logging_level;
extern char log_message[LOGMSGLEN];
extern world myworld;
extern spells myspells[MAX_SPELLS];
extern player myplayers[MAX_PLAYERS];
extern monster mymonsters[MAX_MONSTERS];
extern tree mytrees[MAX_TREES];
extern wall mywalls[MAX_WALLS];
extern blob myblobs[MAX_BLOBS];
extern magic_special mymagic_special[MAX_MAGIC_SPECIAL];
extern magic_upgrade mymagic_upgrade[MAX_MAGIC_UPGRADE];
extern magic_balance mymagic_balance[MAX_MAGIC_BALANCE];
extern magic_spell_attrib mymagic_spell_attrib[MAX_MAGIC_SPELL_ATTRIB];
extern magic_ranged mymagic_ranged[MAX_MAGIC_RANGED];
extern int mypid;
bool net_enable = false;
bool net_dedicated = false;
bool net_connected = false;
bool net_wait = false;
int net_port = NET_DEFAULT_PORT;
extern int fdmax, listener;
extern int server_socket;
/*
========================================================== Internal Functions ==
*/
/*
######################
#..net_sockettopid().#
######################
*/
int net_sockettopid (int socket)
{
int i;
for(i=0;i<myworld.players;i++)
if(myplayers[i].socket == socket)
return myplayers[i].id;
return -1;
}
/*
#################
#..net_gencrc().#
#################
*/
char* net_gencrc (char data[NET_MAX_PACKET_SIZE])
{
/* CRC should include a Version Number
so client is in sync with server
for enum'd data types? */
return "CRCLOLZ";
}
/*
###################
#..net_checkcrc().#
###################
*/
static bool net_checkcrc (char data[NET_MAX_PACKET_SIZE])
{
return true;
}
/*
######################
#..valid_ipaddress().#
######################
*/
bool valid_ipaddress (char address[255])
{
char validchars[11] = "1234567890";
int i,j;
int sepcount=0;
bool valid;
if(strlen(address) < 7 || strlen(address) > 15){
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Address too short/long [%d]",(int)strlen(address)));
return false;
}
for(i=0;i<(int)strlen(address);i++){
valid = false;
for(j=0;j<(int)strlen(validchars);j++){
if(address[i] == '.'){
valid = true;
sepcount++;
break;
}
if(address[i] == validchars[j]){
valid = true;
break;
}
}
if(!valid){
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"invalid char [%c]",address[i]));
return false;
}
}
if(sepcount !=3){
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"invalid amount of seperators [%d]",sepcount));
return false;
}
return true;
}
/*
###############
#..net_send().#
###############
*/
void net_send (int socket, char data[NET_MAX_PACKET_SIZE])
{
char senddata[NET_MAX_PACKET_SIZE+1];
int ret,left;
char *data_sent;
/* Send All 'socket'? */
if(socket == CE_NET_SOCKET_ALL){
net_send_all(data);
return;
}
if((int)strlen(data)>NET_MAX_PACKET_SIZE){
console_log(__FILE__,__func__,__LINE__,LOG_WARNING, sprintf(log_message,
"Net - Trying to send too much? : Len %d",(int)strlen(data)));
return;
}
memset(senddata,'\0',sizeof(senddata));
sprintf(senddata,"%s%s",data,NET_END_PACKET_SEP);
data_sent = (char *)malloc((int)strlen(senddata)+1);
if(data_sent == NULL){
console_log(__FILE__,__func__,__LINE__,LOG_ERROR, sprintf(log_message,
"Net - Failed to Alloc memory for net_send()"));
shutdown_chaos();
return;
}
left = (int)strlen(senddata);
strcpy(data_sent,senddata);
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Net - Sending to %d : %s (Len %d)",socket,senddata,(int)strlen(senddata)));
while(left > 0){
if((ret = send(socket, data_sent, left, 0)) == -1){
console_log(__FILE__,__func__,__LINE__,LOG_WARNING, sprintf(log_message,
"Net - Send Error : %d (Left to Send: %d)",ret,left));
return;
}
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Net - Sent %d of %d",ret,left));
left -= ret;
data_sent += ret;
}
}
/*
###################
#..net_send_all().#
###################
*/
void net_send_all (char data[NET_MAX_PACKET_SIZE])
{
int i;
for(i=0;i<myworld.players;i++)
if(myplayers[i].socket > 0)
net_send(myplayers[i].socket,data);
}
/*
######################
#..net_get_request().#
######################
*/
ce_net_request net_get_request (char data[NET_MAX_PACKET_SIZE])
{
ce_net_request request;
char *field = NULL;
int i = 0;
/* Defaults */
request.type = CE_NET_INVALID;
request.len = 0;
memset(request.data,'\0',sizeof(request.data));
memset(request.crc,'\0',sizeof(request.crc));
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Net - Split Packet Data : %s (Len %d)",data,(int)strlen(data)));
field = strtok(data,NET_PACKET_SEP);
while(field != NULL){
switch(i){
case 0:
request.type = atoi(field);
break;
case 1:
request.len = atoi(field);
if(request.len > (strlen(data) - 4)){
request.type = CE_NET_INVALID;
goto BADPACKET;
}
break;
case 2:
if(strlen(field)!=request.len){
request.type = CE_NET_INVALID;
goto BADPACKET;
}
memset(request.data, '\0', sizeof(request.data));
sprintf(request.data,"%s",field);
break;
case 3:
strcpy(request.crc,field);
break;
}
i++;
field = strtok(NULL,NET_PACKET_SEP);
}
if(!net_checkcrc(request.crc))
request.type = CE_NET_BADCRC;
/* Dirty Data Escape */
BADPACKET:
return request;
}
/*
######################
#..net_unserialise().#
######################
WARNING : NEVER call this from within a strtok() loop.
*/
ce_net_serial_data net_unserialise (char datastr[NET_MAX_PACKET_SIZE], char sep[2])
{
char *data = datastr;
char *newdata = NULL;
int i=0;
ce_net_serial_data returndata;
console_log(__FILE__,__func__,__LINE__,LOG_DEBUG, sprintf(log_message,
"Net - Serialised Data : %s",datastr));
newdata = strtok(data,sep);
while(newdata != NULL){
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Net - Seperated Data : %s",newdata));
/* Data too long to handle? bin it all. */
if((int)strlen(newdata) > NET_MAX_SERIAL_CHUNK){
returndata.len = 0;
return returndata;
}
memset(returndata.data[i],'\0',sizeof(returndata.data[i]));
sprintf(returndata.data[i],"%s",newdata);
i++;
newdata = strtok(NULL,sep);
}
returndata.len = i;
return returndata;
}
/*
============================================================ Return Functions ==
*/
/*
##################
#..net_invalid().#
##################
*/
void net_invalid (int socket)
{
char packet[NET_MAX_PACKET_SIZE],data[NET_MAX_PACKET_SIZE];
memset(data,'\0',sizeof(data));
memset(packet,'\0',sizeof(packet));
sprintf(data,"Invalid Request");
sprintf(packet,"%d|%d|%s|%s",CE_NET_INVALID,(int)strlen(data),data,net_gencrc(data));
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Net - %s (Packed Data Len %d)",packet,(int)strlen(data)));
net_send(socket, packet);
}
/*
================================================================== Game Loops ==
*/
/*
##########################
#..gameloop_net_server().#
##########################
*/
void gameloop_net_server(void)
{
switch(myworld.mode){
/* Setup Mode */
case CE_WORLD_MODE_SETUP:
switch(myworld.submode){
/* Setup Players */
case 1:
case 3:
if(allplayersready()){
generate_players();
empty_buffer();
myworld.submode=2;
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = false;
setplayersready(false);
} else {
/* Keep Connected Players Informed */
if(!net_wait){
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = true;
}
}
break;
default:
break;
}
break;
/* Spell Selection */
case CE_WORLD_MODE_SELECTSPELLS:
if(allplayersready()){
myworld.mode = CE_WORLD_MODE_CASTING;
myworld.submode = 0;
myworld.current_player = 1;
skipdeadplayers();
myworld.cursor[0] = myspells[myworld.current_player+9].current_pos[0];
myworld.cursor[1] = myspells[myworld.current_player+9].current_pos[1];
net_gamestate(CE_NET_SOCKET_ALL);
net_spelldata(CE_NET_SOCKET_ALL,SPELL_PLAYER);
net_wait = false;
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE,sprintf(log_message,
"Going to Casting Round"));
}
if(!net_wait)
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = true;
break;
/* Casting Round */
case CE_WORLD_MODE_CASTING:
/* Skip players with no spells to cast */
if(myplayers[myworld.current_player-1].selected_spell == 0){
myworld.current_player++;
skipdeadplayers();
if(myworld.current_player > myworld.players) {
myworld.mode = CE_WORLD_MODE_MOVE;
myworld.submode = 0;
myworld.current_player = 1;
skipdeadplayers();
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE,
sprintf(log_message,"Going to Movement Mode"));
}
myworld.cursor[0] = myspells[myworld.current_player+9].current_pos[0];
myworld.cursor[1] = myspells[myworld.current_player+9].current_pos[1];
/* Keep Connected Players Informed */
if(!net_wait){
net_arenalayout(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_spelllist(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_spelldata(CE_NET_SOCKET_ALL,SPELL_PLAYER);
net_wait = true;
}
}
break;
/* Movement Round */
case CE_WORLD_MODE_MOVE:
if(myworld.current_player > myworld.players) {
myworld.mode = CE_WORLD_MODE_POSTROUND;
myworld.submode = 0;
myworld.current_player = 1;
myworld.cursor[0] = myspells[myworld.current_player+9].current_pos[0];
myworld.cursor[1] = myspells[myworld.current_player+9].current_pos[1];
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE,sprintf(log_message,
"Going to Post Round"));
net_spelllist(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = true;
} else {
/* Keep Connected Players Informed */
if(!net_wait){
net_arenalayout(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = true;
}
}
break;
/* Post/Grow Round */
case CE_WORLD_MODE_POSTROUND:
if(!myworld.beenreset){
reset_world();
myworld.beenreset = true;
net_beepmsg(CE_NET_SOCKET_ALL);
net_arenalayout(CE_NET_SOCKET_ALL);
net_spelllist(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_spelldata(CE_NET_SOCKET_ALL,SPELL_PLAYER);
}
if(!beepmsg){
myworld.beenreset = false;
myworld.mode = CE_WORLD_MODE_SELECTSPELLS;
net_spelllist(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_spelldata(CE_NET_SOCKET_ALL,SPELL_PLAYER);
net_wait = true;
console_log(__FILE__,__func__,__LINE__,LOG_NOTICE, sprintf(log_message,
"Going to Spell Selection Round"));
}
break;
/* Game Over */
case CE_WORLD_MODE_ENDGAME:
if(!net_wait){
net_spelllist(CE_NET_SOCKET_ALL);
net_gamestate(CE_NET_SOCKET_ALL);
net_playerstate(CE_NET_SOCKET_ALL);
net_wait = true;
}
break;
default:
break;
}
}
/*
##########################
#..gameloop_net_client().#
##########################
*/
void gameloop_net_client(void)
{
/* Force dead players to view arena mode */
if(net_connected && isspelldead(myplayers[mypid].id+10) &&
myworld.mode != CE_WORLD_MODE_ENDGAME){
if(myworld.mode == CE_WORLD_MODE_SELECTSPELLS)
sprintf(infobar_text,"Players are selecting their next spells");
myworld.mode = CE_WORLD_MODE_SELECTSPELLS;
if(myworld.submode != 2) /* View Spell Details */
myworld.submode = 5;
}
switch(myworld.mode){
/* Setup Mode */
case CE_WORLD_MODE_SETUP:
switch(myworld.submode){
/* Server chosing arena size */
case 2:
net_wait = true;
break;
}
break;
/* Spell Selection */
case CE_WORLD_MODE_SELECTSPELLS:
if(myplayers[mypid].ready && !isspelldead(myplayers[mypid].id+10))
net_wait=true;
break;
/* Casting Round */
case CE_WORLD_MODE_CASTING:
if((myworld.current_player-1) != mypid && !beepmsg)
sprintf(infobar_text,"%s's turn",myplayers[myworld.current_player-1].name);
break;
/* Movement Round */
case CE_WORLD_MODE_MOVE:
if((myworld.current_player-1) != mypid)
sprintf(infobar_text,"%s's turn",myplayers[myworld.current_player-1].name);
break;
/* Post/Grow Round */
case CE_WORLD_MODE_POSTROUND:
break;
/* Game Over */
case CE_WORLD_MODE_ENDGAME:
forceupdate = true;
net_wait = false;
break;
default:
break;
}
}