The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MojoX::NetstringStream - Turn a (tcp) stream into a NetstringStream

SYNOPSIS

  use MojoX::NetstringStream;

  my $clientid = Mojo::IOLoop->client({
    port => $port,
  } => sub {
    my ($loop, $err, $stream) = @_;
    my $ns = MojoX::NetstringStream->new(stream => $stream);
      $ns->on(chunk => sub {
         my ($ns, $chunk) = @_;
         say 'got chunk: ', $chunk;
         ...
      });
      $ns->on(close => sub {
         say 'got close';
         ...
      });
  });

DESCRIPTION

MojoX::NetstringStream is a wrapper around Mojo::IOLoop::Stream that adds framing using the netstring encoding.

ATTRIBUTES

stream

The underlying Mojo::IOLoop stream to use for reading and writing

debug

Enables debugging

maxsize

Maximum size of the accepted netstring frames, if set. A nserr event is raised when a oversized frame is received.

Default: none

EVENTS

MojoX::NetstringStream inherits all events from Mojo::EventEmitter and can emit the following new ones.

chunk

  $ns->on(chunk => sub {
    my ($ns, $chunk) = @_;
    ...
  });

Emitted for every (full) netstring received on the underlying stream.

close

  $ns->on(close => sub {
    my $ns = shift;
    ...
  });

Emitted if the underlying stream gets closed.

nserr

  $ns->on(nserr => sub {
    my ($ns, $err) = @_;
    ...
  });

Emitted if there was some kind of framing error, currenty either a missing ',' at the end or a oversized frame.

ATTRIBUTES

MojoX::NetstringStream implements the following attributes.

stream

  my $stream = $ns->stream;

The underlying Mojo::IOLoop::Stream-like stream

debug

  $ls->debug = 1;

Enables or disables debugging output.

METHODS

MojoX::NetstringStream inherits all methods from Mojo::EventEmitter and implements the following new ones.

new

  my $ns = MojoX::NetstringStream->new(
      stream => $stream,
      debug => $debug,
  );

Construct a new MojoX::NetstringStream object. The stream argument must behave like a Mojo::IOLoop::Stream object. The debug argument is optional and just sets the debug attribute.

write

  $ns->write($chunk);

Writes chunk to the underlying stream as a netstring.

SEE ALSO

ACKNOWLEDGEMENT

This software has been developed with support from STRATO. In German: Diese Software wurde mit Unterstützung von STRATO entwickelt.

AUTHORS

  • Wieger Opmeer <wiegerop@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Wieger Opmeer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.