Skip to content

Commit 79e06e2

Browse files
committed
Raise a peer.ProtocolException if reading the header failed
First part of #9
1 parent 3a8d112 commit 79e06e2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/php/com/mongodb/Protocol.class.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace com\mongodb;
22

33
use lang\{FormatException, Throwable};
4-
use peer\{Socket, ConnectException};
4+
use peer\{Socket, ConnectException, ProtocolException};
55

66
/**
77
* MongoDB Wire Protocol
@@ -179,14 +179,18 @@ private function read($bytes) {
179179

180180
public function send($operation, $body) {
181181
$this->id > 2147483647 ? $this->id= 1 : $this->id++;
182-
183182
$payload= pack('VVVV', strlen($body) + 16, $this->id, 0, $operation).$body;
184183

185184
// \util\cmd\Console::writeLine('>>> ', strlen($payload), ': ', $this->hex($payload));
186185
$this->conn->write($payload);
187186

188187
$header= unpack('VmessageLength/VrequestID/VresponseTo/VopCode', $this->read(16));
189188
// \util\cmd\Console::writeLine('<<< ', $header);
189+
if (false === $header) {
190+
$e= new ProtocolException($this->conn->eof() ? 'Received EOF while reading' : 'Reading header failed');
191+
\xp::gc(__FILE__);
192+
throw $e;
193+
}
190194

191195
$response= $this->read($header['messageLength'] - 16);
192196
// \util\cmd\Console::writeLine('<<< ', strlen($response), ': ', $this->hex($response));

0 commit comments

Comments
 (0)