ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
(Generate patch)

Comparing deliantra/server/socket/loop.C (file contents):
Revision 1.11 by root, Thu Dec 14 00:01:37 2006 UTC vs.
Revision 1.14 by root, Thu Dec 14 04:30:33 2006 UTC

65 * before player joins, and those happen after the player has joined. 65 * before player joins, and those happen after the player has joined.
66 * As such, we have function types that might be called, so 66 * As such, we have function types that might be called, so
67 * we end up having 2 tables. 67 * we end up having 2 tables.
68 */ 68 */
69 69
70typedef void (*func_uint8_int_ns) (char *, int, NewSocket *); 70typedef void (*func_uint8_int_ns) (char *, int, client_socket *);
71 71
72struct NsCmdMapping 72struct NsCmdMapping
73{ 73{
74 const char *cmdname; 74 const char *cmdname;
75 func_uint8_int_ns cmdproc; 75 func_uint8_int_ns cmdproc;
132 * RequestInfo is sort of a meta command. There is some specific 132 * RequestInfo is sort of a meta command. There is some specific
133 * request of information, but we call other functions to provide 133 * request of information, but we call other functions to provide
134 * that information. 134 * that information.
135 */ 135 */
136void 136void
137RequestInfo (char *buf, int len, NewSocket * ns) 137RequestInfo (char *buf, int len, client_socket * ns)
138{ 138{
139 char *params = NULL, *cp; 139 char *params = NULL, *cp;
140 140
141 /* No match */ 141 /* No match */
142 char bigbuf[MAX_BUF]; 142 char bigbuf[MAX_BUF];
167 else if (!strcmp (buf, "skill_info")) 167 else if (!strcmp (buf, "skill_info"))
168 send_skill_info (ns, params); 168 send_skill_info (ns, params);
169 else if (!strcmp (buf, "spell_paths")) 169 else if (!strcmp (buf, "spell_paths"))
170 send_spell_paths (ns, params); 170 send_spell_paths (ns, params);
171 else 171 else
172 Write_String_To_Socket (ns, bigbuf, len); 172 ns->send_packet (bigbuf, len);
173} 173}
174 174
175/** 175/**
176 * Handle client input. 176 * Handle client input.
177 * 177 *
181 * with this socket, null if no player (one of the init_sockets for just 181 * with this socket, null if no player (one of the init_sockets for just
182 * starting a connection) 182 * starting a connection)
183 */ 183 */
184 184
185void 185void
186HandleClient (NewSocket *ns, player *pl) 186HandleClient (client_socket *ns, player *pl)
187{ 187{
188 /* Loop through this - maybe we have several complete packets here. */ 188 /* Loop through this - maybe we have several complete packets here. */
189 // limit to a few commands only, though, as to not monopolise the server 189 // limit to a few commands only, though, as to not monopolise the server
190 for (int repeat = 16; repeat--;) 190 for (int repeat = 16; repeat--;)
191 { 191 {
264 264
265 return; 265 return;
266 } 266 }
267 } 267 }
268 268
269 printf ("BP34n");//D
270 /* If we get here, we didn't find a valid command. Logging 269 /* If we get here, we didn't find a valid command. Logging
271 * this might be questionable, because a broken client/malicious 270 * this might be questionable, because a broken client/malicious
272 * user could certainly send a whole bunch of invalid commands. 271 * user could certainly send a whole bunch of invalid commands.
273 */ 272 */
274 LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); 273 LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2);
321flush_sockets (void) 320flush_sockets (void)
322{ 321{
323 player *pl; 322 player *pl;
324 323
325 for (pl = first_player; pl != NULL; pl = pl->next) 324 for (pl = first_player; pl != NULL; pl = pl->next)
326 if (pl->socket.status != Ns_Dead) 325 if (pl->socket->status != Ns_Dead)
327 Socket_Flush (&pl->socket); 326 Socket_Flush (pl->socket);
328} 327}
329 328
330/** 329/**
331 * This checks the sockets for input, does the right thing. 330 * This checks the sockets for input, does the right thing.
332 * 331 *
340 int i, pollret; 339 int i, pollret;
341 fd_set tmp_read, tmp_write; 340 fd_set tmp_read, tmp_write;
342 struct sockaddr_in addr; 341 struct sockaddr_in addr;
343 socklen_t addrlen = sizeof (struct sockaddr); 342 socklen_t addrlen = sizeof (struct sockaddr);
344 player *pl, *next; 343 player *pl, *next;
344 int maxfd = 0;
345 345
346#ifdef CS_LOGSTATS 346#ifdef CS_LOGSTATS
347 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) 347 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME)
348 write_cs_stats (); 348 write_cs_stats ();
349#endif 349#endif
350 350
351 FD_ZERO (&tmp_read);
352 FD_ZERO (&tmp_write);
353
354 for (i = 0; i < socket_info.allocated_sockets; i++)
355 {
356 if (init_sockets[i].status == Ns_Dead)
357 {
358 free_newsocket (&init_sockets[i]);
359 init_sockets[i].status = Ns_Avail;
360 socket_info.nconns--;
361 }
362 else if (init_sockets[i].status != Ns_Avail)
363 {
364 FD_SET (init_sockets[i].fd, &tmp_read);
365 FD_SET (init_sockets[i].fd, &tmp_write);
366 }
367 }
368
369 /* Go through the players. Let the loop set the next pl value, 351 /* Go through the players. Let the loop set the next pl value,
370 * since we may remove some 352 * since we may remove some
371 */ 353 */
354 //TODO: must be handled cleanly elsewhere
372 for (pl = first_player; pl != NULL;) 355 for (pl = first_player; pl; )
373 { 356 {
357 player *npl = pl->next;
358
359 //TODO: must be handled cleanly elsewhere
374 if (pl->socket.status == Ns_Dead) 360 if (pl->socket->status == Ns_Dead)
375 { 361 {
376 player *npl = pl->next;
377
378 save_player (pl->ob, 0); 362 save_player (pl->ob, 0);
379 363
380 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) 364 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
381 { 365 {
382 terminate_all_pets (pl->ob); 366 terminate_all_pets (pl->ob);
383 pl->ob->remove (); 367 pl->ob->remove ();
384 } 368 }
385 369
386 leave (pl, 1); 370 leave (pl, 1);
387 final_free_player (pl); 371 final_free_player (pl);
372 }
373
388 pl = npl; 374 pl = npl;
375 }
376
377 FD_ZERO (&tmp_read);
378 FD_ZERO (&tmp_write);
379
380 for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); )
381 {
382 client_socket *s = *i;
383
384 if (s->status == Ns_Dead)
385 {
386 client_sockets.erase (i);
387 delete s;
389 } 388 }
390 else 389 else
391 { 390 {
391 if (s->fd > maxfd) maxfd = s->fd;
392
392 FD_SET ((uint32) pl->socket.fd, &tmp_read); 393 FD_SET (s->fd, &tmp_read);
394
395 if (s->outputbuffer.len)
393 FD_SET ((uint32) pl->socket.fd, &tmp_write); 396 FD_SET (s->fd, &tmp_write);
394 pl = pl->next;
395 }
396 }
397 397
398 /* Reset timeout each time, since some OS's will change the values on 398 ++i;
399 * the return from select. 399 }
400 */ 400 }
401
402 struct timeval timeout;
403
401 socket_info.timeout.tv_sec = 0; 404 timeout.tv_sec = 0;
402 socket_info.timeout.tv_usec = 0; 405 timeout.tv_usec = 0;
403 406
404 pollret = select (socket_info.max_filedescriptor, 407 pollret = select (maxfd + 1,
405 &tmp_read, &tmp_write, 0, 408 &tmp_read, &tmp_write, 0,
406 &socket_info.timeout); 409 &timeout);
407 410
408 if (pollret == -1) 411 if (pollret == -1)
409 { 412 {
410 LOG (llevError, "select failed: %s\n", strerror (errno)); 413 LOG (llevError, "select failed: %s\n", strerror (errno));
411 return; 414 return;
412 } 415 }
413 416
414 /* We need to do some of the processing below regardless */ 417 /* We need to do some of the processing below regardless */
415 418
416/* if (!pollret) return;*/
417
418 /* Following adds a new connection */
419 if (pollret && FD_ISSET (init_sockets[0].fd, &tmp_read))
420 {
421 int newsocknum = 0;
422
423#ifdef ESRV_DEBUG
424 LOG (llevDebug, "doeric_server: New Connection\n");
425#endif
426 /* If this is the case, all sockets currently in used */
427 if (socket_info.allocated_sockets <= socket_info.nconns)
428 {
429 init_sockets = (NewSocket *) realloc (init_sockets, sizeof (NewSocket) * (socket_info.nconns + 1));
430 if (!init_sockets)
431 fatal (OUT_OF_MEMORY);
432 newsocknum = socket_info.allocated_sockets;
433 socket_info.allocated_sockets++;
434 init_sockets[newsocknum].faces_sent_len = nrofpixmaps;
435 init_sockets[newsocknum].faces_sent = (uint8 *) malloc (nrofpixmaps * sizeof (*init_sockets[newsocknum].faces_sent));
436 if (!init_sockets[newsocknum].faces_sent)
437 fatal (OUT_OF_MEMORY);
438 init_sockets[newsocknum].status = Ns_Avail;
439 }
440 else
441 {
442 int j;
443
444 for (j = 1; j < socket_info.allocated_sockets; j++)
445 if (init_sockets[j].status == Ns_Avail)
446 {
447 newsocknum = j;
448 break;
449 }
450 }
451
452 init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen);
453
454 if (init_sockets[newsocknum].fd == -1)
455 LOG (llevError, "accept failed: %s\n", strerror (errno));
456 else
457 {
458 char buf[MAX_BUF];
459 long ip;
460 NewSocket *ns;
461
462 ns = &init_sockets[newsocknum];
463
464 ip = ntohl (addr.sin_addr.s_addr);
465 sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
466
467 if (checkbanned (NULL, buf))
468 {
469 LOG (llevInfo, "Banned host tried to connect: [%s]\n", buf);
470 close (init_sockets[newsocknum].fd);
471 init_sockets[newsocknum].fd = -1;
472 }
473 else
474 {
475 InitConnection (ns, buf);
476 socket_info.nconns++;
477 }
478 }
479 }
480
481 /* Check for any input on the sockets */ 419 /* Check for any input on the sockets */
482 if (pollret) 420 for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i)
483 for (i = 1; i < socket_info.allocated_sockets; i++) 421 {
422 client_socket *s = *i;
423 player *pl = s->pl;
424
425 //TODO: writing should be independent of tick
426 if (FD_ISSET (s->fd, &tmp_write))
484 { 427 {
485 if (init_sockets[i].status == Ns_Avail) 428 s->can_write = 1;
486 continue; 429 write_socket_buffer (s);
430 }
487 431
488 //TODO: disassociate handleclient from socket readin 432 //TODO: disassociate handleclient from socket readin
489 if (init_sockets[i].inbuf_len || FD_ISSET (init_sockets[i].fd, &tmp_read)) 433 if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read))
490 HandleClient (&init_sockets[i], NULL);
491
492 if (FD_ISSET (init_sockets[i].fd, &tmp_write))
493 init_sockets[i].can_write = 1;
494 }
495
496 /* This does roughly the same thing, but for the players now */
497 for (pl = first_player; pl != NULL; pl = next)
498 {
499
500 next = pl->next;
501 if (pl->socket.status == Ns_Dead)
502 continue;
503
504 if (FD_ISSET (pl->socket.fd, &tmp_write))
505 {
506 if (!pl->socket.can_write)
507 {
508#if 0
509 LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name);
510#endif
511 pl->socket.can_write = 1;
512 write_socket_buffer (&pl->socket);
513 }
514
515 /* if we get an error on the write_socket buffer, no reason to
516 * continue on this socket.
517 */
518 if (pl->socket.status == Ns_Dead)
519 continue;
520 }
521 else
522 pl->socket.can_write = 0;
523
524 HandleClient (&pl->socket, pl); 434 HandleClient (s, pl);
525 /* If the player has left the game, then the socket status 435
526 * will be set to this be the leave function. We don't 436 //TODO: should not be done here, either
527 * need to call leave again, as it has already been called
528 * once.
529 */
530 if (pl->socket.status == Ns_Dead) 437 if (s->status != Ns_Dead && pl)
531 {
532 save_player (pl->ob, 0);
533
534 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
535 {
536 terminate_all_pets (pl->ob);
537 pl->ob->remove ();
538 }
539
540 leave (pl, 1);
541 final_free_player (pl);
542 }
543 else
544 { 438 {
545 /* Update the players stats once per tick. More efficient than 439 /* Update the players stats once per tick. More efficient than
546 * sending them whenever they change, and probably just as useful 440 * sending them whenever they change, and probably just as useful
547 */ 441 */
548 esrv_update_stats (pl); 442 esrv_update_stats (pl);
556 450
557 /* draw_client_map does sanity checking that map is 451 /* draw_client_map does sanity checking that map is
558 * valid, so don't do it here. 452 * valid, so don't do it here.
559 */ 453 */
560 draw_client_map (pl->ob); 454 draw_client_map (pl->ob);
561 if (pl->socket.update_look) 455 if (s->update_look)
562 esrv_draw_look (pl->ob); 456 esrv_draw_look (pl->ob);
563 } 457 }
564 } 458 }
565} 459}
566 460

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines