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

NAME

UV::Stream - Stream handles in libuv

SYNOPSIS

  #!/usr/bin/env perl
  use strict;
  use warnings;

  use UV;

  # Stream is the superclass of Pipe, TTY and TCP handles

  # TODO

DESCRIPTION

This module provides an interface to libuv's stream. We will try to document things here as best as we can, but we also suggest you look at the libuv docs directly for more details on how things work.

You will likely never use this class directly. You will use the different stream sub-classes directly. Some of these methods or events will be called or fired from those sub-classes.

EVENTS

UV::Stream makes the following extra events available.

connection

    $stream->on("connection", sub {
        my ($self) = @_;
        my $client = $self->accept;
        ...
    });

The connection callback fires when a new connection is received on a listening stream server.

Within the callback you should use "accept" to obtain the new client stream.

read

    $stream->on("read", sub {
        my ($self, $status, $buf) = @_;
        say "Received more data: <$buf>";
    });

The read callback fires whenever there is more incoming data on the stream to be passed to the application.

METHODS

UV::Stream makes the following methods available.

listen

    # start listening with the callback we supplied with ->on()
    $stream->listen($backlog);

    # pass a callback for the "connection" event
    $stream->listen($backlog, sub {
        my $client = $stream->accept;
        say "Received a new connection";
    });

The listen method starts a stream server listening for incoming client client connections. The connection event will be fired each time a new one arrives.

accept

    my $client = $stream->accept;

The accept method prepares a new stream connection to represent the next incoming client connection that has been received.

shutdown

    $stream->shutdown(sub {
        say "Stream is now shut down";
    });

The shutdown method stops the writing half of the socket once all of the currently-pending writes have been flushed.

read_start

    # start reading with the callback we supplied with ->on()
    $stream->read_start;

The read_start starts the reading side of the stream handle. The read event callback will be invoked whenever there is new data to be given to the application.

Returns the $stream instance itself.

read_stop

    $stream->read_stop;

The read_stop method stops the reading side of the stream handle.

write

    $stream->write($s, sub {
        say "Data has now been written to the stream";
    });

The write method sends more data through the writing side of the stream. The callback argument will be invoked when the data has been flushed to the filehandle.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

LICENSE

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