.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "IO::Async::Protocol::Stream 3pm" .TH IO::Async::Protocol::Stream 3pm 2024-02-04 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "IO::Async::Protocol::Stream" \- base class for stream\-based protocols .SH SYNOPSIS .IX Header "SYNOPSIS" Most likely this class will be subclassed to implement a particular network protocol. .PP .Vb 1 \& package Net::Async::HelloWorld; \& \& use strict; \& use warnings; \& use base qw( IO::Async::Protocol::Stream ); \& \& sub on_read \& { \& my $self = shift; \& my ( $buffref, $eof ) = @_; \& \& return 0 unless $$buffref =~ s/^(.*)\en//; \& my $line = $1; \& \& if( $line =~ m/^HELLO (.*)/ ) { \& my $name = $1; \& \& $self\->invoke_event( on_hello => $name ); \& } \& \& return 1; \& } \& \& sub send_hello \& { \& my $self = shift; \& my ( $name ) = @_; \& \& $self\->write( "HELLO $name\en" ); \& } .Ve .PP This small example elides such details as error handling, which a real protocol implementation would be likely to contain. .SH DESCRIPTION .IX Header "DESCRIPTION" This subclass of IO::Async::Protocol is intended to stand as a base class for implementing stream-based protocols. It provides an interface similar to IO::Async::Stream, primarily, a \f(CW\*(C`write\*(C'\fR method and an \f(CW\*(C`on_read\*(C'\fR event handler. .PP It contains an instance of an IO::Async::Stream object which it uses for actual communication, rather than being a subclass of it, allowing a level of independence from the actual stream being used. For example, the stream may actually be an IO::Async::SSLStream to allow the protocol to be used over SSL. .PP As with IO::Async::Stream, it is required that by the time the protocol object is added to a Loop, that it either has an \f(CW\*(C`on_read\*(C'\fR method, or has been configured with an \f(CW\*(C`on_read\*(C'\fR callback handler. .SH EVENTS .IX Header "EVENTS" The following events are invoked, either using subclass methods or CODE references in parameters: .ie n .SS "$ret = on_read \e$buffer, $eof" .el .SS "\f(CW$ret\fP = on_read \e$buffer, \f(CW$eof\fP" .IX Subsection "$ret = on_read $buffer, $eof" .SS on_read_eof .IX Subsection "on_read_eof" .SS on_write_eof .IX Subsection "on_write_eof" The event handlers are invoked identically to IO::Async::Stream. .SS on_closed .IX Subsection "on_closed" The \f(CW\*(C`on_closed\*(C'\fR handler is optional, but if provided, will be invoked after the stream is closed by either side (either because the \f(CWclose()\fR method has been invoked on it, or on an incoming EOF). .SH PARAMETERS .IX Header "PARAMETERS" The following named parameters may be passed to \f(CW\*(C`new\*(C'\fR or \f(CW\*(C`configure\*(C'\fR: .SS "on_read => CODE" .IX Subsection "on_read => CODE" .SS "on_read_eof => CODE" .IX Subsection "on_read_eof => CODE" .SS "on_write_eof => CODE" .IX Subsection "on_write_eof => CODE" CODE references for the events. .SS "handle => IO" .IX Subsection "handle => IO" A shortcut for the common case where the transport only needs to be a plain IO::Async::Stream object. If this argument is provided without a \&\f(CW\*(C`transport\*(C'\fR object, a new IO::Async::Stream object will be built around the given IO handle, and used as the transport. .SH METHODS .IX Header "METHODS" .SS write .IX Subsection "write" .Vb 1 \& $protocol\->write( $data ); .Ve .PP Writes the given data by calling the \f(CW\*(C`write\*(C'\fR method on the contained transport stream. .SS connect .IX Subsection "connect" .Vb 1 \& $protocol\->connect( %args ); .Ve .PP Sets up a connection to a peer, and configures the underlying \f(CW\*(C`transport\*(C'\fR for the Protocol. Calls IO::Async::Protocol \f(CW\*(C`connect\*(C'\fR with \f(CW\*(C`socktype\*(C'\fR set to \&\f(CW"stream"\fR. .SH AUTHOR .IX Header "AUTHOR" Paul Evans