Overview
Comment: | More string migrations |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
d0e460fce1af321d6d9198053a792128 |
User & Date: | js on 2025-03-15 12:05:41 |
Other Links: | manifest | tags |
Context
2025-03-15
| ||
23:05 | Fix accidentally dropped if statement check-in: a6b2705084 user: js tags: trunk | |
12:05 | More string migrations check-in: d0e460fce1 user: js tags: trunk | |
00:24 | More string migrations check-in: 5659be76db user: js tags: trunk | |
Changes
Modified src/server.mm from [8f47418a23] to [f2cdc4d59c].
1 2 3 4 5 6 7 | // server.cpp: little more than enhanced multicaster // runs dedicated or as client coroutine #include "cube.h" enum { ST_EMPTY, ST_LOCAL, ST_TCPIP }; | | < > | | | | | | > | > > | < | > | 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 | // server.cpp: little more than enhanced multicaster // runs dedicated or as client coroutine #include "cube.h" enum { ST_EMPTY, ST_LOCAL, ST_TCPIP }; // server side version of "dynent" type @interface Client: OFObject @property (nonatomic) int type; @property (nonatomic) ENetPeer *peer; @property (copy, nonatomic) OFString *hostname; @property (copy, nonatomic) OFString *mapvote; @property (copy, nonatomic) OFString *name; @property (nonatomic) int modevote; @end @implementation Client @end static OFMutableArray<Client *> *clients; int maxclients = 8; static OFString *smapname; // server side version of "entity" type struct server_entity { bool spawned; int spawnsecs; }; vector<server_entity> sents; // true when map has changed and waiting for clients to send item bool notgotitems = true; int mode = 0; void restoreserverstate( vector<entity> &ents) // hack: called from savegame code, only works in SP { loopv(sents) |
︙ | ︙ | |||
52 53 54 55 56 57 58 | ENetHost *serverhost = NULL; int bsend = 0, brec = 0, laststatus = 0, lastsec = 0; #define MAXOBUF 100000 void process(ENetPacket *packet, int sender); void multicast(ENetPacket *packet, int sender); | | > | < < | 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 | ENetHost *serverhost = NULL; int bsend = 0, brec = 0, laststatus = 0, lastsec = 0; #define MAXOBUF 100000 void process(ENetPacket *packet, int sender); void multicast(ENetPacket *packet, int sender); void disconnect_client(int n, OFString *reason); void send(int n, ENetPacket *packet) { 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 |
︙ | ︙ | |||
108 109 110 111 112 113 114 | enet_packet_resize(packet, p - start); multicast(packet, -1); if (packet->referenceCount == 0) enet_packet_destroy(packet); } void | | > | | 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | enet_packet_resize(packet, p - start); multicast(packet, -1); if (packet->referenceCount == 0) enet_packet_destroy(packet); } void disconnect_client(int n, OFString *reason) { [OFStdOut writeFormat:@"disconnecting client (%@) [%@]\n", clients[n].hostname, reason]; enet_peer_disconnect(clients[n].peer); clients[n].type = ST_EMPTY; send2(true, -1, SV_CDIS, n); } void resetitems() |
︙ | ︙ | |||
139 140 141 142 143 144 145 | send2(true, sender, SV_ITEMACC, i); } } void resetvotes() { | > | | | > > | < | | | | | | | | | > > > | > > | > | > > | | | > | 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 | send2(true, sender, SV_ITEMACC, i); } } void resetvotes() { for (Client *client in clients) client.mapvote = @""; } bool vote(OFString *map, int reqmode, int sender) { clients[sender].mapvote = map; clients[sender].modevote = reqmode; int yes = 0, no = 0; for (Client *client in clients) { if (client.type != ST_EMPTY) { if (client.mapvote.length > 0) { if ([client.mapvote isEqual:map] && client.modevote == reqmode) yes++; else no++; } else no++; } } if (yes == 1 && no == 0) return true; // single player @autoreleasepool { OFString *msg = [OFString stringWithFormat: @"%@ suggests %@ on map %@ (set map to vote)", clients[sender].name, modestr(reqmode), map]; sendservmsg(msg); } if (yes / (float)(yes + no) <= 0.5f) return false; sendservmsg(@"vote passed"); resetvotes(); return true; } // server side processing of updates: does very little and most state is tracked // client only could be extended to move more gameplay to server (at expense of // lag) 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) switch (type = getint(p)) { case SV_TEXT: sgetstr(); break; case SV_INITC2S: sgetstr(); @autoreleasepool { clients[cn].name = @(text); } sgetstr(); getint(p); break; case SV_MAPCHANGE: { sgetstr(); int reqmode = getint(p); if (reqmode < 0) reqmode = 0; @autoreleasepool { if (smapname.length > 0 && !mapreload && !vote(@(text), reqmode, sender)) return; } mapreload = false; mode = reqmode; minremain = mode & 1 ? 15 : 10; mapend = lastsec + minremain * 60; interm = 0; @autoreleasepool { smapname = @(text); |
︙ | ︙ | |||
252 253 254 255 256 257 258 | case SV_PING: send2(false, cn, SV_PONG, getint(p)); break; case SV_POS: { cn = getint(p); | | | | 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | case SV_PING: send2(false, cn, SV_PONG, getint(p)); break; case SV_POS: { cn = getint(p); if (cn < 0 || cn >= clients.count || clients[cn].type == ST_EMPTY) { disconnect_client(sender, @"client num"); return; } int size = msgsizelookup(type); assert(size != -1); loopi(size - 2) getint(p); break; } |
︙ | ︙ | |||
287 288 289 290 291 292 293 | getint(p); break; } default: { int size = msgsizelookup(type); if (size == -1) { | | | | | < < | < | > | | > > > > > > > > > | 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 | 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 = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); uchar *start = packet->data; uchar *p = start + 2; putint(p, SV_INITS2C); putint(p, n); putint(p, PROTOCOL_VERSION); @autoreleasepool { putint(p, *smapname.UTF8String); } sendstring(serverpassword, p); putint(p, clients.count > maxclients); if (smapname.length > 0) { putint(p, SV_MAPCHANGE); sendstring(smapname, p); putint(p, mode); putint(p, SV_ITEMLIST); loopv(sents) if (sents[i].spawned) putint(p, i); putint(p, -1); } *(ushort *)start = ENET_HOST_TO_NET_16(p - start); enet_packet_resize(packet, p - start); send(n, packet); } void multicast(ENetPacket *packet, int sender) { size_t count = clients.count; for (size_t i = 0; i < count; i++) send(i, packet); } void localclienttoserver(ENetPacket *packet) { process(packet, 0); if (!packet->referenceCount) enet_packet_destroy(packet); } Client * addclient() { for (Client *client in clients) if (client.type == ST_EMPTY) return client; Client *client = [[Client alloc] init]; if (clients == nil) clients = [[OFMutableArray alloc] init]; [clients addObject:client]; return client; } void checkintermission() { if (!minremain) { interm = lastsec + 10; |
︙ | ︙ | |||
375 376 377 378 379 380 381 | minremain = 0; checkintermission(); } void resetserverifempty() { | > | > | > | 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | minremain = 0; checkintermission(); } void resetserverifempty() { for (Client *client in clients) if (client.type != ST_EMPTY) return; [clients removeAllObjects]; smapname = @""; resetvotes(); resetitems(); mode = 0; mapreload = false; minremain = 10; mapend = lastsec + minremain * 60; |
︙ | ︙ | |||
412 413 414 415 416 417 418 | lastsec = seconds; if ((mode > 1 || (mode == 0 && nonlocalclients)) && seconds > mapend - minremain * 60) checkintermission(); if (interm && seconds > interm) { interm = 0; | > > | < < | > | | > > > > | > > | < | < > > | | > | | > | | | | | > > | | | | | | > | | | | | | | 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 | lastsec = seconds; if ((mode > 1 || (mode == 0 && nonlocalclients)) && seconds > mapend - minremain * 60) checkintermission(); if (interm && seconds > interm) { interm = 0; size_t i = 0; for (Client *client in clients) { if (client.type != ST_EMPTY) { // ask a client to trigger map reload send2(true, i, SV_MAPRELOAD, 0); mapreload = true; break; } i++; } } resetserverifempty(); if (!isdedicated) return; // below is network only int numplayers = 0; for (Client *client in clients) if (client.type != ST_EMPTY) numplayers++; serverms(mode, numplayers, minremain, smapname, seconds, clients.count >= maxclients); // display bandwidth stats, useful for server ops if (seconds - laststatus > 60) { nonlocalclients = 0; for (Client *client in clients) if (client.type == ST_TCPIP) nonlocalclients++; 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; c.peer = event.peer; c.peer->data = (void *)(clients.count - 1); char hn[1024]; @autoreleasepool { c.hostname = (enet_address_get_host( &c.peer->address, hn, sizeof(hn)) == 0 ? @(hn) : @"localhost"); } [OFStdOut writeFormat:@"client connected (%@)\n", c.hostname]; send_welcome(lastconnect = clients.count - 1); break; } case ENET_EVENT_TYPE_RECEIVE: brec += event.packet->dataLength; process(event.packet, (intptr_t)event.peer->data); if (event.packet->referenceCount == 0) enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: if ((intptr_t)event.peer->data < 0) break; [OFStdOut writeFormat:@"disconnected client (%@)\n", clients[(size_t)event.peer->data].hostname]; clients[(size_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) disconnect_client(lastconnect, @"maxclients reached"); } #ifndef _WIN32 fflush(stdout); #endif } void cleanupserver() { if (serverhost) enet_host_destroy(serverhost); } void localdisconnect() { for (Client *client in clients) if (client.type == ST_LOCAL) client.type = ST_EMPTY; } void localconnect() { Client *c = addclient(); c.type = ST_LOCAL; c.hostname = @"local"; send_welcome(clients.count - 1); } void initserver(bool dedicated, int uprate, OFString *sdesc, OFString *ip, OFString *master, OFString *passwd, int maxcl) { 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 }; @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); |
︙ | ︙ |