Browse Source

set error handler

walkor 7 năm trước cách đây
mục cha
commit
1fa7bef652
1 tập tin đã thay đổi với 32 bổ sung17 xóa
  1. 32 17
      Connection/TcpConnection.php

+ 32 - 17
Connection/TcpConnection.php

@@ -260,6 +260,7 @@ class TcpConnection extends ConnectionInterface
      *
      * @param string $name
      * @param array  $arguments
+     * @return void
      */
     public function __call($name, $arguments) {
         // Try to emit custom function within protocol
@@ -273,10 +274,7 @@ class TcpConnection extends ConnectionInterface
                 Worker::log($e);
                 exit(250);
             }
-	} else {
-	    trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);
-	}
-
+        }
     }
 
     /**
@@ -299,8 +297,8 @@ class TcpConnection extends ConnectionInterface
             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;
+        $this->maxSendBufferSize        = self::$defaultMaxSendBufferSize;
+        $this->_remoteAddress           = $remote_address;
         static::$connections[$this->id] = $this;
     }
 
@@ -358,7 +356,9 @@ class TcpConnection extends ConnectionInterface
 
         // Attempt to send data directly.
         if ($this->_sendBuffer === '') {
-            $len = @fwrite($this->_socket, $send_buffer, 8192);
+            set_error_handler(function(){});
+            $len = fwrite($this->_socket, $send_buffer, 8192);
+            restore_error_handler();
             // send successful.
             if ($len === strlen($send_buffer)) {
                 $this->bytesWritten += $len;
@@ -575,7 +575,9 @@ class TcpConnection extends ConnectionInterface
             }
         }
 
-        $buffer = @fread($socket, self::READ_BUFFER_SIZE);
+        set_error_handler(function(){});
+        $buffer = fread($socket, self::READ_BUFFER_SIZE);
+        restore_error_handler();
 
         // Check connection closed.
         if ($buffer === '' || $buffer === false) {
@@ -600,7 +602,9 @@ class TcpConnection extends ConnectionInterface
                     }
                 } else {
                     // Get current package length.
+                    set_error_handler(null);
                     $this->_currentPackageLength = $parser::input($this->_recvBuffer, $this);
+                    restore_error_handler();
                     // The packet length is unknown.
                     if ($this->_currentPackageLength === 0) {
                         break;
@@ -678,7 +682,9 @@ class TcpConnection extends ConnectionInterface
      */
     public function baseWrite()
     {
-        $len = @fwrite($this->_socket, $this->_sendBuffer, 8192);
+        set_error_handler(function(){});
+        $len = fwrite($this->_socket, $this->_sendBuffer, 8192);
+        restore_error_handler();
         if ($len === strlen($this->_sendBuffer)) {
             $this->bytesWritten += $len;
             Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
@@ -720,18 +726,23 @@ class TcpConnection extends ConnectionInterface
             $this->destroy();
             return false;
         }
-        $async=$this instanceof AsyncTcpConnection;
+        $async = $this instanceof AsyncTcpConnection;
         if($async){
-            $type=STREAM_CRYPTO_METHOD_SSLv2_CLIENT | STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
+            $type = STREAM_CRYPTO_METHOD_SSLv2_CLIENT | STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
         }else{
-            $type=STREAM_CRYPTO_METHOD_SSLv2_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER;
+            $type = STREAM_CRYPTO_METHOD_SSLv2_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER;
         }
-        $ret = stream_socket_enable_crypto($socket, true, $type);
+
+        // Hidden error.
+        set_error_handler(function($errno, $errstr, $file){
+            if (!Worker::$daemonize) {
+                echo 'SSL handshake error: ',$errstr, "\n";
+            }
+        });
+        $ret     = stream_socket_enable_crypto($socket, true, $type);
+        restore_error_handler();
         // Negotiation has failed.
         if (false === $ret) {
-            if (!feof($socket)) {
-                echo "\nSSL Handshake fail as ".($async?'client':'server').". \nBuffer:".bin2hex(fread($socket, 8182))."\n";
-            }
             $this->destroy();
             return false;
         } elseif (0 === $ret) {
@@ -889,8 +900,12 @@ class TcpConnection extends ConnectionInterface
         // Remove event listener.
         Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
         Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
+
         // Close socket.
-        @fclose($this->_socket);
+        set_error_handler(function(){});
+        fclose($this->_socket);
+        restore_error_handler();
+
         $this->_status = self::STATUS_CLOSED;
         // Try to emit onClose callback.
         if ($this->onClose) {