--- deliantra/server/socket/loop.C 2006/12/16 03:08:26 1.20 +++ deliantra/server/socket/loop.C 2006/12/21 23:37:06 1.31 @@ -28,8 +28,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 +48,17 @@ #include #define MAX_QUEUE_DEPTH 500 //TODO +#define MAX_QUEUE_BACKLOG 5. //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 +66,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; + bool skipping = false; - if (cmd_queue.empty ()) - return 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 (); } /** @@ -125,7 +145,7 @@ player *npl = pl->next; //TODO: must be handled cleanly elsewhere - if (!pl->socket || pl->socket->status == Ns_Dead) + if (!pl->ns || pl->ns->destroyed ()) { save_player (pl->ob, 0); @@ -146,7 +166,7 @@ { client *s = *i; - if (s->status == Ns_Dead) + if (s->destroyed ()) { clients.erase (i); delete s; @@ -163,11 +183,8 @@ client *s = *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 (!s->destroyed () && pl) { /* Update the players stats once per tick. More efficient than * sending them whenever they change, and probably just as useful