blob: e7e649f3305b4a348a044c37f28b3b5d8fa72040 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package Spline::Netstring;
use strict;
use warnings;
our @EXPORT = qw(
netstring_read
netstring_write
);
sub read_length($) {
my $fd = shift;
my $length;
local $/ = ':';
$length = <$fd>;
die "Cannot read netstring length" unless defined($length);
chomp $length;
return $length;
}
sub netstring_read($) {
my $fd = shift;
my ($length, $data);
$length = read_length($fd);
if (read($fd, $data, $length) == $length) {
(getc() eq ',') or die "Closing , missing";
}
else {
die 'Received only ' . length($data) . " of $length bytes";
}
return $data;
}
sub netstring_write($$) {
my ($fd, $data) = @_;
print $fd length($data).':'.$data.',';
}
|