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