# Bugs encountered were reported to bug-Danga-Socket@rt.cpan.org,
# fixed in Danga::Socket 1.62 and visible at:
# https://rt.cpan.org/Public/Dist/Display.html?Name=Danga-Socket
+#
+# fields:
+# sock: underlying socket
+# rbuf: scalarref, usually undef
+# wbuf: arrayref of coderefs or tmpio (autovivified))
+# (tmpio = [ GLOB, offset, [ length ] ])
package PublicInbox::DS;
use strict;
use bytes;
use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
use parent qw(Exporter);
our @EXPORT_OK = qw(now msg_more);
-use warnings;
use 5.010_001;
use Scalar::Util qw(blessed);
-
use PublicInbox::Syscall qw(:epoll);
use PublicInbox::Tmpfile;
-
-use fields ('sock', # underlying socket
- 'rbuf', # scalarref, usually undef
- 'wbuf', # arrayref of coderefs or tmpio (autovivified))
- # (tmpio = [ GLOB, offset, [ length ] ])
- );
-
use Errno qw(EAGAIN EINVAL);
use Carp qw(confess carp);
=cut
sub Reset {
%DescriptorMap = ();
- $wait_pids = $later_queue = undef;
+ $in_loop = $wait_pids = $later_queue = undef;
$EXPMAP = {};
$nextq = $ToClose = $reap_timer = $later_timer = $exp_timer = undef;
$LoopTimeout = -1; # no timeout by default
return $LoopTimeout = $_[1] + 0;
}
-=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef ) >>
+=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >>
Add a timer to occur $seconds from now. $seconds may be fractional, but timers
are not guaranteed to fire at the exact time you ask for.
=cut
-sub add_timer ($$) {
- my ($secs, $coderef) = @_;
+sub add_timer ($$;$) {
+ my ($secs, $coderef, $arg) = @_;
my $fire_time = now() + $secs;
- my $timer = [$fire_time, $coderef];
+ my $timer = [$fire_time, $coderef, $arg];
if (!@Timers || $fire_time >= $Timers[-1][0]) {
push @Timers, $timer;
# Run expired timers
while (@Timers && $Timers[0][0] <= $now) {
my $to_run = shift(@Timers);
- $to_run->[1]->($now) if $to_run->[1];
+ $to_run->[1]->($to_run->[2]);
}
# timers may enqueue into nextq:
=cut
sub new {
my ($self, $sock, $ev) = @_;
- $self = fields::new($self) unless ref $self;
-
$self->{sock} = $sock;
my $fd = fileno($sock);
# returns 1 if done, 0 if incomplete
sub flush_write ($) {
my ($self) = @_;
+ my $sock = $self->{sock} or return;
my $wbuf = $self->{wbuf} or return 1;
- my $sock = $self->{sock};
next_buf:
while (my $bref = $wbuf->[0]) {