… | |
… | |
1102 | sub { |
1102 | sub { |
1103 | # 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 |
1104 | defined (my $len = eval { unpack $format, $_[0]{rbuf} }) |
1104 | defined (my $len = eval { unpack $format, $_[0]{rbuf} }) |
1105 | or return; |
1105 | or return; |
1106 | |
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 { |
1107 | # remove prefix |
1115 | # remove prefix |
1108 | substr $_[0]{rbuf}, 0, (length pack $format, $len), ""; |
1116 | substr $_[0]{rbuf}, 0, $format, ""; |
1109 | |
1117 | |
1110 | # read rest |
1118 | # read remaining chunk |
1111 | $_[0]->unshift_read (chunk => $len, $cb); |
1119 | $_[0]->unshift_read (chunk => $len, $cb); |
|
|
1120 | } |
1112 | |
1121 | |
1113 | 1 |
1122 | 1 |
1114 | } |
1123 | } |
1115 | }; |
1124 | }; |
1116 | |
1125 | |
… | |
… | |
1176 | sub { |
1185 | sub { |
1177 | # 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 |
1178 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1187 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1179 | or return; |
1188 | or return; |
1180 | |
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 { |
1181 | # remove prefix |
1198 | # remove prefix |
1182 | substr $_[0]{rbuf}, 0, (length pack "w", $len), ""; |
1199 | substr $_[0]{rbuf}, 0, $format, ""; |
1183 | |
1200 | |
1184 | # read rest |
1201 | # read remaining chunk |
1185 | $_[0]->unshift_read (chunk => $len, sub { |
1202 | $_[0]->unshift_read (chunk => $len, sub { |
1186 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1203 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1187 | $cb->($_[0], $ref); |
1204 | $cb->($_[0], $ref); |
1188 | } else { |
1205 | } else { |
1189 | $self->_error (&Errno::EBADMSG); |
1206 | $self->_error (&Errno::EBADMSG); |
|
|
1207 | } |
1190 | } |
1208 | }); |
1191 | }); |
1209 | } |
|
|
1210 | |
|
|
1211 | 1 |
1192 | } |
1212 | } |
1193 | }; |
1213 | }; |
1194 | |
1214 | |
1195 | =back |
1215 | =back |
1196 | |
1216 | |