… | |
… | |
70 | package conn; |
70 | package conn; |
71 | |
71 | |
72 | use Socket; |
72 | use Socket; |
73 | use HTTP::Date; |
73 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
|
|
75 | use Linux::AIO; |
|
|
76 | |
|
|
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
|
|
78 | |
|
|
79 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
80 | poll => 'r', |
|
|
81 | async => 1, |
|
|
82 | cb => \&Linux::AIO::poll_cb ); |
|
|
83 | |
|
|
84 | Event->add_hooks(prepare => sub { |
|
|
85 | &Coro::cede while &Coro::nready; |
|
|
86 | 1e6; |
|
|
87 | }); |
75 | |
88 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
89 | our %conn; # $conn{ip}{fh} => connobj |
77 | our %blocked; |
90 | our %blocked; |
78 | our %mimetype; |
91 | our %mimetype; |
79 | |
92 | |
… | |
… | |
427 | $hdr->{"Content-Length"} = $length; |
440 | $hdr->{"Content-Length"} = $length; |
428 | |
441 | |
429 | $self->response(@code, $hdr, ""); |
442 | $self->response(@code, $hdr, ""); |
430 | |
443 | |
431 | if ($self->{method} eq "GET") { |
444 | if ($self->{method} eq "GET") { |
432 | my ($fh, $buf); |
445 | my ($fh, $buf, $r); |
|
|
446 | my $current = $Coro::current; |
433 | open $fh, "<", $self->{path} |
447 | open $fh, "<", $self->{path} |
434 | or die "$self->{path}: late open failure ($!)"; |
448 | or die "$self->{path}: late open failure ($!)"; |
435 | |
449 | |
436 | if ($l) { |
|
|
437 | sysseek $fh, $l, 0 |
|
|
438 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
439 | } |
|
|
440 | |
|
|
441 | $h -= $l - 1; |
450 | $h -= $l - 1; |
442 | |
451 | |
443 | while ($h > 0) { |
452 | while ($h > 0) { |
444 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
453 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
454 | $buf, 0, sub { |
|
|
455 | $r = $_[0]; |
|
|
456 | $current->ready; |
|
|
457 | }); |
|
|
458 | &Coro::schedule; |
|
|
459 | last unless $r; |
445 | my $w = $self->{fh}->syswrite($buf) |
460 | my $w = $self->{fh}->syswrite($buf) |
446 | or last; |
461 | or last; |
447 | $::written += $w; |
462 | $::written += $w; |
448 | $self->{written} += $w; |
463 | $self->{written} += $w; |
|
|
464 | $l += $r; |
449 | } |
465 | } |
450 | } |
466 | } |
451 | |
467 | |
452 | close $fh; |
468 | close $fh; |
453 | } |
469 | } |