| 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 |
#include "global.h" |
| 26 |
|
| 27 |
#include <arpa/inet.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
#ifndef WIN32 |
| 36 |
#include <sys/types.h> |
| 37 |
#include <sys/time.h> |
| 38 |
#include <netinet/in.h> |
| 39 |
#include <netdb.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "image.h" |
| 43 |
#include "newserver.h" |
| 44 |
#include "sockproto.h" |
| 45 |
#include "sproto.h" |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
typedef void (*func_uint8_int_ns)(char *, int, socket_struct *); |
| 62 |
|
| 63 |
|
| 64 |
struct client_cmd_mapping { |
| 65 |
const char *cmdname; |
| 66 |
const func_uint8_int_ns cmdproc; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
typedef void (*func_uint8_int_pl)(char *, int, player *); |
| 71 |
|
| 72 |
struct player_cmd_mapping { |
| 73 |
const char *cmdname; |
| 74 |
const func_uint8_int_pl cmdproc; |
| 75 |
const uint8_t flag; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static const struct player_cmd_mapping player_commands[] = { |
| 94 |
{ "examine", examine_cmd, 1 }, |
| 95 |
{ "apply", apply_cmd, 1 }, |
| 96 |
{ "move", move_cmd, 1 }, |
| 97 |
{ "reply", reply_cmd, 0 }, |
| 98 |
{ "ncom", (func_uint8_int_pl)new_player_cmd, 1 }, |
| 99 |
{ "lookat", look_at_cmd, 1 }, |
| 100 |
{ "lock", (func_uint8_int_pl)lock_item_cmd, 1 }, |
| 101 |
{ "mark", (func_uint8_int_pl)mark_item_cmd, 1 }, |
| 102 |
{ "inscribe", inscribe_scroll_cmd, 0 }, |
| 103 |
{ NULL, NULL, 0 } |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
static const struct client_cmd_mapping client_commands[] = { |
| 108 |
{ "addme", add_me_cmd }, |
| 109 |
{ "askface", send_face_cmd }, |
| 110 |
{ "beat", NULL }, |
| 111 |
{ "requestinfo", request_info_cmd }, |
| 112 |
{ "setup", set_up_cmd }, |
| 113 |
{ "version", version_cmd }, |
| 114 |
{ "asksmooth", ask_smooth_cmd }, |
| 115 |
{ "accountlogin", account_login_cmd }, |
| 116 |
{ "accountnew", account_new_cmd }, |
| 117 |
{ "accountaddplayer", account_add_player_cmd }, |
| 118 |
{ "accountplay", account_play_cmd }, |
| 119 |
{ "accountpw", account_password }, |
| 120 |
{ "createplayer", create_player_cmd }, |
| 121 |
{ NULL, NULL } |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
void request_info_cmd(char *buf, int len, socket_struct *ns) { |
| 133 |
char *params = NULL, *cp; |
| 134 |
|
| 135 |
SockList sl; |
| 136 |
|
| 137 |
if (len <= 0 || !buf) { |
| 138 |
LOG(llevDebug, "IP '%s' sent bogus request_info_cmd information\n", ns->host); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
SockList_Init(&sl); |
| 146 |
SockList_AddString(&sl, "replyinfo "); |
| 147 |
SockList_AddString(&sl, buf); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
for (cp = buf; *cp != '\0'; cp++) |
| 153 |
if (*cp == ' ') { |
| 154 |
*cp = '\0'; |
| 155 |
params = cp+1; |
| 156 |
break; |
| 157 |
} |
| 158 |
if (!strcmp(buf, "image_info")) |
| 159 |
send_image_info(ns); |
| 160 |
else if (!strcmp(buf, "image_sums")) |
| 161 |
send_image_sums(ns, params); |
| 162 |
else if (!strcmp(buf, "skill_info")) |
| 163 |
send_skill_info(ns, params); |
| 164 |
else if (!strcmp(buf, "spell_paths")) |
| 165 |
send_spell_paths(ns, params); |
| 166 |
else if (!strcmp(buf, "exp_table")) |
| 167 |
send_exp_table(ns, params); |
| 168 |
else if (!strcmp(buf, "race_list")) |
| 169 |
send_race_list(ns, params); |
| 170 |
else if (!strcmp(buf, "race_info")) |
| 171 |
send_race_info(ns, params); |
| 172 |
else if (!strcmp(buf, "class_list")) |
| 173 |
send_class_list(ns, params); |
| 174 |
else if (!strcmp(buf, "class_info")) |
| 175 |
send_class_info(ns, params); |
| 176 |
else if (!strcmp(buf, "rules")) |
| 177 |
send_file(ns, "rules"); |
| 178 |
else if (!strcmp(buf, "motd")) |
| 179 |
send_file(ns, "motd"); |
| 180 |
else if (!strcmp(buf, "news")) |
| 181 |
send_file(ns, "news"); |
| 182 |
else if (!strcmp(buf,"newcharinfo")) |
| 183 |
send_new_char_info(ns); |
| 184 |
else if (!strcmp(buf,"startingmap")) |
| 185 |
send_map_info(ns); |
| 186 |
else if (!strcmp(buf, "knowledge_info")) |
| 187 |
knowledge_send_info(ns); |
| 188 |
else |
| 189 |
Send_With_Handling(ns, &sl); |
| 190 |
SockList_Term(&sl); |
| 191 |
} |
| 192 |
|
| 193 |
static int |
| 194 |
handle_cmd(socket_struct *ns, player *pl, char *cmd, char *data, int len) { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if (cmd == NULL) { |
| 201 |
LOG(llevDebug, "%s: missing command. Sending garbage?\n", ns->host); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
for (int i = 0; client_commands[i].cmdname != NULL; i++) { |
| 205 |
if (strcmp(cmd, client_commands[i].cmdname) == 0) { |
| 206 |
if (client_commands[i].cmdproc != NULL) { |
| 207 |
client_commands[i].cmdproc(data, len, ns); |
| 208 |
} |
| 209 |
return 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if (pl) { |
| 220 |
for (int i = 0; player_commands[i].cmdname != NULL; i++) { |
| 221 |
if (strcmp(cmd, player_commands[i].cmdname) == 0) { |
| 222 |
if (pl->state == ST_PLAYING || player_commands[i].flag == 0) { |
| 223 |
player_commands[i].cmdproc(data, len, pl); |
| 224 |
} |
| 225 |
return 1; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
LOG(llevDebug, "%s: invalid command '%s'\n", ns->host, cmd); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
bool handle_client(socket_struct *ns, player *pl) { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
int command_count = 0; |
| 254 |
while (command_count < 5 || (pl && command_count < 25)) { |
| 255 |
if (pl && pl->state == ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) { |
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
int status = SockList_ReadPacket(ns->fd, &ns->inbuf, sizeof(ns->inbuf.buf)-1); |
| 261 |
if (status != 1) { |
| 262 |
if (status < 0) { |
| 263 |
ns->status = Ns_Dead; |
| 264 |
} |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
ns->last_tick = 0; |
| 270 |
|
| 271 |
SockList_NullTerminate(&ns->inbuf); |
| 272 |
assert(ns->inbuf.len >= 2); |
| 273 |
char *data; |
| 274 |
char *cmd = strtok_r((char *)ns->inbuf.buf + 2, " ", &data); |
| 275 |
|
| 276 |
int got_player_cmd; |
| 277 |
if (data != NULL) { |
| 278 |
int rem = ns->inbuf.len - ((unsigned char *)data - ns->inbuf.buf); |
| 279 |
got_player_cmd = handle_cmd(ns, pl, cmd, data, rem); |
| 280 |
} else { |
| 281 |
got_player_cmd = handle_cmd(ns, pl, cmd, NULL, 0); |
| 282 |
} |
| 283 |
|
| 284 |
SockList_ResetRead(&ns->inbuf); |
| 285 |
if (got_player_cmd) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
command_count += 1; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if (ns->faces_sent == NULL) { |
| 298 |
command_count = 6; |
| 299 |
} |
| 300 |
} |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
#ifdef WATCHDOG |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
void watchdog(void) { |
| 319 |
static int fd = -1; |
| 320 |
static struct sockaddr_in insock; |
| 321 |
|
| 322 |
if (fd == -1) { |
| 323 |
struct protoent *protoent; |
| 324 |
|
| 325 |
if ((protoent = getprotobyname("udp")) == NULL |
| 326 |
|| (fd = socket(PF_INET, SOCK_DGRAM, protoent->p_proto)) == -1) { |
| 327 |
return; |
| 328 |
} |
| 329 |
insock.sin_family = AF_INET; |
| 330 |
insock.sin_port = htons((unsigned short)13325); |
| 331 |
insock.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 332 |
} |
| 333 |
sendto(fd, (void *)&fd, 1, 0, (struct sockaddr *)&insock, sizeof(insock)); |
| 334 |
} |
| 335 |
#endif |
| 336 |
|
| 337 |
extern unsigned long todtick; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static void block_until_new_connection(void) { |
| 343 |
struct timeval Timeout; |
| 344 |
fd_set readfs; |
| 345 |
int cycles; |
| 346 |
int i; |
| 347 |
|
| 348 |
LOG(llevInfo, "Waiting for connections...\n"); |
| 349 |
|
| 350 |
cycles = 1; |
| 351 |
do { |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
cycles++; |
| 356 |
if (cycles%2 == 0) |
| 357 |
tick_the_clock(); |
| 358 |
|
| 359 |
FD_ZERO(&readfs); |
| 360 |
for (i = 0; i < socket_info.allocated_sockets && init_sockets[i].listen; i++) |
| 361 |
if (init_sockets[i].status == Ns_Add) |
| 362 |
FD_SET((uint32_t)init_sockets[i].fd, &readfs); |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if (settings.fastclock > 0) { |
| 370 |
#ifdef WATCHDOG |
| 371 |
if (cycles%120000 == 0) { |
| 372 |
watchdog(); |
| 373 |
flush_old_maps(); |
| 374 |
} |
| 375 |
#endif |
| 376 |
if (cycles == 720000) { |
| 377 |
metaserver_update(); |
| 378 |
cycles = 1; |
| 379 |
} |
| 380 |
Timeout.tv_sec = 0; |
| 381 |
Timeout.tv_usec = 50; |
| 382 |
} else { |
| 383 |
Timeout.tv_sec = 60; |
| 384 |
Timeout.tv_usec = 0; |
| 385 |
if (cycles == 7) { |
| 386 |
metaserver_update(); |
| 387 |
cycles = 1; |
| 388 |
} |
| 389 |
flush_old_maps(); |
| 390 |
} |
| 391 |
} while (select(socket_info.max_filedescriptor, &readfs, NULL, NULL, &Timeout) == 0); |
| 392 |
|
| 393 |
reset_sleep(); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static int is_fd_valid(int fd) { |
| 405 |
#ifndef WIN32 |
| 406 |
return fcntl(fd, F_GETFL) != -1 || errno != EBADF; |
| 407 |
#else |
| 408 |
return 1; |
| 409 |
#endif |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static void new_connection(int listen_fd) { |
| 417 |
int newsocknum = -1, j; |
| 418 |
#ifdef HAVE_GETNAMEINFO |
| 419 |
struct sockaddr_storage addr; |
| 420 |
#else |
| 421 |
struct sockaddr_in addr; |
| 422 |
#endif |
| 423 |
socklen_t addrlen = sizeof(addr); |
| 424 |
|
| 425 |
#ifdef ESRV_DEBUG |
| 426 |
LOG(llevDebug, "do_server: New Connection\n"); |
| 427 |
#endif |
| 428 |
|
| 429 |
for (j = 0; j < socket_info.allocated_sockets; j++) |
| 430 |
if (init_sockets[j].status == Ns_Avail) { |
| 431 |
newsocknum = j; |
| 432 |
break; |
| 433 |
} |
| 434 |
|
| 435 |
if (newsocknum == -1) { |
| 436 |
|
| 437 |
init_sockets = realloc(init_sockets, sizeof(socket_struct)*(socket_info.allocated_sockets+1)); |
| 438 |
if (!init_sockets) |
| 439 |
fatal(OUT_OF_MEMORY); |
| 440 |
newsocknum = socket_info.allocated_sockets; |
| 441 |
socket_info.allocated_sockets++; |
| 442 |
init_sockets[newsocknum].listen = NULL; |
| 443 |
init_sockets[newsocknum].faces_sent_len = get_faces_count(); |
| 444 |
init_sockets[newsocknum].faces_sent = calloc(get_faces_count(), sizeof(*init_sockets[newsocknum].faces_sent)); |
| 445 |
if (!init_sockets[newsocknum].faces_sent) |
| 446 |
fatal(OUT_OF_MEMORY); |
| 447 |
init_sockets[newsocknum].status = Ns_Avail; |
| 448 |
} |
| 449 |
|
| 450 |
if (newsocknum < 0) { |
| 451 |
LOG(llevError, "FATAL: didn't allocate a newsocket?? alloc = %d, newsocknum = %d", socket_info.allocated_sockets, newsocknum); |
| 452 |
fatal(SEE_LAST_ERROR); |
| 453 |
} |
| 454 |
|
| 455 |
init_sockets[newsocknum].fd = accept(listen_fd, (struct sockaddr *)&addr, &addrlen); |
| 456 |
if (init_sockets[newsocknum].fd == -1) { |
| 457 |
LOG(llevError, "accept failed: %s\n", strerror(errno)); |
| 458 |
} else { |
| 459 |
char buf[MAX_BUF]; |
| 460 |
#ifndef HAVE_GETNAMEINFO |
| 461 |
long ip; |
| 462 |
#endif |
| 463 |
socket_struct *ns; |
| 464 |
|
| 465 |
ns = &init_sockets[newsocknum]; |
| 466 |
|
| 467 |
#ifdef HAVE_GETNAMEINFO |
| 468 |
getnameinfo((struct sockaddr *) &addr, addrlen, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST); |
| 469 |
#else |
| 470 |
ip = ntohl(addr.sin_addr.s_addr); |
| 471 |
snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld", (ip>>24)&255, (ip>>16)&255, (ip>>8)&255, ip&255); |
| 472 |
#endif |
| 473 |
|
| 474 |
if (checkbanned(NULL, buf)) { |
| 475 |
LOG(llevInfo, "Banned host tried to connect: [%s]\n", buf); |
| 476 |
close(init_sockets[newsocknum].fd); |
| 477 |
init_sockets[newsocknum].fd = -1; |
| 478 |
} else { |
| 479 |
init_connection(ns, buf); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
bool connection_alive(socket_struct socket) { |
| 490 |
|
| 491 |
if (!socket.heartbeat) { |
| 492 |
return true; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
if (socket.last_tick < tick_length(BEAT_INTERVAL + 1)) { |
| 497 |
return true; |
| 498 |
} |
| 499 |
|
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
static void send_updates(player *pl) { |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
esrv_update_stats(pl); |
| 511 |
if (pl->last_weight != -1 && pl->last_weight != WEIGHT(pl->ob)) { |
| 512 |
esrv_update_item(UPD_WEIGHT, pl->ob, pl->ob); |
| 513 |
if (pl->last_weight != WEIGHT(pl->ob)) |
| 514 |
LOG(llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", (unsigned long)pl->last_weight, (unsigned long)WEIGHT(pl->ob)); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
draw_client_map(pl->ob); |
| 520 |
if (pl->socket.update_look) |
| 521 |
esrv_draw_look(pl->ob); |
| 522 |
if (pl->socket.update_inventory) { |
| 523 |
if (pl->ob->container != NULL) |
| 524 |
esrv_send_inventory(pl->ob, pl->ob->container); |
| 525 |
pl->socket.update_inventory = 0; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
void do_server(void) { |
| 537 |
fd_set tmp_read, tmp_exceptions; |
| 538 |
int active = 0; |
| 539 |
FD_ZERO(&tmp_read); |
| 540 |
FD_ZERO(&tmp_exceptions); |
| 541 |
|
| 542 |
for (int i = 0; i < socket_info.allocated_sockets; i++) { |
| 543 |
if (init_sockets[i].status == Ns_Add && !is_fd_valid(init_sockets[i].fd)) { |
| 544 |
LOG(llevError, "do_server: invalid waiting fd %d\n", i); |
| 545 |
init_sockets[i].status = Ns_Dead; |
| 546 |
} |
| 547 |
if (init_sockets[i].status == Ns_Dead) { |
| 548 |
LOG(llevInfo, "Disconnected from %s\n", init_sockets[i].host); |
| 549 |
if (init_sockets[i].listen) { |
| 550 |
|
| 551 |
init_listening_socket(&init_sockets[i]); |
| 552 |
} else { |
| 553 |
free_newsocket(&init_sockets[i]); |
| 554 |
init_sockets[i].status = Ns_Avail; |
| 555 |
} |
| 556 |
} else if (init_sockets[i].status != Ns_Avail) { |
| 557 |
FD_SET((uint32_t)init_sockets[i].fd, &tmp_read); |
| 558 |
FD_SET((uint32_t)init_sockets[i].fd, &tmp_exceptions); |
| 559 |
active++; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
player *pl, *next; |
| 567 |
for (pl = first_player; pl != NULL; ) { |
| 568 |
if (pl->socket.status != Ns_Dead && !is_fd_valid(pl->socket.fd)) { |
| 569 |
LOG(llevError, "do_server: invalid file descriptor for player %s [%s]: %d\n", (pl->ob && pl->ob->name) ? pl->ob->name : "(unnamed player?)", (pl->socket.host) ? pl->socket.host : "(unknown ip?)", pl->socket.fd); |
| 570 |
pl->socket.status = Ns_Dead; |
| 571 |
} |
| 572 |
|
| 573 |
if (pl->socket.status == Ns_Dead) { |
| 574 |
player *npl = pl->next; |
| 575 |
|
| 576 |
save_player(pl->ob, 0); |
| 577 |
leave(pl, 1); |
| 578 |
final_free_player(pl); |
| 579 |
pl = npl; |
| 580 |
} else { |
| 581 |
FD_SET((uint32_t)pl->socket.fd, &tmp_read); |
| 582 |
FD_SET((uint32_t)pl->socket.fd, &tmp_exceptions); |
| 583 |
pl = pl->next; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
if (active == 1 && first_player == NULL) |
| 588 |
block_until_new_connection(); |
| 589 |
|
| 590 |
long sleep_time = get_sleep_remaining(); |
| 591 |
if (sleep_time < 0) { |
| 592 |
LOG(llevInfo, "skipping time (over by %ld ms)\n", -sleep_time/1000); |
| 593 |
jump_time(); |
| 594 |
} |
| 595 |
|
| 596 |
while (sleep_time > 0) { |
| 597 |
socket_info.timeout.tv_sec = 0; |
| 598 |
socket_info.timeout.tv_usec = sleep_time; |
| 599 |
int pollret = select(socket_info.max_filedescriptor, &tmp_read, NULL, |
| 600 |
&tmp_exceptions, &socket_info.timeout); |
| 601 |
if (pollret == -1) { |
| 602 |
if (errno != EINTR) { |
| 603 |
LOG(llevError, "select failed: %s\n", strerror(errno)); |
| 604 |
} |
| 605 |
return; |
| 606 |
} else if (!pollret) { |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
for (int i = 0; i < socket_info.allocated_sockets; i++) { |
| 612 |
|
| 613 |
if (init_sockets[i].status != Ns_Add) { |
| 614 |
continue; |
| 615 |
} |
| 616 |
if (FD_ISSET(init_sockets[i].fd, &tmp_exceptions)) { |
| 617 |
free_newsocket(&init_sockets[i]); |
| 618 |
init_sockets[i].status = Ns_Avail; |
| 619 |
continue; |
| 620 |
} |
| 621 |
if (FD_ISSET(init_sockets[i].fd, &tmp_read)) { |
| 622 |
if (init_sockets[i].listen) |
| 623 |
new_connection(init_sockets[i].fd); |
| 624 |
else |
| 625 |
handle_client(&init_sockets[i], NULL); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
for (pl = first_player; pl != NULL; pl = next) { |
| 631 |
next = pl->next; |
| 632 |
if (pl->socket.status == Ns_Dead) |
| 633 |
continue; |
| 634 |
|
| 635 |
if (FD_ISSET(pl->socket.fd, &tmp_exceptions)) { |
| 636 |
save_player(pl->ob, 0); |
| 637 |
leave(pl, 1); |
| 638 |
final_free_player(pl); |
| 639 |
} else { |
| 640 |
bool keep_processing = handle_client(&pl->socket, pl); |
| 641 |
if (!keep_processing) { |
| 642 |
FD_CLR(pl->socket.fd, &tmp_read); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
next = pl->next; |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if (pl->socket.status == Ns_Dead) { |
| 662 |
save_player(pl->ob, 0); |
| 663 |
leave(pl, 1); |
| 664 |
final_free_player(pl); |
| 665 |
} else { |
| 666 |
send_updates(pl); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
sleep_time = get_sleep_remaining(); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
void update_players() { |
| 678 |
player *pl, *next; |
| 679 |
for (pl = first_player; pl != NULL; pl = next) { |
| 680 |
next = pl->next; |
| 681 |
send_updates(pl); |
| 682 |
|
| 683 |
if (pl->state == ST_PLAYING) { |
| 684 |
pl->socket.last_tick++; |
| 685 |
|
| 686 |
if (!connection_alive(pl->socket)) { |
| 687 |
|
| 688 |
LOG(llevDebug, "Lost client connection!\n"); |
| 689 |
} |
| 690 |
|
| 691 |
if (pl->socket.tick) |
| 692 |
send_tick(pl); |
| 693 |
} |
| 694 |
} |
| 695 |
} |