1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
|
|
3 | use Coro::SemaphoreSet; |
3 | use Coro::Event; |
4 | use Coro::EV; |
4 | use Coro::Socket; |
5 | use Coro::Socket; |
5 | use Coro::Signal; |
6 | use Coro::Signal; |
6 | use Coro::AIO (); |
7 | use Coro::AIO (); |
7 | |
8 | |
|
|
9 | use Fcntl; |
8 | use HTTP::Date; |
10 | use HTTP::Date; |
9 | use POSIX (); |
11 | use POSIX (); |
10 | |
12 | |
11 | use Compress::Zlib (); |
13 | use Compress::Zlib (); |
12 | |
14 | |
13 | no utf8; |
15 | use common::sense; |
14 | use bytes; |
|
|
15 | |
16 | |
16 | # at least on my machine, this thingy serves files |
17 | # at least on my machine, this thingy serves files |
17 | # quite a bit faster than apache, ;) |
18 | # quite a bit faster than apache, ;) |
18 | # and quite a bit slower than thttpd :( |
19 | # and quite a bit slower than thttpd :( |
19 | |
20 | |
20 | $SIG{PIPE} = 'IGNORE'; |
21 | $SIG{PIPE} = 'IGNORE'; |
21 | |
22 | |
22 | our $accesslog; |
23 | our $accesslog; |
23 | our $errorlog; |
24 | our $errorlog; |
|
|
25 | our @listen_sockets; |
24 | |
26 | |
25 | our $NOW; |
27 | our $NOW; |
26 | our $HTTP_NOW; |
28 | our $HTTP_NOW; |
27 | |
29 | |
28 | our $ERROR_LOG; |
30 | our $ERROR_LOG; |
29 | our $ACCESS_LOG; |
31 | our $ACCESS_LOG; |
|
|
32 | our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip |
30 | |
33 | |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
34 | our $update_time = EV::periodic 0, 1, undef, sub { |
32 | $NOW = time; |
35 | $NOW = time; |
33 | $HTTP_NOW = time2str $NOW; |
36 | $HTTP_NOW = time2str $NOW; |
34 | })->now; |
37 | }; |
|
|
38 | $update_time->invoke; |
35 | |
39 | |
36 | if ($ERROR_LOG) { |
40 | if ($ERROR_LOG) { |
37 | use IO::Handle; |
41 | use IO::Handle; |
38 | open $errorlog, ">>$ERROR_LOG" |
42 | open $errorlog, ">>$ERROR_LOG" |
39 | or die "$ERROR_LOG: $!"; |
43 | or die "$ERROR_LOG: $!"; |
… | |
… | |
48 | } |
52 | } |
49 | |
53 | |
50 | sub slog { |
54 | sub slog { |
51 | my $level = shift; |
55 | my $level = shift; |
52 | my $format = shift; |
56 | my $format = shift; |
|
|
57 | |
|
|
58 | $format = sprintf $format, @_ if @_; |
|
|
59 | |
53 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
60 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
54 | printf "$NOW: $format\n", @_; |
61 | print "$NOW: $format\n"; |
55 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
62 | print $errorlog "$NOW: $format\n", @_ if $errorlog; |
56 | } |
63 | } |
57 | |
64 | |
58 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
65 | our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250; |
59 | our $httpevent = new Coro::Signal; |
66 | our $httpevent = new Coro::Signal; |
60 | |
67 | |
61 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
68 | our $queue_file = new transferqueue $::MAX_TRANSFERS; |
62 | our $queue_index = new transferqueue 10; |
69 | our $queue_index = new transferqueue 10; |
63 | |
70 | |
64 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
71 | our $tbf_top = new tbf rate => $::TBF_RATE || 100000; |
65 | |
72 | |
66 | my $unused_bytes = 0; |
73 | my $unused_bytes = 0; |
67 | my $unused_last = time; |
74 | my $unused_last = time; |
68 | |
75 | |
69 | sub unused_bandwidth { |
76 | sub unused_bandwidth { |
70 | $unused_bytes += $_[0]; |
77 | $unused_bytes += $_[0]; |
71 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
78 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
72 | $unused_last = $NOW; |
79 | $unused_last = $NOW; |
73 | $unused_bytes = 0; |
80 | $unused_bytes = 0; |
74 | $queue_file->force_wake_next; |
81 | $queue_file->force_wake_next |
75 | slog 1, "forced filetransfer due to unused bandwidth"; |
82 | and slog 1, "forced filetransfer due to unused bandwidth"; |
76 | } |
|
|
77 | } |
|
|
78 | |
|
|
79 | my @newcons; |
|
|
80 | my @pool; |
|
|
81 | |
|
|
82 | # one "execution thread" |
|
|
83 | sub handler { |
|
|
84 | while () { |
|
|
85 | if (@newcons) { |
|
|
86 | eval { |
|
|
87 | conn->new(@{pop @newcons})->handle; |
|
|
88 | }; |
|
|
89 | slog 1, "$@" if $@ && !ref $@; |
|
|
90 | |
|
|
91 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
92 | |
|
|
93 | $connections->up; |
|
|
94 | } else { |
|
|
95 | last if @pool >= $MAX_POOL; |
|
|
96 | push @pool, $Coro::current; |
|
|
97 | schedule; |
|
|
98 | } |
|
|
99 | } |
83 | } |
100 | } |
84 | } |
101 | |
85 | |
102 | sub listen_on { |
86 | sub listen_on { |
103 | my $listen = $_[0]; |
87 | my $listen = $_[0]; |
… | |
… | |
107 | # the "main thread" |
91 | # the "main thread" |
108 | async { |
92 | async { |
109 | slog 1, "accepting connections"; |
93 | slog 1, "accepting connections"; |
110 | while () { |
94 | while () { |
111 | $connections->down; |
95 | $connections->down; |
112 | push @newcons, [$listen->accept]; |
96 | my @conn = $listen->accept; |
113 | #slog 3, "accepted @$connections ".scalar(@pool); |
97 | #slog 3, "accepted @$connections ".scalar(@pool); |
114 | if (@pool) { |
98 | |
115 | (pop @pool)->ready; |
99 | async_pool { |
116 | } else { |
100 | eval { |
117 | async \&handler; |
101 | conn->new (@conn)->handle; |
|
|
102 | }; |
|
|
103 | slog 1, "$@" if $@ && !ref $@; |
|
|
104 | |
|
|
105 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
106 | |
|
|
107 | $connections->up; |
118 | } |
108 | } |
119 | } |
109 | } |
120 | }; |
110 | }; |
121 | } |
111 | } |
122 | |
112 | |
123 | my $http_port = new Coro::Socket |
113 | my $http_port = new Coro::Socket |
124 | LocalAddr => $SERVER_HOST, |
114 | LocalAddr => $::SERVER_HOST, |
125 | LocalPort => $SERVER_PORT, |
115 | LocalPort => $::SERVER_PORT, |
126 | ReuseAddr => 1, |
116 | ReuseAddr => 1, |
127 | Listen => 50, |
117 | Listen => 50, |
128 | or die "unable to start server"; |
118 | or die "unable to start server"; |
129 | |
119 | |
130 | listen_on $http_port; |
120 | listen_on $http_port; |
131 | |
121 | |
132 | if ($SERVER_PORT2) { |
122 | if ($::SERVER_PORT2) { |
133 | my $http_port = new Coro::Socket |
123 | my $http_port = new Coro::Socket |
134 | LocalAddr => $SERVER_HOST, |
124 | LocalAddr => $::SERVER_HOST, |
135 | LocalPort => $SERVER_PORT2, |
125 | LocalPort => $::SERVER_PORT2, |
136 | ReuseAddr => 1, |
126 | ReuseAddr => 1, |
137 | Listen => 50, |
127 | Listen => 50, |
138 | or die "unable to start server"; |
128 | or die "unable to start server"; |
139 | |
129 | |
140 | listen_on $http_port; |
130 | listen_on $http_port; |
141 | } |
131 | } |
142 | |
132 | |
143 | package conn; |
133 | package conn; |
144 | |
134 | |
145 | use strict; |
135 | use common::sense; |
146 | use bytes; |
|
|
147 | |
136 | |
148 | use Socket; |
137 | use Socket; |
149 | use HTTP::Date; |
138 | use HTTP::Date; |
150 | use Convert::Scalar 'weaken'; |
139 | use Convert::Scalar 'weaken'; |
151 | use IO::AIO; |
140 | use IO::AIO; |
152 | |
141 | |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
142 | IO::AIO::min_parallel $::AIO_PARALLEL; |
154 | |
143 | |
155 | Event->io (fd => IO::AIO::poll_fileno, |
144 | our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
156 | poll => 'r', async => 1, |
|
|
157 | cb => \&IO::AIO::poll_cb); |
|
|
158 | |
145 | |
159 | our %conn; # $conn{ip}{self} => connobj |
146 | our %conn; # $conn{ip}{self} => connobj |
160 | our %uri; # $uri{ip}{uri}{self} |
147 | our %uri; # $uri{ip}{uri}{self} |
161 | our %blocked; |
148 | our %blocked; |
162 | our %mimetype; |
149 | our %mimetype; |
163 | |
150 | |
164 | sub read_mimetypes { |
151 | sub read_mimetypes { |
165 | local *M; |
|
|
166 | if (open M, "<mime_types") { |
152 | if (open my $fh, "<mime_types") { |
167 | while (<M>) { |
153 | while (<$fh>) { |
168 | if (/^([^#]\S+)\t+(\S+)$/) { |
154 | if (/^([^#]\S+)\t+(\S+)$/) { |
169 | $mimetype{lc $1} = $2; |
155 | $mimetype{lc $1} = $2; |
170 | } |
156 | } |
171 | } |
157 | } |
172 | } else { |
158 | } else { |
… | |
… | |
180 | my $class = shift; |
166 | my $class = shift; |
181 | my $fh = shift; |
167 | my $fh = shift; |
182 | my $peername = shift; |
168 | my $peername = shift; |
183 | my $self = bless { fh => $fh }, $class; |
169 | my $self = bless { fh => $fh }, $class; |
184 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
170 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
185 | or $self->err(500, "unable to decode peername"); |
171 | or $self->err (500, "unable to decode peername"); |
186 | |
172 | |
187 | $self->{remote_addr} = |
173 | $self->{remote_addr} = |
188 | $self->{remote_id} = inet_ntoa $iaddr; |
174 | $self->{remote_id} = inet_ntoa $iaddr; |
189 | |
175 | |
190 | $self->{time} = $::NOW; |
176 | $self->{time} = $::NOW; |
191 | |
177 | |
192 | weaken ($Coro::current->{conn} = $self); |
178 | weaken ($Coro::current->{conn} = $self); |
193 | |
179 | |
194 | $::conns++; |
180 | ++$::conns; |
195 | $::maxconns = $::conns if $::conns > $::maxconns; |
181 | $::maxconns = $::conns if $::conns > $::maxconns; |
196 | |
182 | |
197 | $self; |
183 | $self |
198 | } |
184 | } |
199 | |
185 | |
200 | sub DESTROY { |
186 | sub DESTROY { |
201 | #my $self = shift; |
187 | my $self = shift; |
|
|
188 | |
202 | $::conns--; |
189 | --$::conns; |
203 | } |
190 | } |
204 | |
191 | |
205 | sub prune_cache { |
192 | sub prune_cache { |
206 | my $hash = $_[0]; |
193 | my $hash = $_[0]; |
207 | |
194 | |
… | |
… | |
222 | for (keys %blocked) { |
209 | for (keys %blocked) { |
223 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
210 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
224 | } |
211 | } |
225 | } |
212 | } |
226 | |
213 | |
227 | Event->timer(interval => 60, cb => \&prune_caches); |
214 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
228 | |
215 | |
229 | sub slog { |
216 | sub slog { |
230 | my $self = shift; |
217 | my $self = shift; |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
218 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
232 | } |
219 | } |
… | |
… | |
293 | $hdr->{"Content-Type"} = "text/plain"; |
280 | $hdr->{"Content-Type"} = "text/plain"; |
294 | $hdr->{"Content-Length"} = length $content; |
281 | $hdr->{"Content-Length"} = length $content; |
295 | } |
282 | } |
296 | $hdr->{"Connection"} = "close"; |
283 | $hdr->{"Connection"} = "close"; |
297 | |
284 | |
298 | $self->response($code, $msg, $hdr, $content); |
285 | $self->response ($code, $msg, $hdr, $content); |
299 | |
286 | |
300 | die bless {}, err::; |
287 | die bless {}, err:: |
301 | } |
288 | } |
302 | |
289 | |
303 | sub handle { |
290 | sub handle { |
304 | my $self = shift; |
291 | my $self = shift; |
305 | my $fh = $self->{fh}; |
292 | my $fh = $self->{fh}; |
306 | |
293 | |
307 | my $host; |
294 | my $host; |
308 | |
295 | |
309 | $fh->timeout($::REQ_TIMEOUT); |
296 | $fh->timeout($::REQ_TIMEOUT); |
310 | while() { |
297 | while () { |
311 | $self->{reqs}++; |
298 | $self->{reqs}++; |
312 | |
299 | |
313 | # read request and parse first line |
300 | # read request and parse first line |
314 | my $req = $fh->readline("\015\012\015\012"); |
301 | my $req = $fh->readline("\015\012\015\012"); |
315 | |
302 | |
… | |
… | |
424 | # uri => path mapping |
411 | # uri => path mapping |
425 | sub map_uri { |
412 | sub map_uri { |
426 | my $self = shift; |
413 | my $self = shift; |
427 | my $host = $self->{server_name}; |
414 | my $host = $self->{server_name}; |
428 | my $uri = $self->{uri}; |
415 | my $uri = $self->{uri}; |
|
|
416 | |
|
|
417 | $host =~ /[\/\\]/ |
|
|
418 | and $self->err(400, "bad request"); |
429 | |
419 | |
430 | # some massaging, also makes it more secure |
420 | # some massaging, also makes it more secure |
431 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
421 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
432 | $uri =~ s%//+%/%g; |
422 | $uri =~ s%//+%/%g; |
433 | $uri =~ s%/\.(?=/|$)%%g; |
423 | $uri =~ s%/\.(?=/|$)%%g; |
… | |
… | |
477 | |
467 | |
478 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
468 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
479 | if ($::internal{$1}) { |
469 | if ($::internal{$1}) { |
480 | $::internal{$1}->($self); |
470 | $::internal{$1}->($self); |
481 | } else { |
471 | } else { |
482 | $self->err(404, "not found"); |
472 | $self->err (404, "not found"); |
483 | } |
473 | } |
484 | } else { |
474 | } else { |
485 | |
475 | |
486 | stat $path |
476 | Coro::AIO::aio_stat $path |
487 | or $self->err(404, "not found"); |
477 | and $self->err (404, "not found"); |
488 | |
478 | |
489 | $self->{stat} = [stat _]; |
479 | $self->{stat} = [stat _]; |
490 | |
480 | |
491 | # idiotic netscape sends idiotic headers AGAIN |
481 | # idiotic netscape sends idiotic headers AGAIN |
492 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
482 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
495 | if (-d _ && -r _) { |
485 | if (-d _ && -r _) { |
496 | # directory |
486 | # directory |
497 | if ($path !~ /\/$/) { |
487 | if ($path !~ /\/$/) { |
498 | # create a redirect to get the trailing "/" |
488 | # create a redirect to get the trailing "/" |
499 | # we don't try to avoid the :80 |
489 | # we don't try to avoid the :80 |
500 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
490 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
501 | } else { |
491 | } else { |
502 | $ims < $self->{stat}[9] |
492 | $ims < $self->{stat}[9] |
503 | or $self->err(304, "not modified"); |
493 | or $self->err (304, "not modified"); |
504 | |
494 | |
505 | if (-r "$path/index.html") { |
495 | if (-r "$path/index.html") { |
506 | # replace directory "size" by index.html filesize |
496 | # replace directory "size" by index.html filesize |
507 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
497 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
508 | $self->handle_file($queue_index, $tbf_top); |
498 | $self->handle_file ($queue_index, $tbf_top); |
509 | } else { |
499 | } else { |
510 | $self->handle_dir; |
500 | $self->handle_dir; |
511 | } |
501 | } |
512 | } |
502 | } |
513 | } elsif (-f _ && -r _) { |
503 | } elsif (-f _ && -r _) { |
514 | -x _ and $self->err(403, "forbidden"); |
504 | -x _ and $self->err (403, "forbidden"); |
515 | |
505 | |
516 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
506 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
517 | my $timeout = $::NOW + 10; |
507 | my $timeout = $::NOW + 10; |
518 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
508 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
519 | if ($timeout < $::NOW) { |
509 | if ($timeout < $::NOW) { |
… | |
… | |
522 | $httpevent->wait; |
512 | $httpevent->wait; |
523 | } |
513 | } |
524 | } |
514 | } |
525 | } |
515 | } |
526 | |
516 | |
527 | $self->handle_file($queue_file, $tbf_top); |
517 | $self->handle_file ($queue_file, $tbf_top); |
528 | } else { |
518 | } else { |
529 | $self->err(404, "not found"); |
519 | $self->err (404, "not found"); |
530 | } |
520 | } |
531 | } |
521 | } |
532 | } |
522 | } |
533 | |
523 | |
534 | sub handle_dir { |
524 | sub handle_dir { |
535 | my $self = shift; |
525 | my $self = shift; |
536 | my $idx = $self->diridx; |
526 | my $idx = $self->diridx; |
537 | |
527 | |
538 | $self->response(200, "ok", |
528 | $self->response (200, "ok", |
539 | { |
529 | { |
540 | "Content-Type" => "text/html; charset=utf-8", |
530 | "Content-Type" => "text/html; charset=utf-8", |
541 | "Content-Length" => length $idx, |
531 | "Content-Length" => length $idx, |
542 | "Last-Modified" => time2str ($self->{stat}[9]), |
532 | "Last-Modified" => time2str ($self->{stat}[9]), |
543 | }, |
533 | }, |
… | |
… | |
553 | }; |
543 | }; |
554 | |
544 | |
555 | my @code = (200, "ok"); |
545 | my @code = (200, "ok"); |
556 | my ($l, $h); |
546 | my ($l, $h); |
557 | |
547 | |
558 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
548 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
559 | for (split /,/, $1) { |
549 | for (split /,/, $1) { |
560 | if (/^-(\d+)$/) { |
550 | if (/^-(\d+)$/) { |
561 | ($l, $h) = ($length - $1, $length - 1); |
551 | ($l, $h) = ($length - $1, $length - 1); |
562 | } elsif (/^(\d+)-(\d*)$/) { |
552 | } elsif (/^(\d+)-(\d*)$/) { |
563 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
553 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
567 | } |
557 | } |
568 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
558 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
569 | } |
559 | } |
570 | $hdr->{"Content-Range"} = "bytes */$length"; |
560 | $hdr->{"Content-Range"} = "bytes */$length"; |
571 | $hdr->{"Content-Length"} = $length; |
561 | $hdr->{"Content-Length"} = $length; |
572 | $self->err(416, "not satisfiable", $hdr, ""); |
562 | $self->err (416, "not satisfiable", $hdr, ""); |
573 | |
563 | |
574 | satisfiable: |
564 | satisfiable: |
575 | # check for segmented downloads |
565 | # check for segmented downloads |
576 | if ($l && $::NO_SEGMENTED) { |
566 | if ($l && $::NO_SEGMENTED) { |
577 | my $timeout = $::NOW + 15; |
567 | my $timeout = $::NOW + 15; |
578 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
568 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
579 | if ($timeout <= $::NOW) { |
569 | if ($timeout <= $::NOW) { |
580 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
570 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
581 | #$self->err_segmented_download; |
571 | #$self->err_segmented_download; |
582 | } else { |
572 | } else { |
583 | $httpevent->wait; |
573 | $httpevent->wait; |
584 | } |
574 | } |
585 | } |
575 | } |
… | |
… | |
596 | |
586 | |
597 | $self->{path} =~ /\.([^.]+)$/; |
587 | $self->{path} =~ /\.([^.]+)$/; |
598 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
588 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
599 | $hdr->{"Content-Length"} = $length; |
589 | $hdr->{"Content-Length"} = $length; |
600 | |
590 | |
601 | $self->response(@code, $hdr, ""); |
591 | $self->response (@code, $hdr, ""); |
602 | |
592 | |
603 | if ($self->{method} eq "GET") { |
593 | if ($self->{method} eq "GET") { |
604 | $self->{time} = $::NOW; |
594 | $self->{time} = $::NOW; |
605 | $self->{written} = 0; |
595 | $self->{written} = 0; |
606 | |
596 | |
607 | my $fh; |
597 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
608 | |
|
|
609 | open $fh, "<", $self->{path} |
|
|
610 | or die "$self->{path}: late open failure ($!)"; |
598 | or die "$self->{path}: late open failure ($!)"; |
611 | |
599 | |
612 | $h -= $l - 1; |
600 | $h -= $l - 1; |
613 | |
601 | |
614 | if (0) { # !AIO |
|
|
615 | if ($l) { |
|
|
616 | sysseek $fh, $l, 0; |
|
|
617 | } |
|
|
618 | } |
|
|
619 | |
|
|
620 | my $transfer = $queue->start_transfer($h); |
602 | my $transfer = $queue->start_transfer ($h); |
621 | my $locked; |
603 | my $locked; |
622 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
604 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
623 | |
605 | |
624 | while ($h > 0) { |
606 | while ($h > 0) { |
|
|
607 | Coro::cede; |
|
|
608 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
609 | |
625 | unless ($locked) { |
610 | unless ($locked) { |
626 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
611 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
627 | $bufsize = $::BUFSIZE; |
612 | $bufsize = $::BUFSIZE; |
628 | $self->{time} = $::NOW; |
613 | $self->{time} = $::NOW; |
629 | $self->{written} = 0; |
614 | $self->{written} = 0; |
630 | } |
615 | } |
631 | } |
616 | } |
… | |
… | |
637 | |
622 | |
638 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
623 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
639 | or last; |
624 | or last; |
640 | |
625 | |
641 | $tbf->request (length $buf); |
626 | $tbf->request (length $buf); |
642 | my $w = syswrite $self->{fh}, $buf |
627 | my $w = $self->{fh}->syswrite ($buf) |
643 | or last; |
628 | or last; |
644 | $::written += $w; |
629 | $::written += $w; |
645 | $self->{written} += $w; |
630 | $self->{written} += $w; |
646 | $l += $w; |
631 | $l += $w; |
647 | } |
632 | } |