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.73 by root, Thu Jul 17 15:21:02 2008 UTC vs.
Revision 1.77 by root, Sun Jul 27 07:25:39 2008 UTC

75NOTE: The filehandle will be set to non-blocking (using 75NOTE: The filehandle will be set to non-blocking (using
76AnyEvent::Util::fh_nonblocking). 76AnyEvent::Util::fh_nonblocking).
77 77
78=item on_eof => $cb->($handle) 78=item on_eof => $cb->($handle)
79 79
80Set the callback to be called when an end-of-file condition is detcted, 80Set the callback to be called when an end-of-file condition is detected,
81i.e. in the case of a socket, when the other side has closed the 81i.e. in the case of a socket, when the other side has closed the
82connection cleanly. 82connection cleanly.
83 83
84While not mandatory, it is highly recommended to set an eof callback, 84While not mandatory, it is highly recommended to set an eof callback,
85otherwise you might end up with a closed socket while you are still 85otherwise you might end up with a closed socket while you are still
225You can also provide your own TLS connection object, but you have 225You can also provide your own TLS connection object, but you have
226to make sure that you call either C<Net::SSLeay::set_connect_state> 226to make sure that you call either C<Net::SSLeay::set_connect_state>
227or C<Net::SSLeay::set_accept_state> on it before you pass it to 227or C<Net::SSLeay::set_accept_state> on it before you pass it to
228AnyEvent::Handle. 228AnyEvent::Handle.
229 229
230See the C<starttls> method if you need to start TLs negotiation later. 230See the C<starttls> method if you need to start TLS negotiation later.
231 231
232=item tls_ctx => $ssl_ctx 232=item tls_ctx => $ssl_ctx
233 233
234Use the given Net::SSLeay::CTX object to create the new TLS connection 234Use the given Net::SSLeay::CTX object to create the new TLS connection
235(unless a connection object was specified directly). If this parameter is 235(unless a connection object was specified directly). If this parameter is
938=cut 938=cut
939 939
940register_read_type line => sub { 940register_read_type line => sub {
941 my ($self, $cb, $eol) = @_; 941 my ($self, $cb, $eol) = @_;
942 942
943 $eol = qr|(\015?\012)| if @_ < 3; 943 if (@_ < 3) {
944 # this is more than twice as fast as the generic code below
945 sub {
946 $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return;
947
948 $cb->($_[0], $1, $2);
949 1
950 }
951 } else {
944 $eol = quotemeta $eol unless ref $eol; 952 $eol = quotemeta $eol unless ref $eol;
945 $eol = qr|^(.*?)($eol)|s; 953 $eol = qr|^(.*?)($eol)|s;
946 954
947 sub { 955 sub {
948 $_[0]{rbuf} =~ s/$eol// or return; 956 $_[0]{rbuf} =~ s/$eol// or return;
949 957
950 $cb->($_[0], $1, $2); 958 $cb->($_[0], $1, $2);
959 1
951 1 960 }
952 } 961 }
953}; 962};
954 963
955# compatibility with older API 964# compatibility with older API
956sub push_read_line { 965sub push_read_line {
1090register_read_type packstring => sub { 1099register_read_type packstring => sub {
1091 my ($self, $cb, $format) = @_; 1100 my ($self, $cb, $format) = @_;
1092 1101
1093 sub { 1102 sub {
1094 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method 1103 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method
1095 defined (my $len = eval { unpack $format, $_[0]->{rbuf} }) 1104 defined (my $len = eval { unpack $format, $_[0]{rbuf} })
1096 or return; 1105 or return;
1097 1106
1107 $format = length pack $format, $len;
1108
1109 # bypass unshift if we already have the remaining chunk
1110 if ($format + $len <= length $_[0]{rbuf}) {
1111 my $data = substr $_[0]{rbuf}, $format, $len;
1112 substr $_[0]{rbuf}, 0, $format + $len, "";
1113 $cb->($_[0], $data);
1114 } else {
1098 # remove prefix 1115 # remove prefix
1099 substr $_[0]->{rbuf}, 0, (length pack $format, $len), ""; 1116 substr $_[0]{rbuf}, 0, $format, "";
1100 1117
1101 # read rest 1118 # read remaining chunk
1102 $_[0]->unshift_read (chunk => $len, $cb); 1119 $_[0]->unshift_read (chunk => $len, $cb);
1120 }
1103 1121
1104 1 1122 1
1105 } 1123 }
1106}; 1124};
1107 1125
1164 1182
1165 require Storable; 1183 require Storable;
1166 1184
1167 sub { 1185 sub {
1168 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method 1186 # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method
1169 defined (my $len = eval { unpack "w", $_[0]->{rbuf} }) 1187 defined (my $len = eval { unpack "w", $_[0]{rbuf} })
1170 or return; 1188 or return;
1171 1189
1190 my $format = length pack "w", $len;
1191
1192 # bypass unshift if we already have the remaining chunk
1193 if ($format + $len <= length $_[0]{rbuf}) {
1194 my $data = substr $_[0]{rbuf}, $format, $len;
1195 substr $_[0]{rbuf}, 0, $format + $len, "";
1196 $cb->($_[0], Storable::thaw ($data));
1197 } else {
1172 # remove prefix 1198 # remove prefix
1173 substr $_[0]->{rbuf}, 0, (length pack "w", $len), ""; 1199 substr $_[0]{rbuf}, 0, $format, "";
1174 1200
1175 # read rest 1201 # read remaining chunk
1176 $_[0]->unshift_read (chunk => $len, sub { 1202 $_[0]->unshift_read (chunk => $len, sub {
1177 if (my $ref = eval { Storable::thaw ($_[1]) }) { 1203 if (my $ref = eval { Storable::thaw ($_[1]) }) {
1178 $cb->($_[0], $ref); 1204 $cb->($_[0], $ref);
1179 } else { 1205 } else {
1180 $self->_error (&Errno::EBADMSG); 1206 $self->_error (&Errno::EBADMSG);
1207 }
1181 } 1208 });
1182 }); 1209 }
1210
1211 1
1183 } 1212 }
1184}; 1213};
1185 1214
1186=back 1215=back
1187 1216
1448=over 4 1477=over 4
1449 1478
1450=item * all constructor arguments become object members. 1479=item * all constructor arguments become object members.
1451 1480
1452At least initially, when you pass a C<tls>-argument to the constructor it 1481At least initially, when you pass a C<tls>-argument to the constructor it
1453will end up in C<< $handle->{tls} >>. Those members might be changes or 1482will end up in C<< $handle->{tls} >>. Those members might be changed or
1454mutated later on (for example C<tls> will hold the TLS connection object). 1483mutated later on (for example C<tls> will hold the TLS connection object).
1455 1484
1456=item * other object member names are prefixed with an C<_>. 1485=item * other object member names are prefixed with an C<_>.
1457 1486
1458All object members not explicitly documented (internal use) are prefixed 1487All object members not explicitly documented (internal use) are prefixed

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines