diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-17 18:07:46 +0200 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-17 18:07:46 +0200 |
commit | fe71a855fc8b8d7a809c2087e3b12971ab74b0e2 (patch) | |
tree | 404802fd640f8c1ad98bd18456c42f9a8d1e972f /Spline/Netstring.pm | |
parent | 727eba8606fd58331ec5f1ae266eb0f9d6c0c130 (diff) | |
download | srs-fe71a855fc8b8d7a809c2087e3b12971ab74b0e2.tar.gz srs-fe71a855fc8b8d7a809c2087e3b12971ab74b0e2.tar.bz2 srs-fe71a855fc8b8d7a809c2087e3b12971ab74b0e2.zip |
First version
Diffstat (limited to 'Spline/Netstring.pm')
-rw-r--r-- | Spline/Netstring.pm | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Spline/Netstring.pm b/Spline/Netstring.pm new file mode 100644 index 0000000..e7e649f --- /dev/null +++ b/Spline/Netstring.pm @@ -0,0 +1,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.','; +} |