--- deliantra/server/socket/loop.C 2007/07/28 00:45:05 1.64 +++ deliantra/server/socket/loop.C 2007/07/29 02:24:34 1.65 @@ -48,7 +48,7 @@ #define BG_SCRUB_RATE 4 // how often to send a face in the background -#define MAX_QUEUE_DEPTH 500 +#define MAX_QUEUE_DEPTH 50 #define MAX_QUEUE_BACKLOG 3. void @@ -158,118 +158,102 @@ if (update_look) esrv_draw_look (pl); - bool overload = false; - -#if HAVE_TCP_INFO - // do bandwidth checking - if (tcpi.tcpi_unacked + 1 > max (4, tcpi.tcpi_snd_cwnd)) + if (ixface.empty ()) { - overload = true; - if (next_rate_adjust <= NOW) - { - next_rate_adjust = NOW + 2; - max_rate = max (max_rate >> 1, int (5000 * TICK)); - LOG (llevDebug, "tcp %s overloaded (%d + 1 > 4 | %d), rate now %d\n", - host, (int)tcpi.tcpi_unacked, (int)tcpi.tcpi_snd_cwnd, int (max_rate / TICK));//D - send_packet_printf ("drawinfo %d Due to possible network congestion, your output-rate has been reduced to %dbps.", - NDI_RED, int (max_rate / TICK)); - } + // 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]) + if (faceinfo *f = face_info (scrub_idx)) + if (f->type == FT_FACE) // only scrub faces for now + { + send_face (scrub_idx, -120); + 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 + mss, rate_avail + max_rate); + + int avail = rate_avail; + + // if we can split images, round to next-lowest mss + if (fxix) avail -= avail % mss; + + rate_avail -= ol; + avail -= ol; + +#if HAVE_TCP_INFO + // further restrict the available bandwidth by the excess bandwidth available + avail = max (0, min (avail, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss)); #endif - if (!overload) - { - if (ixface.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]) - if (faceinfo *f = face_info (scrub_idx)) - if (f->type == FT_FACE) // only scrub faces for now - { - send_face (scrub_idx, -120); - flush_fx (); - - bg_scrub = 1; // send up to one face per tick, unless an image was requested - break; - } - } + bg_scrub = BG_SCRUB_RATE; - rate_avail = max_rate - outputbuffer_len (); - } - else + while (avail > 0) { - int ol = outputbuffer_len (); + ixsend &ix = ixface.back (); - rate_avail = min (max_rate, rate_avail + max_rate); - rate_avail -= ol; + if (facedata *d = face_data (ix.idx, faceset)) + { + if (fxix) + { + // estimate the packet header overhead "ix " + idx + (new)ofs + int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); + int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; + + // only transfer something if the amount of data transferred + // has a healthy relation to the header overhead + if (chunk < 64) + break; - int avail = rate_avail; + chunk = min (chunk, (int)ix.ofs); - // if we can split images, transfer up to mss-sized packets if possible - // but never 768 bytes more. - if (fxix) avail += min (768, mss - (ol % mss)); + ix.ofs -= chunk; - bg_scrub = BG_SCRUB_RATE; + //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D - while (avail > 0) - { - ixsend &ix = ixface.back (); + packet sl ("ix"); - if (facedata *d = face_data (ix.idx, faceset)) - { - if (fxix) - { - // only transfer something if the amount of data transferred - // has a healthy relation to the header overhead - if (avail <= 40) - break; - - // 9 bytes is enough for fx_FFFOOO - int chunk = min (min (avail, MAXSOCKBUF - 9), (int)ix.ofs); - - if (chunk <= 0) - break; - - ix.ofs -= chunk; - - //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D - - packet sl ("ix"); - - sl << ber32 (ix.idx) - << ber32 (ix.ofs) - << data (d->data.data () + ix.ofs, chunk); - - send_packet (sl); - } - else - { - send_image (ix.idx); - ix.ofs = 0; - } + sl << ber32 (ix.idx) + << ber32 (ix.ofs) + << data (d->data.data () + ix.ofs, chunk); + + send_packet (sl); } else - ix.ofs = 0; + { + send_image (ix.idx); + ix.ofs = 0; + } + } + else + ix.ofs = 0; - int consumed = outputbuffer_len () - ol; + int consumed = outputbuffer_len () - ol; - avail -= consumed; - rate_avail -= consumed; + avail -= consumed; + rate_avail -= consumed; - ol = outputbuffer_len (); + ol = outputbuffer_len (); - if (!ix.ofs) - { - ixface.pop_back (); + if (!ix.ofs) + { + ixface.pop_back (); - if (ixface.empty ()) - break; - } + if (ixface.empty ()) + break; } } }