Skip to content

Commit

Permalink
close socket on exception; better detection of EOF vs. EOB
Browse files Browse the repository at this point in the history
  • Loading branch information
quipo committed Feb 16, 2013
1 parent 5f6fe67 commit 958febf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib/Kafka/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,15 @@ public function read($len, $verifyExactLength = false) {
while ($remainingBytes > 0) {
$chunk = fread($this->stream, $remainingBytes);
if ($chunk === false) {
$this->close();
throw new Kafka_Exception_Socket_EOF('Could not read '.$len.' bytes from stream (no data)');
}
if ($chunk == '' /* && feof($this->stream) */ ) {
break; // unexpected EOF
if (strlen($chunk) === 0) {
if (feof($this->stream)) {
$this->close();
throw new Kafka_Exception_Socket_EOF('Unexpected EOF whilst reading '.$len.' bytes from stream (no data)');
}
break; // end of ByteBuffer?
}
$data .= $chunk;
$remainingBytes -= strlen($chunk);
Expand All @@ -209,9 +214,11 @@ public function read($len, $verifyExactLength = false) {
if (false !== $readable) {
$res = stream_get_meta_data($this->stream);
if (!empty($res['timed_out'])) {
$this->close();
throw new Kafka_Exception_Socket_Timeout('Timed out reading '.$len.' bytes from stream');
}
}
$this->close();
throw new Kafka_Exception_Socket_EOF('Could not read '.$len.' bytes from stream (not readable)');
}

Expand Down

0 comments on commit 958febf

Please sign in to comment.