App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/Protocol.pm view on Meta::CPAN
my $class = shift;
my $args = named(\@_, {
in => { type => 'handle' },
out => { type => 'handle' },
language => { type => 'string', optional => 1 },
}, 'Protocol->new');
binmode $args->{in};
binmode $args->{out};
$args->{out}->autoflush(1);
return bless {
in => $args->{in},
out => $args->{out},
language => $args->{language},
buffer => '',
closed => 0,
transport_failed => 0,
# Payload bytes as they crossed the wire, headers and control
# messages excluded: this is the number a transfer report means.
sent_bytes => 0,
received_bytes => 0,
}, $class;
}
=head2 send
Purpose: send one control message.
Args: C<\%message>.
Returns: 1.
Side Effects: writes one line.
Usage:
$connection->send({ cmd => 'plan' });
=head3 EXAMPLE
$connection->send({ ok => 1, off => $offset });
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'self', type => 'object' },
{ name => 'message', type => 'hashref' },
]
=head4 OUTPUT
{ type => 'integer', min => 1, max => 1 }
=head3 MESSAGES
Key Meaning Resolution
protocol.closed the connection was closed a caller bug
protocol.write_failed the pipe would not take it see the transport's stderr
=head3 FORMAL SPECIFICATION
pre ¬closed
out' = out â json(message) â "\n"
=cut
sub send { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
my ($self, $message) = @_;
$self->_write(json_encode($message) . "\n");
return 1;
}
=head2 recv
Purpose: read one control message.
Args: none.
Returns: the decoded message hashref.
Side Effects: reads from C<in>; blocks until a whole line arrives.
Usage:
my $reply = $connection->recv();
=head3 EXAMPLE
my $hello = $connection->recv();
die unless $hello->{ok};
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'self', type => 'object' } ]
=head4 OUTPUT
{ type => 'hashref' }
=head3 MESSAGES
Key Meaning Resolution
protocol.malformed the line is not JSON version mismatch or corrupt stream
protocol.not_object the JSON is not an object version mismatch
protocol.eof the peer closed the connection see the peer's log
protocol.read_failed the pipe failed see the transport's stderr
=head3 FORMAL SPECIFICATION
in = line â "\n" â rest â recv = jsonâ»Â¹(line) â§ in' = rest
pre jsonâ»Â¹(line) â OBJECT
=cut
sub recv { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
my ($self) = @_;
my $line = $self->read_line();
my $message = eval { json_decode($line) };
$self->_throw('protocol.malformed', { error => strip_location($@) }) if $@;
$self->_throw('protocol.not_object') unless ref($message) eq 'HASH';
return $message;
}
=head2 send_bytes
Purpose: write raw bytes (the payload after a header).
Args: C<$bytes>; empty or undef writes nothing.
Returns: 1.
Side Effects: writes to C<out>. Not counted in L</bytes_sent>; use
L</send_data> for counted payloads.
Usage:
$connection->send_bytes($payload);
=head3 EXAMPLE
$connection->send({ len => length $bytes });
$connection->send_bytes($bytes);
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'self', type => 'object' },
{ name => 'bytes', type => 'string', optional => 1 },
]
=head4 OUTPUT
{ type => 'integer', min => 1, max => 1 }
=head3 MESSAGES
Key Meaning Resolution
protocol.closed the connection was closed a caller bug
protocol.write_failed the pipe would not take it see the transport's stderr
=head3 FORMAL SPECIFICATION
out' = out â bytes
=cut
sub send_bytes {
my ($self, $bytes) = @_;
$self->_write($bytes) if defined $bytes && length $bytes;
return 1;
}
=head2 send_data
Purpose: send a header and its payload, compressing when worthwhile.
Args: C<\%header>, C<$bytes>.
Returns: 1.
Side Effects: writes the header (with C<len> and, if compressed, C<z>) and the
payload; adds the wire length to L</bytes_sent>.
Usage:
$connection->send_data({ cmd => 'chunk', p => $path }, $bytes);
=head3 EXAMPLE
$connection->send_data({ ok => 1, eof => 1 }, $last_chunk);
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'self', type => 'object' },
{ name => 'message', type => 'hashref', optional => 1 },
{ name => 'bytes', type => 'string', optional => 1 },
]
=head4 OUTPUT
{ type => 'integer', min => 1, max => 1 }
=head3 MESSAGES
As for L</send>.
=head3 FORMAL SPECIFICATION
let (w, z) = deflate(bytes)
out' = out â json(header â {len ⦠#w} â (z â {z ⦠1})) â "\n" â w
sent' = sent + #w
=cut
sub send_data {
lib/App/FileSync/Protocol.pm view on Meta::CPAN
read_data(h) = inflate(take(h.len, in)) if h.z
= take(h.len, in) otherwise
received' = received + h.len
=cut
sub read_data {
my ($self, $message) = @_;
my $length = defined $message->{len} ? $message->{len} : 0;
$self->_throw('protocol.bad_length')
unless $length =~ /\A\d+\z/ && $length <= $MAX_WIRE;
my $wire = $length ? $self->read_bytes($length) : '';
$self->{received_bytes} += length $wire;
return $message->{z} ? $self->_inflate($wire) : $wire;
}
=head2 read_line
Documented with L</read_bytes> below.
=head2 read_bytes
Purpose: read up to the next newline, or exactly C<$length> bytes.
Args: C<read_bytes($length)>.
Returns: the line (without its newline) or the bytes.
Side Effects: consumes them from the buffer, reading from C<in> as needed.
Usage:
my $line = $connection->read_line();
my $bytes = $connection->read_bytes(1024);
=head3 EXAMPLE
my $payload = $connection->read_bytes($header->{len});
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'self', type => 'object' },
{ name => 'length', type => 'integer', min => 0 },
]
=head4 OUTPUT
{ type => 'string' }
=head3 MESSAGES
Key Meaning Resolution
protocol.negative a negative length was asked for a caller bug
protocol.eof the peer closed the connection see the peer's log
protocol.read_failed the pipe failed see the transport's stderr
=head3 FORMAL SPECIFICATION
read_line: in = l â "\n" â r â§ "\n" â ran l â result = l â§ in' = r
read_bytes(n): in = b â r â§ #b = n â result = b â§ in' = r
=cut
sub read_line {
my ($self) = @_;
my $position;
$self->_fill() while ($position = index($self->{buffer}, "\n")) < 0;
my $line = substr($self->{buffer}, 0, $position);
substr($self->{buffer}, 0, $position + 1, '');
return $line;
}
sub read_bytes {
my ($self, $length) = @_;
$self->_throw('protocol.negative') if $length < 0;
$self->_fill() while length($self->{buffer}) < $length;
return substr($self->{buffer}, 0, $length, '');
}
=head2 bytes_sent
Documented with L</bytes_received> below.
=head2 bytes_received
Purpose: payload bytes that crossed the wire in each direction.
Args: none.
Returns: a byte count (headers and control messages excluded).
Side Effects: none.
Usage:
printf "sent %d bytes\n", $connection->bytes_sent;
=head3 EXAMPLE
my %summary = (bytes_sent => $connection->bytes_sent, bytes_received => $connection->bytes_received);
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'self', type => 'object' } ]
=head4 OUTPUT
{ type => 'integer', min => 0 }
( run in 0.594 second using v1.01-cache-2.11-cpan-60884a8df76 )