--- deliantra/server/socket/loop.C 2007/03/14 15:44:47 1.41 +++ deliantra/server/socket/loop.C 2007/05/12 22:04:20 1.47 @@ -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 @@ -89,7 +90,7 @@ bool skipping = false; while (!cmd_queue.empty () - && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left <= 0)) + && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left <= 0.f)) { command &cmd = cmd_queue.front (); @@ -140,42 +141,120 @@ //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); + + 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)); + } + + draw_client_map (pl->ob); + + if (update_look) + esrv_draw_look (pl->ob); - draw_client_map (pl->ob); + if (askface.empty () && !partial_face_ofs) + { + // 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); + flush_fx (); + + 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 (s->update_look) - esrv_draw_look (pl->ob); + // if we can split images, transfer up to mss packets if possible + // but never more 768 bytes more. + if (fxix) avail += min (768, mss - (ol % mss)); - while (!ns->askface.empty () && ns->outputbuffer_len () < ns->max_rate) + bg_scrub = BG_SCRUB_RATE; + + while (avail > 0) + { + if (partial_face_ofs) + { + if (facedata *d = face_data (partial_face, faceset)) + { + // 9 bytes is enough for fx_FFFOOO, 40 leaves some room + int chunk = min (min (avail - 40, MAXSOCKBUF - 9), partial_face_ofs); + + if (chunk <= 0) + break; + + partial_face_ofs -= chunk; + + packet sl ("ix"); + + sl << ber32 (partial_face) + << ber32 (partial_face_ofs) + << data (d->data.data () + partial_face_ofs, chunk); + + send_packet (sl); + } + else + partial_face_ofs = 0; + } + else if (!askface.empty ()) { // use a lifo to send most recently requested images - faceidx face = ns->askface.back (); - ns->askface.pop_back (); + faceidx facenum = askface.back (); + askface.pop_back (); - ns->send_image (face); + if (fxix) + { + if (facedata *d = face_data (facenum, faceset)) + { + partial_face = facenum; + partial_face_ofs = d->data.size (); + } + } + else + send_image (facenum); } - } + else + break; - s->refcnt_chk (); + int consumed = ol - outputbuffer_len (); + avail -= consumed; + rate_avail -= consumed; + } } }