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.61 by root, Fri Jun 6 10:23:50 2008 UTC vs.
Revision 1.66 by root, Fri Jun 6 15:32:54 2008 UTC

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
17=cut 17=cut
18 18
19our $VERSION = 4.14; 19our $VERSION = 4.15;
20 20
21=head1 SYNOPSIS 21=head1 SYNOPSIS
22 22
23 use AnyEvent; 23 use AnyEvent;
24 use AnyEvent::Handle; 24 use AnyEvent::Handle;
167 167
168Sets the amount of bytes (default: C<0>) that make up an "empty" write 168Sets the amount of bytes (default: C<0>) that make up an "empty" write
169buffer: If the write reaches this size or gets even samller it is 169buffer: If the write reaches this size or gets even samller it is
170considered empty. 170considered empty.
171 171
172=item linger => <seconds>
173
174If non-zero (default: C<3600>), then the destructor of the
175AnyEvent::Handle object will check wether there is still outstanding write
176data and will install a watcher that will write out this data. No errors
177will be reported (this mostly matches how the operating system treats
178outstanding data at socket close time).
179
180This will not work for partial TLS data that could not yet been
181encoded. This data will be lost.
182
172=item tls => "accept" | "connect" | Net::SSLeay::SSL object 183=item tls => "accept" | "connect" | Net::SSLeay::SSL object
173 184
174When this parameter is given, it enables TLS (SSL) mode, that means it 185When this parameter is given, it enables TLS (SSL) mode, that means it
175will start making tls handshake and will transparently encrypt/decrypt 186will start making tls handshake and will transparently encrypt/decrypt
176data. 187data.
230 241
231 $self->{_activity} = AnyEvent->now; 242 $self->{_activity} = AnyEvent->now;
232 $self->_timeout; 243 $self->_timeout;
233 244
234 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 245 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
246
247 $self->start_read
248 if $self->{on_read} || @{ $self->{_queue} };
235 249
236 $self 250 $self
237} 251}
238 252
239sub _shutdown { 253sub _shutdown {
489=cut 503=cut
490 504
491register_write_type packstring => sub { 505register_write_type packstring => sub {
492 my ($self, $format, $string) = @_; 506 my ($self, $format, $string) = @_;
493 507
494 pack "$format/a", $string 508 pack "$format/a*", $string
495}; 509};
496 510
497=item json => $array_or_hashref 511=item json => $array_or_hashref
498 512
499Encodes the given hash or array reference into a JSON object. Unless you 513Encodes the given hash or array reference into a JSON object. Unless you
531 545
532 require JSON; 546 require JSON;
533 547
534 $self->{json} ? $self->{json}->encode ($ref) 548 $self->{json} ? $self->{json}->encode ($ref)
535 : JSON::encode_json ($ref) 549 : JSON::encode_json ($ref)
550};
551
552=item storable => $reference
553
554Freezes the given reference using L<Storable> and writes it to the
555handle. Uses the C<nfreeze> format.
556
557=cut
558
559register_write_type storable => sub {
560 my ($self, $ref) = @_;
561
562 require Storable;
563
564 pack "w/a*", Storable::nfreeze ($ref)
536}; 565};
537 566
538=back 567=back
539 568
540=item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) 569=item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args)
1034the C<json> write type description, above, for an actual example. 1063the C<json> write type description, above, for an actual example.
1035 1064
1036=cut 1065=cut
1037 1066
1038register_read_type json => sub { 1067register_read_type json => sub {
1039 my ($self, $cb, $accept, $reject, $skip) = @_; 1068 my ($self, $cb) = @_;
1040 1069
1041 require JSON; 1070 require JSON;
1042 1071
1043 my $data; 1072 my $data;
1044 my $rbuf = \$self->{rbuf}; 1073 my $rbuf = \$self->{rbuf};
1056 1 1085 1
1057 } else { 1086 } else {
1058 $self->{rbuf} = ""; 1087 $self->{rbuf} = "";
1059 () 1088 ()
1060 } 1089 }
1090 }
1091};
1092
1093=item storable => $cb->($handle, $ref)
1094
1095Deserialises a L<Storable> frozen representation as written by the
1096C<storable> write type (BER-encoded length prefix followed by nfreeze'd
1097data).
1098
1099Raises C<EBADMSG> error if the data could not be decoded.
1100
1101=cut
1102
1103register_read_type storable => sub {
1104 my ($self, $cb) = @_;
1105
1106 require Storable;
1107
1108 sub {
1109 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method
1110 defined (my $len = eval { unpack "w", $_[0]->{rbuf} })
1111 or return;
1112
1113 # remove prefix
1114 substr $_[0]->{rbuf}, 0, (length pack "w", $len), "";
1115
1116 # read rest
1117 $_[0]->unshift_read (chunk => $len, sub {
1118 if (my $ref = eval { Storable::thaw ($_[1]) }) {
1119 $cb->($_[0], $ref);
1120 } else {
1121 $self->_error (&Errno::EBADMSG);
1122 }
1123 });
1061 } 1124 }
1062}; 1125};
1063 1126
1064=back 1127=back
1065 1128
1253 1316
1254sub DESTROY { 1317sub DESTROY {
1255 my $self = shift; 1318 my $self = shift;
1256 1319
1257 $self->stoptls; 1320 $self->stoptls;
1321
1322 my $linger = exists $self->{linger} ? $self->{linger} : 3600;
1323
1324 if ($linger && length $self->{wbuf}) {
1325 my $fh = delete $self->{fh};
1326 my $wbuf = delete $self->{wbuf};
1327
1328 my @linger;
1329
1330 push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub {
1331 my $len = syswrite $fh, $wbuf, length $wbuf;
1332
1333 if ($len > 0) {
1334 substr $wbuf, 0, $len, "";
1335 } else {
1336 @linger = (); # end
1337 }
1338 });
1339 push @linger, AnyEvent->timer (after => $linger, cb => sub {
1340 @linger = ();
1341 });
1342 }
1258} 1343}
1259 1344
1260=item AnyEvent::Handle::TLS_CTX 1345=item AnyEvent::Handle::TLS_CTX
1261 1346
1262This function creates and returns the Net::SSLeay::CTX object used by 1347This function creates and returns the Net::SSLeay::CTX object used by

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines