Cube  Diff

Differences From Artifact [2b7d5c9bfe]:

To Artifact [8f47418a23]:


16
17
18
19
20
21
22
23

24

25
26
27
28
29
30
31
16
17
18
19
20
21
22

23

24
25
26
27
28
29
30
31







-
+
-
+







};

vector<client> clients;

int maxclients = 8;
static OFString *smapname;

struct server_entity // server side version of "entity" type
// server side version of "entity" type
{
struct server_entity {
	bool spawned;
	int spawnsecs;
};

vector<server_entity> sents;

bool notgotitems =
64
65
66
67
68
69
70
71

72
73
74
75
76

77
78
79
80
81
82
83
64
65
66
67
68
69
70

71
72
73
74
75

76
77
78
79
80
81
82
83







-
+




-
+







	if (!packet)
		return;
	switch (clients[n].type) {
	case ST_TCPIP: {
		enet_peer_send(clients[n].peer, 0, packet);
		bsend += packet->dataLength;
		break;
	};
	}

	case ST_LOCAL:
		localservertoclient(packet->data, packet->dataLength);
		break;
	};
	}
}

void
send2(bool rel, int cn, int a, int b)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, 32, rel ? ENET_PACKET_FLAG_RELIABLE : 0);
133
134
135
136
137
138
139
140

141
142
143
144
145
146
147
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147







-
+







{
	if (i >= (uint)sents.length())
		return;
	if (sents[i].spawned) {
		sents[i].spawned = false;
		sents[i].spawnsecs = sec;
		send2(true, sender, SV_ITEMACC, i);
	};
	}
}

void
resetvotes()
{
	loopv(clients) clients[i].mapvote[0] = 0;
}
186
187
188
189
190
191
192
193

194
195
196
197
198
199
200
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200







-
+







void
process(ENetPacket *packet, int sender) // sender may be -1
{
	if (ENET_NET_TO_HOST_16(*(ushort *)packet->data) !=
	    packet->dataLength) {
		disconnect_client(sender, "packet length");
		return;
	};
	}

	uchar *end = packet->data + packet->dataLength;
	uchar *p = packet->data + 2;
	char text[MAXTRANS];
	int cn = -1, type;

	while (p < end)
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
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







-
+



-
+




















-
+







			break;
		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(p)) != -1)
				if (notgotitems) {
					server_entity se = {false, 0};
					server_entity se = { false, 0 };
					while (sents.length() <= n)
						sents.add(se);
					sents[n].spawned = true;
				};
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {
			int n = getint(p);
			pickup(n, getint(p), sender);
			break;
		}

		case SV_PING:
			send2(false, cn, SV_PONG, getint(p));
			break;

		case SV_POS: {
			cn = getint(p);
			if (cn < 0 || cn >= clients.length() ||
			    clients[cn].type == ST_EMPTY) {
				disconnect_client(sender, "client num");
				return;
			};
			}
			int size = msgsizelookup(type);
			assert(size != -1);
			loopi(size - 2) getint(p);
			break;
		}

		case SV_SENDMAP: {
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
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







-
+






-
+

-
-
+
+




-
+








		case SV_EXT: // allows for new features that require no server
		             // updates
		{
			for (int n = getint(p); n; n--)
				getint(p);
			break;
		};
		}

		default: {
			int size = msgsizelookup(type);
			if (size == -1) {
				disconnect_client(sender, "tag type");
				return;
			};
			}
			loopi(size - 1) getint(p);
		};
		};
		}
		}

	if (p > end) {
		disconnect_client(sender, "end of packet");
		return;
	};
	}
	multicast(packet, sender);
}

void
send_welcome(int n)
{
	ENetPacket *packet =
361
362
363
364
365
366
367
368

369
370
371
372
373
374
375
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375







-
+








void
checkintermission()
{
	if (!minremain) {
		interm = lastsec + 10;
		mapend = lastsec + 1000;
	};
	}
	send2(true, -1, SV_TIMEUP, minremain--);
}

void
startintermission()
{
	minremain = 0;
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
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







-
+
















-
+







	loopv(sents) // spawn entities when timer reached
	{
		if (sents[i].spawnsecs &&
		    (sents[i].spawnsecs -= seconds - lastsec) <= 0) {
			sents[i].spawnsecs = 0;
			sents[i].spawned = true;
			send2(true, -1, SV_ITEMSPAWN, i);
		};
		}
	}

	lastsec = seconds;

	if ((mode > 1 || (mode == 0 && nonlocalclients)) &&
	    seconds > mapend - minremain * 60)
		checkintermission();
	if (interm && seconds > interm) {
		interm = 0;
		loopv(clients) if (clients[i].type != ST_EMPTY)
		{
			send2(true, i, SV_MAPRELOAD,
			    0); // ask a client to trigger map reload
			mapreload = true;
			break;
		}
	};
	}

	resetserverifempty();

	if (!isdedicated)
		return; // below is network only

	int numplayers = 0;
444
445
446
447
448
449
450
451

452
453
454
455
456
457
458
444
445
446
447
448
449
450

451
452
453
454
455
456
457
458







-
+







		laststatus = seconds;
		if (nonlocalclients || bsend || brec)
			printf("status: %d remote clients, %.1f send, %.1f rec "
			       "(K/sec)\n",
			    nonlocalclients, bsend / 60.0f / 1024,
			    brec / 60.0f / 1024);
		bsend = brec = 0;
	};
	}

	ENetEvent event;
	if (enet_host_service(serverhost, &event, timeout) > 0) {
		switch (event.type) {
		case ENET_EVENT_TYPE_CONNECT: {
			client &c = addclient();
			c.type = ST_TCPIP;
480
481
482
483
484
485
486
487
488
489



490
491
492

493
494
495
496
497
498
499
480
481
482
483
484
485
486



487
488
489
490


491
492
493
494
495
496
497
498







-
-
-
+
+
+

-
-
+







				break;
			printf("disconnected client (%s)\n",
			    clients[(intptr_t)event.peer->data].hostname);
			clients[(intptr_t)event.peer->data].type = ST_EMPTY;
			send2(true, -1, SV_CDIS, (intptr_t)event.peer->data);
			event.peer->data = (void *)-1;
			break;
		};

		if (numplayers > maxclients) {
		}

		if (numplayers > maxclients)
			disconnect_client(lastconnect, "maxclients reached");
		};
	};
	}
#ifndef _WIN32
	fflush(stdout);
#endif
}

void
cleanupserver()
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


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







-
+













-
+
-
+









-
-
+
+
{
	serverpassword = passwd;
	maxclients = maxcl;
	servermsinit(master ? master : @"wouter.fov120.com/cube/masterserver/",
	    sdesc, dedicated);

	if (isdedicated = dedicated) {
		ENetAddress address = {ENET_HOST_ANY, CUBE_SERVER_PORT};
		ENetAddress address = { ENET_HOST_ANY, CUBE_SERVER_PORT };
		@autoreleasepool {
			if (ip.length > 0 &&
			    enet_address_set_host(&address, ip.UTF8String) < 0)
				printf("WARNING: server ip not resolved");
		}
		serverhost = enet_host_create(&address, MAXCLIENTS, 0, uprate);
		if (!serverhost)
			fatal(@"could not create server host\n");
		loopi(MAXCLIENTS) serverhost->peers[i].data = (void *)-1;
	}

	resetserverifempty();

	if (isdedicated) // do not return, this becomes main loop
	// do not return, this becomes main loop
	{
	if (isdedicated) {
#ifdef _WIN32
		SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
#endif
		printf("dedicated server started, waiting for "
		       "clients...\nCtrl-C to exit\n\n");
		atexit(cleanupserver);
		atexit(enet_deinitialize);
		for (;;)
			serverslice(/*enet_time_get_sec()*/ time(NULL), 5);
	};
}
	}
}