From 829b272af50ea216a31d8d41d7299e5953ec43e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A9=AC=E5=93=A5?= Date: Sun, 14 Jan 2018 00:23:28 +0800 Subject: [PATCH] Fixed #68 --- src/Hprose/Socket/FullDuplexTransporter.php | 4 +- src/Hprose/Socket/HalfDuplexTransporter.php | 4 +- src/Hprose/Socket/Server.php | 6 +-- src/Hprose/Socket/Service.php | 18 ++++---- src/Hprose/Socket/Transporter.php | 49 +++++++++++---------- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/Hprose/Socket/FullDuplexTransporter.php b/src/Hprose/Socket/FullDuplexTransporter.php index bd309810..71732045 100644 --- a/src/Hprose/Socket/FullDuplexTransporter.php +++ b/src/Hprose/Socket/FullDuplexTransporter.php @@ -14,7 +14,7 @@ * * * hprose socket FullDuplexTransporter class for php 5.3+ * * * - * LastModified: Sep 17, 2016 * + * LastModified: Jan 14, 2014 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -56,7 +56,7 @@ protected function asyncReadError($o, $stream, $index = -1) { } unset($o->queue[$stream_id]); unset($o->responses[$stream_id]); - @fclose($stream); + fclose($stream); $this->removeStream($stream, $o->readpool); $this->removeStream($stream, $o->writepool); } diff --git a/src/Hprose/Socket/HalfDuplexTransporter.php b/src/Hprose/Socket/HalfDuplexTransporter.php index 6e63d84a..8384ca64 100644 --- a/src/Hprose/Socket/HalfDuplexTransporter.php +++ b/src/Hprose/Socket/HalfDuplexTransporter.php @@ -14,7 +14,7 @@ * * * hprose socket HalfDuplexTransporter class for php 5.3+ * * * - * LastModified: Sep 17, 2016 * + * LastModified: Jan 14, 2018 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -43,7 +43,7 @@ protected function asyncReadError($o, $stream, $index) { $this->free($o, $index); } unset($o->responses[(integer)$stream]); - @fclose($stream); + fclose($stream); $this->removeStream($stream, $o->readpool); } protected function getBodyLength($stream) { diff --git a/src/Hprose/Socket/Server.php b/src/Hprose/Socket/Server.php index 9f604260..e0dfd27f 100644 --- a/src/Hprose/Socket/Server.php +++ b/src/Hprose/Socket/Server.php @@ -14,7 +14,7 @@ * * * hprose socket server library for php 5.3+ * * * - * LastModified: Aug 6, 2016 * + * LastModified: Jan 14, 2018 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -57,8 +57,8 @@ private function createSocketServer($uri) { } $errno = 0; $errstr = ''; - $context = @stream_context_create($this->settings); - $server = @stream_socket_server($uri, $errno, $errstr, + $context = stream_context_create($this->settings); + $server = stream_socket_server($uri, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); if (function_exists('socket_import_stream')) { if (($scheme === 'tcp') || ($scheme === 'unix')) { diff --git a/src/Hprose/Socket/Service.php b/src/Hprose/Socket/Service.php index ab576089..78d21f96 100644 --- a/src/Hprose/Socket/Service.php +++ b/src/Hprose/Socket/Service.php @@ -14,7 +14,7 @@ * * * hprose socket Service library for php 5.3+ * * * - * LastModified: Dec 21, 2016 * + * LastModified: Jan 14, 2018 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -144,7 +144,7 @@ private function getOnSend($server, $socket) { $self->removeSocket($sockets, $socket); } else { - $sent = @fwrite($socket, $bytes, $len); + $sent = fwrite($socket, $bytes, $len); if ($sent === false) { $self->error($server, $socket, 'Unknown write error'); } @@ -178,7 +178,7 @@ private function getOnReceive($server, $socket) { $userFatalErrorHandler = &$this->userFatalErrorHandler; return function() use ($self, $server, $socket, &$bytes, &$headerLength, &$dataLength, &$id, &$userFatalErrorHandler, $send) { - $data = @fread($socket, $self->readBuffer); + $data = fread($socket, $self->readBuffer); if ($data === false) { $self->error($server, $socket, 'Unknown read error'); return; @@ -226,14 +226,14 @@ private function getOnReceive($server, $socket) { }; } private function accept($server) { - $socket = @stream_socket_accept($server, 0); + $socket = stream_socket_accept($server, 0); if ($socket === false) return; - if (@stream_set_blocking($socket, false) === false) { + if (stream_set_blocking($socket, false) === false) { $this->error($server, $socket, 'Unkown error'); return; } - @stream_set_read_buffer($socket, $this->readBuffer); - @stream_set_write_buffer($socket, $this->writeBuffer); + stream_set_read_buffer($socket, $this->readBuffer); + stream_set_write_buffer($socket, $this->writeBuffer); $onAccept = $this->onAccept; if (is_callable($onAccept)) { try { @@ -264,7 +264,7 @@ private function close($socket, $context) { $this->removeSocket($this->readableSockets, $socket); unset($this->onReceives[(int)$socket]); unset($this->onSends[(int)$socket]); - @stream_socket_shutdown($socket, STREAM_SHUT_RDWR); + stream_socket_shutdown($socket, STREAM_SHUT_RDWR); $onClose = $this->onClose; if (is_callable($onClose)) { try { @@ -318,7 +318,7 @@ public function handle($servers) { $read = array_values($readableSockets); $write = array_values($writeableSockets); $except = NULL; - $n = @stream_select($read, $write, $except, $sec, $usec); + $n = stream_select($read, $write, $except, $sec, $usec); if ($n === false) { foreach ($servers as $server) { $this->error($server, $server, 'Unknown select error'); diff --git a/src/Hprose/Socket/Transporter.php b/src/Hprose/Socket/Transporter.php index 93d2f2d4..aa7cf6b0 100644 --- a/src/Hprose/Socket/Transporter.php +++ b/src/Hprose/Socket/Transporter.php @@ -14,7 +14,7 @@ * * * hprose socket Transporter class for php 5.3+ * * * - * LastModified: Dec 20, 2016 * + * LastModified: Jan 14, 2018 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -47,7 +47,7 @@ public function __construct(Client $client, $async) { $this->async = $async; } public function __destruct() { - if ($this->stream !== null) @fclose($this->stream); + if ($this->stream !== null) fclose($this->stream); } protected function getLastError($error) { $e = error_get_last(); @@ -67,7 +67,10 @@ protected function removeStream($stream, &$pool) { protected function readHeader($stream, $n) { $header = ''; do { - $buffer = @fread($stream, $n - strlen($header)); + $buffer = fread($stream, $n - strlen($header)); + if ($buffer === false) { + return false; + } $header .= $buffer; } while (!empty($buffer) && (strlen($header) < $n)); if (strlen($header) < $n) { @@ -97,11 +100,11 @@ protected function asyncWrite($stream, $o) { return; } } - $sent = @fwrite($stream, $request->buffer, $request->length); + $sent = fwrite($stream, $request->buffer, $request->length); if ($sent === false) { $o->results[$request->index]->reject($this->getLastError('request write error')); $this->free($o, $request->index); - @fclose($stream); + fclose($stream); $this->removeStream($stream, $o->writepool); return; } @@ -124,7 +127,7 @@ private function asyncRead($stream, $o) { return; } $remaining = $response->length - strlen($response->buffer); - $buffer = @fread($stream, $remaining); + $buffer = fread($stream, $remaining); if (empty($buffer)) { $this->asyncReadError($o, $stream, $response->index); return; @@ -146,7 +149,7 @@ private function asyncRead($stream, $o) { private function removeStreamById($stream_id, &$pool) { foreach ($pool as $index => $stream) { if ((integer)$stream == $stream_id) { - @fclose($stream); + fclose($stream); unset($pool[$index]); return; } @@ -185,14 +188,14 @@ private function createPool($client, $o) { $pool = array(); $errno = 0; $errstr = ''; - $context = @stream_context_create($client->options); + $context = stream_context_create($client->options); for ($i = 0; $i < $n; $i++) { $scheme = parse_url($client->uri, PHP_URL_SCHEME); if ($scheme == 'unix') { - $stream = @pfsockopen('unix://' . parse_url($client->uri, PHP_URL_PATH)); + $stream = pfsockopen('unix://' . parse_url($client->uri, PHP_URL_PATH)); } else { - $stream = @stream_socket_client( + $stream = stream_socket_client( $client->uri . '/' . $i, $errno, $errstr, @@ -202,9 +205,9 @@ private function createPool($client, $o) { ); } if (($stream !== false) && - (@stream_set_blocking($stream, false) !== false)) { - @stream_set_read_buffer($stream, $client->readBuffer); - @stream_set_write_buffer($stream, $client->writeBuffer); + (stream_set_blocking($stream, false) !== false)) { + stream_set_read_buffer($stream, $client->readBuffer); + stream_set_write_buffer($stream, $client->writeBuffer); if (function_exists('socket_import_stream')) { if (($scheme === 'tcp') || ($scheme === 'unix')) { $socket = socket_import_stream($stream); @@ -255,7 +258,7 @@ public function loop() { $timeout = max(0, min($o->deadlines) - microtime(true)); $tv_sec = floor($timeout); $tv_usec = ($timeout - $tv_sec) * 1000; - $n = @stream_select($read, $write, $except, $tv_sec, $tv_usec); + $n = stream_select($read, $write, $except, $tv_sec, $tv_usec); if ($n === false) { $e = $this->getLastError('unkown io error.'); foreach ($o->results as $result) { @@ -273,8 +276,8 @@ public function loop() { $o->writepool = $this->createPool($client, $o); } } - foreach ($o->writepool as $stream) @fclose($stream); - foreach ($o->readpool as $stream) @fclose($stream); + foreach ($o->writepool as $stream) fclose($stream); + foreach ($o->readpool as $stream) fclose($stream); } } public function asyncSendAndReceive($buffer, stdClass $context) { @@ -289,7 +292,7 @@ private function write($stream, $request) { $buffer = $this->appendHeader($request); $length = strlen($buffer); while (true) { - $sent = @fwrite($stream, $buffer, $length); + $sent = fwrite($stream, $buffer, $length); if ($sent === false) { return false; } @@ -307,7 +310,7 @@ private function read($stream) { if ($length === false) return false; $response = ''; while (($remaining = $length - strlen($response)) > 0) { - $buffer = @fread($stream, $remaining); + $buffer = fread($stream, $remaining); if ($buffer === false) { return false; } @@ -327,10 +330,10 @@ public function syncSendAndReceive($buffer, stdClass $context) { $scheme = parse_url($client->uri, PHP_URL_SCHEME); if ($this->stream === null) { if ($scheme == 'unix') { - $this->stream = @pfsockopen('unix://' . parse_url($client->uri, PHP_URL_PATH)); + $this->stream = pfsockopen('unix://' . parse_url($client->uri, PHP_URL_PATH)); } else { - $this->stream = @stream_socket_client( + $this->stream = stream_socket_client( $client->uri, $errno, $errstr, @@ -345,8 +348,8 @@ public function syncSendAndReceive($buffer, stdClass $context) { } } $stream = $this->stream; - @stream_set_read_buffer($stream, $client->readBuffer); - @stream_set_write_buffer($stream, $client->writeBuffer); + stream_set_read_buffer($stream, $client->readBuffer); + stream_set_write_buffer($stream, $client->writeBuffer); if (function_exists('socket_import_stream')) { if (($scheme === 'tcp') || ($scheme === 'unix')) { $socket = socket_import_stream($stream); @@ -356,7 +359,7 @@ public function syncSendAndReceive($buffer, stdClass $context) { } } } - if (@stream_set_timeout($stream, $sec, $usec) == false) { + if (stream_set_timeout($stream, $sec, $usec) == false) { if ($trycount > 0) { throw $this->getLastError("unknown error"); }