--- deliantra/server/socket/loop.C 2007/03/14 15:44:47 1.41 +++ deliantra/server/socket/loop.C 2007/04/02 19:56:11 1.44 @@ -32,6 +32,7 @@ * maintanance (checking for lost connections and if data has arrived.) */ +#define BG_SCRUB_RATE 4 // how often to send a face in the background #include #include @@ -140,42 +141,85 @@ //TODO: should not be done here, either for (unsigned i = 0; i < clients.size (); ++i) { - client *s = clients [i]; - player *pl = s->pl; + client *ns = clients [i]; - if (pl && pl->ns && !pl->ns->destroyed ()) - { - client *ns = pl->ns; + ns->tick (); + ns->refcnt_chk (); + } +} - /* Update the players stats once per tick. More efficient than - * sending them whenever they change, and probably just as useful - */ - esrv_update_stats (pl); +void +client::tick () +{ + if (!pl || destroyed ()) + return; - if (ns->last_weight != -1 && ns->last_weight != WEIGHT (pl->ob)) - { - esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); - if (ns->last_weight != WEIGHT (pl->ob)) - LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", - (unsigned long) ns->last_weight, WEIGHT (pl->ob)); - } + /* Update the players stats once per tick. More efficient than + * sending them whenever they change, and probably just as useful + */ + esrv_update_stats (pl); - draw_client_map (pl->ob); + if (last_weight != -1 && last_weight != WEIGHT (pl->ob)) + { + esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); + if (last_weight != WEIGHT (pl->ob)) + LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", + (unsigned long) last_weight, WEIGHT (pl->ob)); + } - if (s->update_look) - esrv_draw_look (pl->ob); + draw_client_map (pl->ob); - while (!ns->askface.empty () && ns->outputbuffer_len () < ns->max_rate) - { - // use a lifo to send most recently requested images - faceidx face = ns->askface.back (); - ns->askface.pop_back (); + if (update_look) + esrv_draw_look (pl->ob); - ns->send_image (face); - } + if (askface.empty ()) + { + // regularly send a new face when queue is empty + if (bg_scrub && !--bg_scrub && enable_bg_scrub) + while (scrub_idx < faces.size () - 1) + { + ++scrub_idx; + if (!faces_sent [scrub_idx]) + { + send_face (scrub_idx); + bg_scrub = 1; // send up to one face per tick, unless an image was requested + break; + } + } + + rate_avail = max_rate - outputbuffer_len (); + } + else + { + int ol = outputbuffer_len (); + + rate_avail = min (max_rate, rate_avail + max_rate); + rate_avail -= ol; + + int avail = rate_avail; + + // if we can split images, transfer up to mss packets if possible + if (fxix) avail += mss - (ol % mss); + + fprintf (stderr, "ra %6d a %6d ", rate_avail, avail); + + while (!askface.empty () && avail > 0) + { + // use a lifo to send most recently requested images + faceidx face = askface.back (); + askface.pop_back (); + + send_image (face); + + bg_scrub = BG_SCRUB_RATE; + + int consumed = ol - outputbuffer_len (); + avail -= consumed; + rate_avail -= consumed; } - s->refcnt_chk (); + fprintf (stderr, "ra %6d a %6d\n", rate_avail, avail); + } }