… | |
… | |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | =cut |
18 | |
18 | |
19 | our $VERSION = 4.14; |
19 | our $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; |
… | |
… | |
242 | $self->{_activity} = AnyEvent->now; |
242 | $self->{_activity} = AnyEvent->now; |
243 | $self->_timeout; |
243 | $self->_timeout; |
244 | |
244 | |
245 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
245 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
246 | |
246 | |
|
|
247 | $self->start_read |
|
|
248 | if $self->{on_read}; |
|
|
249 | |
247 | $self |
250 | $self |
248 | } |
251 | } |
249 | |
252 | |
250 | sub _shutdown { |
253 | sub _shutdown { |
251 | my ($self) = @_; |
254 | my ($self) = @_; |
… | |
… | |
500 | =cut |
503 | =cut |
501 | |
504 | |
502 | register_write_type packstring => sub { |
505 | register_write_type packstring => sub { |
503 | my ($self, $format, $string) = @_; |
506 | my ($self, $format, $string) = @_; |
504 | |
507 | |
505 | pack "$format/a", $string |
508 | pack "$format/a*", $string |
506 | }; |
509 | }; |
507 | |
510 | |
508 | =item json => $array_or_hashref |
511 | =item json => $array_or_hashref |
509 | |
512 | |
510 | Encodes the given hash or array reference into a JSON object. Unless you |
513 | Encodes the given hash or array reference into a JSON object. Unless you |
… | |
… | |
542 | |
545 | |
543 | require JSON; |
546 | require JSON; |
544 | |
547 | |
545 | $self->{json} ? $self->{json}->encode ($ref) |
548 | $self->{json} ? $self->{json}->encode ($ref) |
546 | : JSON::encode_json ($ref) |
549 | : JSON::encode_json ($ref) |
|
|
550 | }; |
|
|
551 | |
|
|
552 | =item storable => $reference |
|
|
553 | |
|
|
554 | Freezes the given reference using L<Storable> and writes it to the |
|
|
555 | handle. Uses the C<nfreeze> format. |
|
|
556 | |
|
|
557 | =cut |
|
|
558 | |
|
|
559 | register_write_type storable => sub { |
|
|
560 | my ($self, $ref) = @_; |
|
|
561 | |
|
|
562 | require Storable; |
|
|
563 | |
|
|
564 | pack "w/a*", Storable::nfreeze ($ref) |
547 | }; |
565 | }; |
548 | |
566 | |
549 | =back |
567 | =back |
550 | |
568 | |
551 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
569 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
… | |
… | |
1045 | the C<json> write type description, above, for an actual example. |
1063 | the C<json> write type description, above, for an actual example. |
1046 | |
1064 | |
1047 | =cut |
1065 | =cut |
1048 | |
1066 | |
1049 | register_read_type json => sub { |
1067 | register_read_type json => sub { |
1050 | my ($self, $cb, $accept, $reject, $skip) = @_; |
1068 | my ($self, $cb) = @_; |
1051 | |
1069 | |
1052 | require JSON; |
1070 | require JSON; |
1053 | |
1071 | |
1054 | my $data; |
1072 | my $data; |
1055 | my $rbuf = \$self->{rbuf}; |
1073 | my $rbuf = \$self->{rbuf}; |
… | |
… | |
1067 | 1 |
1085 | 1 |
1068 | } else { |
1086 | } else { |
1069 | $self->{rbuf} = ""; |
1087 | $self->{rbuf} = ""; |
1070 | () |
1088 | () |
1071 | } |
1089 | } |
|
|
1090 | } |
|
|
1091 | }; |
|
|
1092 | |
|
|
1093 | =item storable => $cb->($handle, $ref) |
|
|
1094 | |
|
|
1095 | Deserialises a L<Storable> frozen representation as written by the |
|
|
1096 | C<storable> write type (BER-encoded length prefix followed by nfreeze'd |
|
|
1097 | data). |
|
|
1098 | |
|
|
1099 | Raises C<EBADMSG> error if the data could not be decoded. |
|
|
1100 | |
|
|
1101 | =cut |
|
|
1102 | |
|
|
1103 | register_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 | }); |
1072 | } |
1124 | } |
1073 | }; |
1125 | }; |
1074 | |
1126 | |
1075 | =back |
1127 | =back |
1076 | |
1128 | |