ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.40 by root, Tue May 27 05:36:27 2008 UTC vs.
Revision 1.47 by root, Thu May 29 00:25:28 2008 UTC

2 2
3no warnings; 3no warnings;
4use strict; 4use strict;
5 5
6use AnyEvent (); 6use AnyEvent ();
7use AnyEvent::Util qw(WSAWOULDBLOCK); 7use AnyEvent::Util qw(WSAEWOULDBLOCK);
8use Scalar::Util (); 8use Scalar::Util ();
9use Carp (); 9use Carp ();
10use Fcntl (); 10use Fcntl ();
11use Errno qw/EAGAIN EINTR/; 11use Errno qw(EAGAIN EINTR);
12 12
13=head1 NAME 13=head1 NAME
14 14
15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
16 16
91 91
92The object will not be in a usable state when this callback has been 92The object will not be in a usable state when this callback has been
93called. 93called.
94 94
95On callback entrance, the value of C<$!> contains the operating system 95On callback entrance, the value of C<$!> contains the operating system
96error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). 96error (or C<ENOSPC>, C<EPIPE>, C<ETIMEDOUT> or C<EBADMSG>).
97 97
98The callback should throw an exception. If it returns, then 98The callback should throw an exception. If it returns, then
99AnyEvent::Handle will C<croak> for you. 99AnyEvent::Handle will C<croak> for you.
100 100
101While not mandatory, it is I<highly> recommended to set this callback, as 101While not mandatory, it is I<highly> recommended to set this callback, as
119 119
120This sets the callback that is called when the write buffer becomes empty 120This sets the callback that is called when the write buffer becomes empty
121(or when the callback is set and the buffer is empty already). 121(or when the callback is set and the buffer is empty already).
122 122
123To append to the write buffer, use the C<< ->push_write >> method. 123To append to the write buffer, use the C<< ->push_write >> method.
124
125=item timeout => $fractional_seconds
126
127If non-zero, then this enables an "inactivity" timeout: whenever this many
128seconds pass without a successful read or write on the underlying file
129handle, the C<on_timeout> callback will be invoked (and if that one is
130missing, an C<ETIMEDOUT> error will be raised).
131
132Note that timeout processing is also active when you currently do not have
133any outstanding read or write requests: If you plan to keep the connection
134idle then you should disable the timout temporarily or ignore the timeout
135in the C<on_timeout> callback.
136
137Zero (the default) disables this timeout.
138
139=item on_timeout => $cb->($handle)
140
141Called whenever the inactivity timeout passes. If you return from this
142callback, then the timeout will be reset as if some activity had happened,
143so this condition is not fatal in any way.
124 144
125=item rbuf_max => <bytes> 145=item rbuf_max => <bytes>
126 146
127If defined, then a fatal error will be raised (with C<$!> set to C<ENOSPC>) 147If defined, then a fatal error will be raised (with C<$!> set to C<ENOSPC>)
128when the read buffer ever (strictly) exceeds this size. This is useful to 148when the read buffer ever (strictly) exceeds this size. This is useful to
135isn't finished). 155isn't finished).
136 156
137=item read_size => <bytes> 157=item read_size => <bytes>
138 158
139The default read block size (the amount of bytes this module will try to read 159The default read block size (the amount of bytes this module will try to read
140on each [loop iteration). Default: C<4096>. 160during each (loop iteration). Default: C<8192>.
141 161
142=item low_water_mark => <bytes> 162=item low_water_mark => <bytes>
143 163
144Sets the amount of bytes (default: C<0>) that make up an "empty" write 164Sets the amount of bytes (default: C<0>) that make up an "empty" write
145buffer: If the write reaches this size or gets even samller it is 165buffer: If the write reaches this size or gets even samller it is
172 192
173=item json => JSON or JSON::XS object 193=item json => JSON or JSON::XS object
174 194
175This is the json coder object used by the C<json> read and write types. 195This is the json coder object used by the C<json> read and write types.
176 196
177If you don't supply it, then AnyEvent::Handle will use C<encode_json> and 197If you don't supply it, then AnyEvent::Handle will create and use a
178C<decode_json>. 198suitable one, which will write and expect UTF-8 encoded JSON texts.
179 199
180Note that you are responsible to depend on the JSON module if you want to 200Note that you are responsible to depend on the JSON module if you want to
181use this functionality, as AnyEvent does not have a dependency itself. 201use this functionality, as AnyEvent does not have a dependency itself.
182 202
183=item filter_r => $cb 203=item filter_r => $cb
202 if ($self->{tls}) { 222 if ($self->{tls}) {
203 require Net::SSLeay; 223 require Net::SSLeay;
204 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); 224 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx});
205 } 225 }
206 226
207 $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; 227# $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop
208 $self->on_error (delete $self->{on_error}) if $self->{on_error}; 228# $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop
229# $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop
209 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 230 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
210 $self->on_read (delete $self->{on_read} ) if $self->{on_read}; 231
232 $self->{_activity} = AnyEvent->now;
233 $self->_timeout;
211 234
212 $self->start_read; 235 $self->start_read;
213 236
214 $self 237 $self
215} 238}
216 239
217sub _shutdown { 240sub _shutdown {
218 my ($self) = @_; 241 my ($self) = @_;
219 242
243 delete $self->{_tw};
220 delete $self->{_rw}; 244 delete $self->{_rw};
221 delete $self->{_ww}; 245 delete $self->{_ww};
222 delete $self->{fh}; 246 delete $self->{fh};
223} 247}
224 248
262 286
263sub on_eof { 287sub on_eof {
264 $_[0]{on_eof} = $_[1]; 288 $_[0]{on_eof} = $_[1];
265} 289}
266 290
291=item $handle->on_timeout ($cb)
292
293Replace the current C<on_timeout> callback, or disables the callback
294(but not the timeout) if C<$cb> = C<undef>. See C<timeout> constructor
295argument.
296
297=cut
298
299sub on_timeout {
300 $_[0]{on_timeout} = $_[1];
301}
302
303#############################################################################
304
305=item $handle->timeout ($seconds)
306
307Configures (or disables) the inactivity timeout.
308
309=cut
310
311sub timeout {
312 my ($self, $timeout) = @_;
313
314 $self->{timeout} = $timeout;
315 $self->_timeout;
316}
317
318# reset the timeout watcher, as neccessary
319# also check for time-outs
320sub _timeout {
321 my ($self) = @_;
322
323 if ($self->{timeout}) {
324 my $NOW = AnyEvent->now;
325
326 # when would the timeout trigger?
327 my $after = $self->{_activity} + $self->{timeout} - $NOW;
328
329 # now or in the past already?
330 if ($after <= 0) {
331 $self->{_activity} = $NOW;
332
333 if ($self->{on_timeout}) {
334 $self->{on_timeout}->($self);
335 } else {
336 $! = Errno::ETIMEDOUT;
337 $self->error;
338 }
339
340 # callbakx could have changed timeout value, optimise
341 return unless $self->{timeout};
342
343 # calculate new after
344 $after = $self->{timeout};
345 }
346
347 Scalar::Util::weaken $self;
348
349 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub {
350 delete $self->{_tw};
351 $self->_timeout;
352 });
353 } else {
354 delete $self->{_tw};
355 }
356}
357
267############################################################################# 358#############################################################################
268 359
269=back 360=back
270 361
271=head2 WRITE QUEUE 362=head2 WRITE QUEUE
316 my $len = syswrite $self->{fh}, $self->{wbuf}; 407 my $len = syswrite $self->{fh}, $self->{wbuf};
317 408
318 if ($len >= 0) { 409 if ($len >= 0) {
319 substr $self->{wbuf}, 0, $len, ""; 410 substr $self->{wbuf}, 0, $len, "";
320 411
412 $self->{_activity} = AnyEvent->now;
413
321 $self->{on_drain}($self) 414 $self->{on_drain}($self)
322 if $self->{low_water_mark} >= length $self->{wbuf} 415 if $self->{low_water_mark} >= length $self->{wbuf}
323 && $self->{on_drain}; 416 && $self->{on_drain};
324 417
325 delete $self->{_ww} unless length $self->{wbuf}; 418 delete $self->{_ww} unless length $self->{wbuf};
326 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) { 419 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) {
327 $self->error; 420 $self->error;
328 } 421 }
329 }; 422 };
330 423
331 # try to write data immediately 424 # try to write data immediately
395in UTF-8. 488in UTF-8.
396 489
397JSON objects (and arrays) are self-delimiting, so you can write JSON at 490JSON objects (and arrays) are self-delimiting, so you can write JSON at
398one end of a handle and read them at the other end without using any 491one end of a handle and read them at the other end without using any
399additional framing. 492additional framing.
493
494The generated JSON text is guaranteed not to contain any newlines: While
495this module doesn't need delimiters after or between JSON texts to be
496able to read them, many other languages depend on that.
497
498A simple RPC protocol that interoperates easily with others is to send
499JSON arrays (or objects, although arrays are usually the better choice as
500they mimic how function argument passing works) and a newline after each
501JSON text:
502
503 $handle->push_write (json => ["method", "arg1", "arg2"]); # whatever
504 $handle->push_write ("\012");
505
506An AnyEvent::Handle receiver would simply use the C<json> read type and
507rely on the fact that the newline will be skipped as leading whitespace:
508
509 $handle->push_read (json => sub { my $array = $_[1]; ... });
510
511Other languages could read single lines terminated by a newline and pass
512this line into their JSON decoder of choice.
400 513
401=cut 514=cut
402 515
403register_write_type json => sub { 516register_write_type json => sub {
404 my ($self, $ref) = @_; 517 my ($self, $ref) = @_;
8592.09 (and JSON::XS version 2.2) and above. You have to provide a 9722.09 (and JSON::XS version 2.2) and above. You have to provide a
860dependency on your own: this module will load the JSON module, but 973dependency on your own: this module will load the JSON module, but
861AnyEvent does not depend on it itself. 974AnyEvent does not depend on it itself.
862 975
863Since JSON texts are fully self-delimiting, the C<json> read and write 976Since JSON texts are fully self-delimiting, the C<json> read and write
864types are an ideal simple RPC protocol: just exchange JSON datagrams. 977types are an ideal simple RPC protocol: just exchange JSON datagrams. See
978the C<json> write type description, above, for an actual example.
865 979
866=cut 980=cut
867 981
868register_read_type json => sub { 982register_read_type json => sub {
869 my ($self, $cb, $accept, $reject, $skip) = @_; 983 my ($self, $cb, $accept, $reject, $skip) = @_;
871 require JSON; 985 require JSON;
872 986
873 my $data; 987 my $data;
874 my $rbuf = \$self->{rbuf}; 988 my $rbuf = \$self->{rbuf};
875 989
876 my $json = $self->{json} ||= JSON::XS->new->utf8; 990 my $json = $self->{json} ||= JSON->new->utf8;
877 991
878 sub { 992 sub {
879 my $ref = $json->incr_parse ($self->{rbuf}); 993 my $ref = $json->incr_parse ($self->{rbuf});
880 994
881 if ($ref) { 995 if ($ref) {
939 $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { 1053 $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub {
940 my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; 1054 my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf};
941 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1055 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
942 1056
943 if ($len > 0) { 1057 if ($len > 0) {
1058 $self->{_activity} = AnyEvent->now;
1059
944 $self->{filter_r} 1060 $self->{filter_r}
945 ? $self->{filter_r}->($self, $rbuf) 1061 ? $self->{filter_r}->($self, $rbuf)
946 : $self->_drain_rbuf; 1062 : $self->_drain_rbuf;
947 1063
948 } elsif (defined $len) { 1064 } elsif (defined $len) {
949 delete $self->{_rw}; 1065 delete $self->{_rw};
950 $self->{_eof} = 1; 1066 $self->{_eof} = 1;
951 $self->_drain_rbuf; 1067 $self->_drain_rbuf;
952 1068
953 } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAWOULDBLOCK) { 1069 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) {
954 return $self->error; 1070 return $self->error;
955 } 1071 }
956 }); 1072 });
957 } 1073 }
958} 1074}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines