- my $n = 65536; # try to negotiate a big TCP window, first
- my ($next, $fh);
- my $cb = sub {
- $n = $len if $len < $n;
- my $r = sysread($in, my $buf, $n);
- if (!defined $r) {
- err($env, "$f read error: $!");
- drop_client($env);
- } elsif ($r <= 0) {
- err($env, "$f EOF with $len bytes left");
- drop_client($env);
- } else {
- $len -= $r;
- $fh->write($buf);
- if ($len == 0) {
- $fh->close;
- } elsif ($next) {
- # avoid recursion in Danga::Socket::write
- unless ($nextq) {
- $nextq = [];
- Danga::Socket->AddTimer(0, *do_next);
- }
- # avoid buffering too much in case we have
- # slow clients:
- $n = 8192;
- push @$nextq, $next;
- return;
- }
- }
- # all done, cleanup references:
- $fh = $next = undef;
- };
-