1Mojo::IOLoop::Stream(3)User Contributed Perl DocumentatioMnojo::IOLoop::Stream(3)
2
3
4
6 Mojo::IOLoop::Stream - Non-blocking I/O stream
7
9 use Mojo::IOLoop::Stream;
10
11 # Create stream
12 my $stream = Mojo::IOLoop::Stream->new($handle);
13 $stream->on(read => sub {
14 my ($stream, $bytes) = @_;
15 ...
16 });
17 $stream->on(close => sub {
18 my $stream = shift;
19 ...
20 });
21 $stream->on(error => sub {
22 my ($stream, $err) = @_;
23 ...
24 });
25
26 # Start and stop watching for new data
27 $stream->start;
28 $stream->stop;
29
30 # Start reactor if necessary
31 $stream->reactor->start unless $stream->reactor->is_running;
32
34 Mojo::IOLoop::Stream is a container for I/O streams used by
35 Mojo::IOLoop.
36
38 Mojo::IOLoop::Stream inherits all events from Mojo::EventEmitter and
39 can emit the following new ones.
40
41 close
42 $stream->on(close => sub {
43 my $stream = shift;
44 ...
45 });
46
47 Emitted if the stream gets closed.
48
49 drain
50 $stream->on(drain => sub {
51 my $stream = shift;
52 ...
53 });
54
55 Emitted once all data has been written.
56
57 error
58 $stream->on(error => sub {
59 my ($stream, $err) = @_;
60 ...
61 });
62
63 Emitted if an error occurs on the stream, fatal if unhandled.
64
65 read
66 $stream->on(read => sub {
67 my ($stream, $bytes) = @_;
68 ...
69 });
70
71 Emitted if new data arrives on the stream.
72
73 timeout
74 $stream->on(timeout => sub {
75 my $stream = shift;
76 ...
77 });
78
79 Emitted if the stream has been inactive for too long and will get
80 closed automatically.
81
82 write
83 $stream->on(write => sub {
84 my ($stream, $bytes) = @_;
85 ...
86 });
87
88 Emitted if new data has been written to the stream.
89
91 Mojo::IOLoop::Stream implements the following attributes.
92
93 reactor
94 my $reactor = $stream->reactor;
95 $stream = $stream->reactor(Mojo::Reactor::Poll->new);
96
97 Low-level event reactor, defaults to the "reactor" attribute value of
98 the global Mojo::IOLoop singleton. Note that this attribute is
99 weakened.
100
102 Mojo::IOLoop::Stream inherits all methods from Mojo::EventEmitter and
103 implements the following new ones.
104
105 bytes_read
106 my $num = $stream->bytes_read;
107
108 Number of bytes received.
109
110 bytes_written
111 my $num = $stream->bytes_written;
112
113 Number of bytes written.
114
115 close
116 $stream->close;
117
118 Close stream immediately.
119
120 close_gracefully
121 $stream->close_gracefully;
122
123 Close stream gracefully.
124
125 handle
126 my $handle = $stream->handle;
127
128 Get handle for stream, usually an IO::Socket::IP or IO::Socket::SSL
129 object.
130
131 is_readable
132 my $bool = $stream->is_readable;
133
134 Quick non-blocking check if stream is readable, useful for identifying
135 tainted sockets.
136
137 is_writing
138 my $bool = $stream->is_writing;
139
140 Check if stream is writing.
141
142 new
143 my $stream = Mojo::IOLoop::Stream->new($handle);
144
145 Construct a new Mojo::IOLoop::Stream object.
146
147 start
148 $stream->start;
149
150 Start or resume watching for new data on the stream.
151
152 steal_handle
153 my $handle = $stream->steal_handle;
154
155 Steal "handle" and prevent it from getting closed automatically.
156
157 stop
158 $stream->stop;
159
160 Stop watching for new data on the stream.
161
162 timeout
163 my $timeout = $stream->timeout;
164 $stream = $stream->timeout(45);
165
166 Maximum amount of time in seconds stream can be inactive before getting
167 closed automatically, defaults to 15. Setting the value to 0 will allow
168 this stream to be inactive indefinitely.
169
170 write
171 $stream = $stream->write($bytes);
172 $stream = $stream->write($bytes => sub {...});
173
174 Write data to stream, the optional drain callback will be executed once
175 all data has been written.
176
178 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
179
180
181
182perl v5.28.1 2018-11-28 Mojo::IOLoop::Stream(3)