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.220 by root, Sun Jul 24 13:10:43 2011 UTC vs.
Revision 1.224 by root, Mon Sep 5 07:21:54 2011 UTC

11 11
12 my $hdl; $hdl = new AnyEvent::Handle 12 my $hdl; $hdl = new AnyEvent::Handle
13 fh => \*STDIN, 13 fh => \*STDIN,
14 on_error => sub { 14 on_error => sub {
15 my ($hdl, $fatal, $msg) = @_; 15 my ($hdl, $fatal, $msg) = @_;
16 warn "got error $msg\n"; 16 AE::log warn => "got error $msg\n";
17 $hdl->destroy; 17 $hdl->destroy;
18 $cv->send; 18 $cv->send;
19 }; 19 };
20 20
21 # send some request line 21 # send some request line
22 $hdl->push_write ("getinfo\015\012"); 22 $hdl->push_write ("getinfo\015\012");
23 23
24 # read the response line 24 # read the response line
25 $hdl->push_read (line => sub { 25 $hdl->push_read (line => sub {
26 my ($hdl, $line) = @_; 26 my ($hdl, $line) = @_;
27 warn "got line <$line>\n"; 27 AE::log warn => "got line <$line>\n";
28 $cv->send; 28 $cv->send;
29 }); 29 });
30 30
31 $cv->recv; 31 $cv->recv;
32 32
359already have occured on BSD systems), but at least it will protect you 359already have occured on BSD systems), but at least it will protect you
360from most attacks. 360from most attacks.
361 361
362=item read_size => <bytes> 362=item read_size => <bytes>
363 363
364The initial read block size, the number of bytes this module will try to 364The initial read block size, the number of bytes this module will try
365read during each loop iteration. Each handle object will consume at least 365to read during each loop iteration. Each handle object will consume
366this amount of memory for the read buffer as well, so when handling many 366at least this amount of memory for the read buffer as well, so when
367connections requirements). See also C<max_read_size>. Default: C<2048>. 367handling many connections watch out for memory requirements). See also
368C<max_read_size>. Default: C<2048>.
368 369
369=item max_read_size => <bytes> 370=item max_read_size => <bytes>
370 371
371The maximum read buffer size used by the dynamic adjustment 372The maximum read buffer size used by the dynamic adjustment
372algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in 373algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in
1080=cut 1081=cut
1081 1082
1082register_write_type storable => sub { 1083register_write_type storable => sub {
1083 my ($self, $ref) = @_; 1084 my ($self, $ref) = @_;
1084 1085
1085 require Storable; 1086 require Storable unless $Storable::VERSION;
1086 1087
1087 pack "w/a*", Storable::nfreeze ($ref) 1088 pack "w/a*", Storable::nfreeze ($ref)
1088}; 1089};
1089 1090
1090=back 1091=back
1127 1128
1128Whenever the given C<type> is used, C<push_write> will the function with 1129Whenever the given C<type> is used, C<push_write> will the function with
1129the handle object and the remaining arguments. 1130the handle object and the remaining arguments.
1130 1131
1131The function is supposed to return a single octet string that will be 1132The function is supposed to return a single octet string that will be
1132appended to the write buffer, so you cna mentally treat this function as a 1133appended to the write buffer, so you can mentally treat this function as a
1133"arguments to on-the-wire-format" converter. 1134"arguments to on-the-wire-format" converter.
1134 1135
1135Example: implement a custom write type C<join> that joins the remaining 1136Example: implement a custom write type C<join> that joins the remaining
1136arguments using the first one. 1137arguments using the first one.
1137 1138
1431data. 1432data.
1432 1433
1433Example: read 2 bytes. 1434Example: read 2 bytes.
1434 1435
1435 $handle->push_read (chunk => 2, sub { 1436 $handle->push_read (chunk => 2, sub {
1436 warn "yay ", unpack "H*", $_[1]; 1437 AE::log debug => "yay " . unpack "H*", $_[1];
1437 }); 1438 });
1438 1439
1439=cut 1440=cut
1440 1441
1441register_read_type chunk => sub { 1442register_read_type chunk => sub {
1710=cut 1711=cut
1711 1712
1712register_read_type storable => sub { 1713register_read_type storable => sub {
1713 my ($self, $cb) = @_; 1714 my ($self, $cb) = @_;
1714 1715
1715 require Storable; 1716 require Storable unless $Storable::VERSION;
1716 1717
1717 sub { 1718 sub {
1718 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method 1719 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method
1719 defined (my $len = eval { unpack "w", $_[0]{rbuf} }) 1720 defined (my $len = eval { unpack "w", $_[0]{rbuf} })
1720 or return; 1721 or return;
2203Probably because your C<on_error> callback is being called instead: When 2204Probably because your C<on_error> callback is being called instead: When
2204you have outstanding requests in your read queue, then an EOF is 2205you have outstanding requests in your read queue, then an EOF is
2205considered an error as you clearly expected some data. 2206considered an error as you clearly expected some data.
2206 2207
2207To avoid this, make sure you have an empty read queue whenever your handle 2208To avoid this, make sure you have an empty read queue whenever your handle
2208is supposed to be "idle" (i.e. connection closes are O.K.). You cna set 2209is supposed to be "idle" (i.e. connection closes are O.K.). You can set
2209an C<on_read> handler that simply pushes the first read requests in the 2210an C<on_read> handler that simply pushes the first read requests in the
2210queue. 2211queue.
2211 2212
2212See also the next question, which explains this in a bit more detail. 2213See also the next question, which explains this in a bit more detail.
2213 2214
2244some data and raises the C<EPIPE> error when the connction is dropped 2245some data and raises the C<EPIPE> error when the connction is dropped
2245unexpectedly. 2246unexpectedly.
2246 2247
2247The second variant is a protocol where the client can drop the connection 2248The second variant is a protocol where the client can drop the connection
2248at any time. For TCP, this means that the server machine may run out of 2249at any time. For TCP, this means that the server machine may run out of
2249sockets easier, and in general, it means you cnanot distinguish a protocl 2250sockets easier, and in general, it means you cannot distinguish a protocl
2250failure/client crash from a normal connection close. Nevertheless, these 2251failure/client crash from a normal connection close. Nevertheless, these
2251kinds of protocols are common (and sometimes even the best solution to the 2252kinds of protocols are common (and sometimes even the best solution to the
2252problem). 2253problem).
2253 2254
2254Having an outstanding read request at all times is possible if you ignore 2255Having an outstanding read request at all times is possible if you ignore
2329C<low_water_mark> this will be called precisely when all data has been 2330C<low_water_mark> this will be called precisely when all data has been
2330written to the socket: 2331written to the socket:
2331 2332
2332 $handle->push_write (...); 2333 $handle->push_write (...);
2333 $handle->on_drain (sub { 2334 $handle->on_drain (sub {
2334 warn "all data submitted to the kernel\n"; 2335 AE::log debug => "all data submitted to the kernel\n";
2335 undef $handle; 2336 undef $handle;
2336 }); 2337 });
2337 2338
2338If you just want to queue some data and then signal EOF to the other side, 2339If you just want to queue some data and then signal EOF to the other side,
2339consider using C<< ->push_shutdown >> instead. 2340consider using C<< ->push_shutdown >> instead.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines