1Mojo::IOLoop::Stream(3)User Contributed Perl DocumentatioMnojo::IOLoop::Stream(3)
2
3
4

NAME

6       Mojo::IOLoop::Stream - Non-blocking I/O stream
7

SYNOPSIS

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

DESCRIPTION

34       Mojo::IOLoop::Stream is a container for I/O streams used by
35       Mojo::IOLoop.
36

EVENTS

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

ATTRIBUTES

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). Note that this attribute is
99       EXPERIMENTAL and might change without warning!
100
101   reactor
102         my $reactor = $stream->reactor;
103         $stream     = $stream->reactor(Mojo::Reactor::Poll->new);
104
105       Low-level event reactor, defaults to the "reactor" attribute value of
106       the global Mojo::IOLoop singleton. Note that this attribute is
107       weakened.
108

METHODS

110       Mojo::IOLoop::Stream inherits all methods from Mojo::EventEmitter and
111       implements the following new ones.
112
113   bytes_read
114         my $num = $stream->bytes_read;
115
116       Number of bytes received.
117
118   bytes_waiting
119         my $num = $stream->bytes_waiting;
120
121       Number of bytes that have been enqueued with "write" and are waiting to
122       be written. Note that this method is EXPERIMENTAL and might change
123       without warning!
124
125   bytes_written
126         my $num = $stream->bytes_written;
127
128       Number of bytes written.
129
130   can_write
131         my $bool = $stream->can_write;
132
133       Returns true if calling "write" is safe. Note that this method is
134       EXPERIMENTAL and might change without warning!
135
136   close
137         $stream->close;
138
139       Close stream immediately.
140
141   close_gracefully
142         $stream->close_gracefully;
143
144       Close stream gracefully.
145
146   handle
147         my $handle = $stream->handle;
148
149       Get handle for stream, usually an IO::Socket::IP or IO::Socket::SSL
150       object.
151
152   is_readable
153         my $bool = $stream->is_readable;
154
155       Quick non-blocking check if stream is readable, useful for identifying
156       tainted sockets.
157
158   is_writing
159         my $bool = $stream->is_writing;
160
161       Check if stream is writing.
162
163   new
164         my $stream = Mojo::IOLoop::Stream->new($handle);
165
166       Construct a new Mojo::IOLoop::Stream object.
167
168   start
169         $stream->start;
170
171       Start or resume watching for new data on the stream.
172
173   steal_handle
174         my $handle = $stream->steal_handle;
175
176       Steal "handle" and prevent it from getting closed automatically.
177
178   stop
179         $stream->stop;
180
181       Stop watching for new data on the stream.
182
183   timeout
184         my $timeout = $stream->timeout;
185         $stream     = $stream->timeout(45);
186
187       Maximum amount of time in seconds stream can be inactive before getting
188       closed automatically, defaults to 15. Setting the value to 0 will allow
189       this stream to be inactive indefinitely.
190
191   write
192         $stream = $stream->write($bytes);
193         $stream = $stream->write($bytes => sub {...});
194
195       Enqueue data to be written to the stream as soon as possible, the
196       optional drain callback will be executed once all data has been
197       written.
198

SEE ALSO

200       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
201
202
203
204perl v5.30.1                      2020-01-30           Mojo::IOLoop::Stream(3)
Impressum