Skip to content

Commit

Permalink
Fixed #68
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Jan 13, 2018
1 parent ddc94fe commit 829b272
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Hprose/Socket/FullDuplexTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
* hprose socket FullDuplexTransporter class for php 5.3+ *
* *
* LastModified: Sep 17, 2016 *
* LastModified: Jan 14, 2014 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Hprose/Socket/HalfDuplexTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
* hprose socket HalfDuplexTransporter class for php 5.3+ *
* *
* LastModified: Sep 17, 2016 *
* LastModified: Jan 14, 2018 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/Hprose/Socket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
* hprose socket server library for php 5.3+ *
* *
* LastModified: Aug 6, 2016 *
* LastModified: Jan 14, 2018 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -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')) {
Expand Down
18 changes: 9 additions & 9 deletions src/Hprose/Socket/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
* hprose socket Service library for php 5.3+ *
* *
* LastModified: Dec 21, 2016 *
* LastModified: Jan 14, 2018 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down
49 changes: 26 additions & 23 deletions src/Hprose/Socket/Transporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
* hprose socket Transporter class for php 5.3+ *
* *
* LastModified: Dec 20, 2016 *
* LastModified: Jan 14, 2018 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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");
}
Expand Down

0 comments on commit 829b272

Please sign in to comment.