--- deliantra/server/socket/loop.C 2006/12/16 03:08:26 1.20 +++ deliantra/server/socket/loop.C 2007/01/06 14:42:31 1.36 @@ -1,6 +1,7 @@ /* CrossFire, A Multiplayer game for X-windows + Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team Copyright (C) 1992 Frank Tore Johansen @@ -28,8 +29,7 @@ * \date 2003-12-02 * * loop.c mainly deals with initialization and higher level socket - * maintenance (checking for lost connections and if data has arrived.) - * The reading of data is handled in ericserver.c + * maintanance (checking for lost connections and if data has arrived.) */ @@ -49,9 +49,17 @@ #include #define MAX_QUEUE_DEPTH 500 //TODO +#define MAX_QUEUE_BACKLOG 3. //TODO -//TODO: cannot use this, as it might ignore vital commands such as fire_stop -#define MAX_QUEUE_BACKLOG 1e99 //TODO +void +client::reset_state () +{ + if (!pl) + return; + + pl->run_on = 0; + pl->fire_on = 0; +} void client::queue_command (packet_type *handler, char *data, int datalen) @@ -59,46 +67,59 @@ tstamp stamp = now (); if (cmd_queue.size () >= MAX_QUEUE_DEPTH) - //TODO: just disconnect here? - send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); - else if (!cmd_queue.empty () && cmd_queue.front ().stamp + MAX_QUEUE_BACKLOG < stamp) - //TODO: do fire_stop and other things that might be necessary - send_packet_printf ("drawinfo %d too many commands in queue, ignoring.", NDI_RED); + { + //TODO: just disconnect here? + reset_state (); + send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); + } else - cmd_queue.push_back (command ( - stamp, handler, (char *)salloc (datalen, data), datalen - )); + { + cmd_queue.push_back (command ()); + command &cmd = cmd_queue.back (); + cmd.stamp = stamp; + cmd.handler = handler; + cmd.data = salloc (datalen + 1, data); + cmd.datalen = datalen; + } } bool client::handle_command () { - if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) - return false; - - if (cmd_queue.empty ()) - return false; + bool skipping = false; - command &cmd = cmd_queue.front (); + while (!cmd_queue.empty () + && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) + { + command &cmd = cmd_queue.front (); - if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) - //TODO: do fire_stop and other things that might be necessary - send_packet_printf ("drawinfo %d ignoring delayed command.", NDI_RED); - else - execute (cmd.handler, cmd.data, cmd.datalen); + if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) + { + if (!skipping) + { + skipping = true; + reset_state (); + send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); + } - sfree (cmd.data, cmd.datalen); - cmd_queue.pop_front (); + cmd_queue.pop_front (); + } + else + { + execute (cmd.handler, cmd.data, cmd.datalen); + cmd_queue.pop_front (); + return true; + } + } - return true; + return false; } void flush_sockets (void) { for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) - if ((*i)->status != Ns_Dead) - (*i)->flush (); + (*i)->flush (); } /** @@ -116,58 +137,13 @@ write_cs_stats (); #endif - /* Go through the players. Let the loop set the next pl value, - * since we may remove some - */ - //TODO: must be handled cleanly elsewhere - for (player *pl = first_player; pl; ) + //TODO: should not be done here, either + for (int i = 0; i < clients.size (); ++i) { - player *npl = pl->next; - - //TODO: must be handled cleanly elsewhere - if (!pl->socket || pl->socket->status == Ns_Dead) - { - save_player (pl->ob, 0); - - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - pl->ob->remove (); - } - - leave (pl, 1); - final_free_player (pl); - } - - pl = npl; - } - - for (sockvec::iterator i = clients.begin (); i != clients.end (); ) - { - client *s = *i; - - if (s->status == Ns_Dead) - { - clients.erase (i); - delete s; - } - else - ++i; - } - - /* We need to do some of the processing below regardless */ - - /* Check for any input on the sockets */ - for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) - { - client *s = *i; + client *s = clients [i]; player *pl = s->pl; - s->handle_packet (); - s->handle_command (); - - //TODO: should not be done here, either - if (s->status != Ns_Dead && pl) + if (pl && pl->ns && !pl->ns->destroyed ()) { /* Update the players stats once per tick. More efficient than * sending them whenever they change, and probably just as useful @@ -187,6 +163,8 @@ if (s->update_look) esrv_draw_look (pl->ob); } + + s->refcnt_chk (); } }