Browse Source

fix ssl fread

walkor 9 năm trước cách đây
mục cha
commit
f63c64934d
2 tập tin đã thay đổi với 14 bổ sung16 xóa
  1. 4 4
      Connection/AsyncTcpConnection.php
  2. 10 12
      Connection/TcpConnection.php

+ 4 - 4
Connection/AsyncTcpConnection.php

@@ -63,7 +63,7 @@ class AsyncTcpConnection extends TcpConnection
      *
      * @var string
      */
-    protected $_transport = 'tcp';
+    public $transport = 'tcp';
 
     /**
      * Construct.
@@ -86,7 +86,7 @@ class AsyncTcpConnection extends TcpConnection
                 }
             }
         } else {
-            $this->_transport = self::$_builtinTransports[$scheme];
+            $this->transport = self::$_builtinTransports[$scheme];
         }
         
         $this->_remoteAddress = substr($address, 2);
@@ -100,7 +100,7 @@ class AsyncTcpConnection extends TcpConnection
     public function connect()
     {
         // Open socket connection asynchronously.
-        $this->_socket = stream_socket_client("{$this->_transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
+        $this->_socket = stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
             STREAM_CLIENT_ASYNC_CONNECT);
         // If failed attempt to emit onError callback.
         if (!$this->_socket) {
@@ -159,7 +159,7 @@ class AsyncTcpConnection extends TcpConnection
             // Nonblocking.
             stream_set_blocking($socket, 0);
             // Try to open keepalive for tcp and disable Nagle algorithm.
-            if (function_exists('socket_import_stream') && $this->_transport === 'tcp') {
+            if (function_exists('socket_import_stream') && $this->transport === 'tcp') {
                 $raw_socket = socket_import_stream($socket);
                 socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1);
                 socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1);

+ 10 - 12
Connection/TcpConnection.php

@@ -211,6 +211,7 @@ class TcpConnection extends ConnectionInterface
         $this->id      = $this->_id = self::$_idRecorder++;
         $this->_socket = $socket;
         stream_set_blocking($this->_socket, 0);
+        stream_set_read_buffer($this->_socket, 0);
         Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
         $this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
         $this->_remoteAddress    = $remote_address;
@@ -354,24 +355,21 @@ class TcpConnection extends ConnectionInterface
      * Base read handler.
      *
      * @param resource $socket
+     * @param bool $check_eof
      * @return void
      */
     public function baseRead($socket, $check_eof = true)
     {
-        $read_data = false;
-        while (1) {
-            $buffer = fread($socket, self::READ_BUFFER_SIZE);
-            if ($buffer === '' || $buffer === false) {
-                break;
-            }
-            $read_data = true;
-            $this->_recvBuffer .= $buffer;
-        }
+        $buffer = fread($socket, self::READ_BUFFER_SIZE);
 
         // Check connection closed.
-        if (!$read_data && $check_eof) {
-            $this->destroy();
-            return;
+        if (!$buffer) {
+            if ($check_eof && (feof($socket) || !is_resource($socket) || $buffer === false)) {
+                $this->destroy();
+                return;
+            }
+        } else {
+            $this->_recvBuffer .= $buffer;
         }
 
         // If the application layer protocol has been set up.