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 high_water_mark
94 my $size = $msg->high_water_mark;
95 $msg = $msg->high_water_mark(1024);
96
97 Maximum size of "write" buffer in bytes before "can_write" returns
98 false, defaults to 1048576 (1MiB).
99
100 reactor
101 my $reactor = $stream->reactor;
102 $stream = $stream->reactor(Mojo::Reactor::Poll->new);
103
104 Low-level event reactor, defaults to the "reactor" attribute value of
105 the global Mojo::IOLoop singleton. Note that this attribute is
106 weakened.
107
109 Mojo::IOLoop::Stream inherits all methods from Mojo::EventEmitter and
110 implements the following new ones.
111
112 bytes_read
113 my $num = $stream->bytes_read;
114
115 Number of bytes received.
116
117 bytes_waiting
118 my $num = $stream->bytes_waiting;
119
120 Number of bytes that have been enqueued with "write" and are waiting to
121 be written.
122
123 bytes_written
124 my $num = $stream->bytes_written;
125
126 Number of bytes written.
127
128 can_write
129 my $bool = $stream->can_write;
130
131 Returns true if calling "write" is safe.
132
133 close
134 $stream->close;
135
136 Close stream immediately.
137
138 close_gracefully
139 $stream->close_gracefully;
140
141 Close stream gracefully.
142
143 handle
144 my $handle = $stream->handle;
145
146 Get handle for stream, usually an IO::Socket::IP or IO::Socket::SSL
147 object.
148
149 is_readable
150 my $bool = $stream->is_readable;
151
152 Quick non-blocking check if stream is readable, useful for identifying
153 tainted sockets.
154
155 is_writing
156 my $bool = $stream->is_writing;
157
158 Check if stream is writing.
159
160 new
161 my $stream = Mojo::IOLoop::Stream->new($handle);
162
163 Construct a new Mojo::IOLoop::Stream object.
164
165 start
166 $stream->start;
167
168 Start or resume watching for new data on the stream.
169
170 steal_handle
171 my $handle = $stream->steal_handle;
172
173 Steal "handle" and prevent it from getting closed automatically.
174
175 stop
176 $stream->stop;
177
178 Stop watching for new data on the stream.
179
180 timeout
181 my $timeout = $stream->timeout;
182 $stream = $stream->timeout(45);
183
184 Maximum amount of time in seconds stream can be inactive before getting
185 closed automatically, defaults to 15. Setting the value to 0 will
186 allow this stream to be inactive indefinitely.
187
188 write
189 $stream = $stream->write($bytes);
190 $stream = $stream->write($bytes => sub {...});
191
192 Enqueue data to be written to the stream as soon as possible, the
193 optional drain callback will be executed once all data has been
194 written.
195
197 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
198
199
200
201perl v5.32.0 2020-07-28 Mojo::IOLoop::Stream(3)