1IO::Async::Protocol::StUrseearm(C3o)ntributed Perl DocumIeOn:t:aAtsiyonnc::Protocol::Stream(3)
2
3
4
6 "IO::Async::Protocol::Stream" - base class for stream-based protocols
7
9 Most likely this class will be subclassed to implement a particular
10 network protocol.
11
12 package Net::Async::HelloWorld;
13
14 use strict;
15 use warnings;
16 use base qw( IO::Async::Protocol::Stream );
17
18 sub on_read
19 {
20 my $self = shift;
21 my ( $buffref, $eof ) = @_;
22
23 return 0 unless $$buffref =~ s/^(.*)\n//;
24 my $line = $1;
25
26 if( $line =~ m/^HELLO (.*)/ ) {
27 my $name = $1;
28
29 $self->invoke_event( on_hello => $name );
30 }
31
32 return 1;
33 }
34
35 sub send_hello
36 {
37 my $self = shift;
38 my ( $name ) = @_;
39
40 $self->write( "HELLO $name\n" );
41 }
42
43 This small example elides such details as error handling, which a real
44 protocol implementation would be likely to contain.
45
47 This subclass of IO::Async::Protocol is intended to stand as a base
48 class for implementing stream-based protocols. It provides an interface
49 similar to IO::Async::Stream, primarily, a "write" method and an
50 "on_read" event handler.
51
52 It contains an instance of an IO::Async::Stream object which it uses
53 for actual communication, rather than being a subclass of it, allowing
54 a level of independence from the actual stream being used. For example,
55 the stream may actually be an IO::Async::SSLStream to allow the
56 protocol to be used over SSL.
57
58 As with IO::Async::Stream, it is required that by the time the protocol
59 object is added to a Loop, that it either has an "on_read" method, or
60 has been configured with an "on_read" callback handler.
61
63 The following events are invoked, either using subclass methods or CODE
64 references in parameters:
65
66 $ret = on_read \$buffer, $eof
67 on_read_eof
68 on_write_eof
69 The event handlers are invoked identically to IO::Async::Stream.
70
71 on_closed
72 The "on_closed" handler is optional, but if provided, will be invoked
73 after the stream is closed by either side (either because the "close()"
74 method has been invoked on it, or on an incoming EOF).
75
77 The following named parameters may be passed to "new" or "configure":
78
79 on_read => CODE
80 on_read_eof => CODE
81 on_write_eof => CODE
82 CODE references for the events.
83
84 handle => IO
85 A shortcut for the common case where the transport only needs to be a
86 plain IO::Async::Stream object. If this argument is provided without a
87 "transport" object, a new IO::Async::Stream object will be built around
88 the given IO handle, and used as the transport.
89
91 write
92 $protocol->write( $data )
93
94 Writes the given data by calling the "write" method on the contained
95 transport stream.
96
97 connect
98 $protocol->connect( %args )
99
100 Sets up a connection to a peer, and configures the underlying
101 "transport" for the Protocol. Calls IO::Async::Protocol "connect" with
102 "socktype" set to "stream".
103
105 Paul Evans <leonerd@leonerd.org.uk>
106
107
108
109perl v5.34.0 2021-08-08 IO::Async::Protocol::Stream(3)