Skip to content

Commit

Permalink
Improved socket error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Sep 29, 2020
1 parent 67c6630 commit 1559972
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Socket/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function sendCommand($dataIn, $flagsSend = 0, $flagsReceive = MSG_WAITALL
$this->connect();

if (false === socket_send($this->socket, $dataIn, strlen($dataIn), $flagsSend)) {
throw new SocketException('Writing to socket failed');
throw new SocketException('Writing to socket failed', socket_last_error());
}
$dataOut = '';
while ($bytes = socket_recv($this->socket, $chunk, self::MAX_READ_BYTES, $flagsReceive)) {
Expand All @@ -67,11 +67,12 @@ public function sendCommand($dataIn, $flagsSend = 0, $flagsReceive = MSG_WAITALL
*/
private function connect()
{
if (!is_resource($this->socket)) {
if (!is_resource($this->socket) || get_resource_type($this->socket) !== 'Socket') {
$this->socket = @ socket_create($this->socketType, SOCK_STREAM, 0);
if ($this->socket === false) {
throw new SocketException('Creating socket failed', socket_last_error());
}

$hasError = @ socket_connect($this->socket, ...$this->connectionArguments);
if ($hasError === false) {
throw new SocketException('Connecting to socket failed', socket_last_error());
Expand Down

0 comments on commit 1559972

Please sign in to comment.