--- deliantra/server/socket/loop.C 2007/07/26 21:44:43 1.62 +++ deliantra/server/socket/loop.C 2007/07/28 00:15:03 1.63 @@ -178,74 +178,96 @@ rate_avail = max_rate - outputbuffer_len (); } - else if (outq < 4096) // only send when bandwidth allows it. the 4k is completely arbitrary :( + else { - int ol = outputbuffer_len (); + bool overload = false; - 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-sized packets if possible - // but never 768 bytes more. - if (fxix) avail += min (768, mss - (ol % mss)); - - bg_scrub = BG_SCRUB_RATE; - - while (avail > 0) +#if HAVE_TCP_INFO + // do bandwidth checking + if (tcpi.tcpi_unacked + 1 > max (4, tcpi.tcpi_snd_cwnd)) { - ixsend &ix = ixface.back (); - - if (facedata *d = face_data (ix.idx, faceset)) + overload = true; + if (next_rate_adjust <= NOW) { - if (fxix) - { - // only transfer something if the amount of data transferred - // has a healthy relation to the header overhead - if (avail <= 40) - break; + 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)); + } + } +#endif - // 9 bytes is enough for fx_FFFOOO - int chunk = min (min (avail, MAXSOCKBUF - 9), (int)ix.ofs); + if (!overload) + { + int ol = outputbuffer_len (); - if (chunk <= 0) - break; + rate_avail = min (max_rate, rate_avail + max_rate); + rate_avail -= ol; - ix.ofs -= chunk; + int avail = rate_avail; - //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D + // 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)); - packet sl ("ix"); + bg_scrub = BG_SCRUB_RATE; - sl << ber32 (ix.idx) - << ber32 (ix.ofs) - << data (d->data.data () + ix.ofs, chunk); + while (avail > 0) + { + ixsend &ix = ixface.back (); - send_packet (sl); - } - else + if (facedata *d = face_data (ix.idx, faceset)) { - send_image (ix.idx); - ix.ofs = 0; + 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; + } } - } - else - 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; + } } } }