-sub tmpbuf ($$) {
- my ($bref, $off) = @_;
- # open(my $fh, '+>>', undef) doesn't set O_APPEND
- my ($fh, $path) = tempfile('wbuf-XXXXXXX', TMPDIR => 1);
- open $fh, '+>>', $path or die "open: $!";
- unlink $path;
- my $to_write = bytes::length($$bref) - $off;
- my $w = write_in_full($fh, $bref, $to_write, $off);
- die "write_in_full ($to_write): $!" unless defined $w;
- $w == $to_write ? $fh : die("short write $w < $to_write");
+sub do_read ($$$;$) {
+ my ($self, $rbuf, $len, $off) = @_;
+ my $r = sysread(my $sock = $self->{sock}, $$rbuf, $len, $off // 0);
+ return ($r == 0 ? $self->close : $r) if defined $r;
+ # common for clients to break connections without warning,
+ # would be too noisy to log here:
+ if ($! == EAGAIN) {
+ my $ev = epbit($sock, EPOLLIN) or return $self->close;
+ epwait($sock, $ev | EPOLLONESHOT);
+ rbuf_idle($self, $rbuf);
+ 0;
+ } else {
+ $self->close;
+ }
+}
+
+# drop the socket if we hit unrecoverable errors on our system which
+# require BOFH attention: ENOSPC, EFBIG, EIO, EMFILE, ENFILE...
+sub drop {
+ my $self = shift;
+ carp(@_);
+ $self->close;
+}
+
+# n.b.: use ->write/->read for this buffer to allow compatibility with
+# PerlIO::mmap or PerlIO::scalar if needed
+sub tmpio ($$$) {
+ my ($self, $bref, $off) = @_;
+ my $fh = tmpfile('wbuf', $self->{sock}, O_APPEND) or
+ return drop($self, "tmpfile $!");
+ $fh->autoflush(1);
+ my $len = bytes::length($$bref) - $off;
+ $fh->write($$bref, $len, $off) or return drop($self, "write ($len): $!");
+ [ $fh, 0 ] # [1] = offset, [2] = length, not set by us