Przeglądaj źródła

Merge pull request #2 from walkor/master

Update
Leonardo 8 lat temu
rodzic
commit
d9b7d5cff8
6 zmienionych plików z 70 dodań i 14 usunięć
  1. 4 2
      Connection/AsyncTcpConnection.php
  2. 10 5
      Connection/TcpConnection.php
  3. 21 4
      Events/React.php
  4. 2 2
      Lib/Timer.php
  5. 30 0
      README.md
  6. 3 1
      Worker.php

+ 4 - 2
Connection/AsyncTcpConnection.php

@@ -106,8 +106,10 @@ class AsyncTcpConnection extends TcpConnection
     {
         $address_info = parse_url($remote_address);
         if (!$address_info) {
-            echo new \Exception('bad remote_address');
-            $this->_remoteAddress = $remote_address;
+            list($scheme, $this->_remoteAddress) = explode(':', $remote_address, 2);
+            if (!$this->_remoteAddress) {
+                echo new \Exception('bad remote_address');
+            }
         } else {
             if (!isset($address_info['port'])) {
                 $address_info['port'] = 80;

+ 10 - 5
Connection/TcpConnection.php

@@ -387,12 +387,17 @@ class TcpConnection extends ConnectionInterface
     {
         // SSL handshake.
         if ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true) {
-            stream_set_blocking($socket, true);
-            stream_set_timeout($socket, 1);
-            $ret = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
-            if(!$ret) {
-                echo new \Exception('ssl handshake fail, stream_socket_enable_crypto return ' . var_export($ret, true));
+            $ret = stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv2_SERVER |
+                STREAM_CRYPTO_METHOD_SSLv3_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER);
+            // Negotiation has failed.
+            if(false === $ret) {
+                if (!feof($socket)) {
+                    echo "\nSSL Handshake fail. \nBuffer:".bin2hex(fread($socket, 8182))."\n";
+                }
                 return $this->destroy();
+            } elseif(0 === $ret) {
+                // There isn't enough data and should try again.
+                return;
             }
             if (isset($this->onSslHandshake)) {
                 try {

+ 21 - 4
Events/React.php

@@ -26,6 +26,16 @@ class React implements LoopInterface
     protected $_loop = null;
 
     /**
+     * @var array
+     */
+    protected $_timerIdMap = array();
+
+    /**
+     * @var int
+     */
+    protected $_timerIdIndex = 0;
+
+    /**
      * React constructor.
      */
     public function __construct() {
@@ -58,13 +68,17 @@ class React implements LoopInterface
             case EventInterface::EV_SIGNAL:
                 return $this->_loop->addSignal($fd, $func);
             case EventInterface::EV_TIMER:
-                return $this->_loop->addPeriodicTimer($fd, function() use ($func, $args) {
+                $timer_obj = $this->_loop->addPeriodicTimer($fd, function() use ($func, $args) {
                     call_user_func_array($func, $args);
                 });
+                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
+                return $this->_timerIdIndex;
             case EventInterface::EV_TIMER_ONCE:
-                return $this->_loop->addTimer($fd, function() use ($func, $args) {
+                $timer_obj = $this->_loop->addTimer($fd, function() use ($func, $args) {
                     call_user_func_array($func, $args);
                 });
+                $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
+                return $this->_timerIdIndex;
         }
         return false;
     }
@@ -87,8 +101,11 @@ class React implements LoopInterface
                 return $this->_loop->removeSignal($fd);
             case EventInterface::EV_TIMER:
             case EventInterface::EV_TIMER_ONCE;
-                if ($fd !== null){
-                    return  $this->_loop->cancelTimer($fd);
+                if (isset($this->_timerIdMap[$fd])){
+                    $timer_obj = $this->_timerIdMap[$fd];
+                    unset($this->_timerIdMap[$fd]);
+                    $this->_loop->cancelTimer($timer_obj);
+                    return true;
                 }
         }
         return false;

+ 2 - 2
Lib/Timer.php

@@ -78,7 +78,7 @@ class Timer
      * @param callback $func
      * @param mixed    $args
      * @param bool     $persistent
-     * @return bool
+     * @return int/false
      */
     public static function add($time_interval, $func, $args = array(), $persistent = true)
     {
@@ -107,7 +107,7 @@ class Timer
             self::$_tasks[$run_time] = array();
         }
         self::$_tasks[$run_time][] = array($func, (array)$args, $persistent, $time_interval);
-        return true;
+        return 1;
     }
 
 

+ 30 - 0
README.md

@@ -129,6 +129,36 @@ $tcp_worker->onClose = function($connection)
 Worker::runAll();
 ```
 
+### Enable SSL.
+```php
+<?php
+require_once __DIR__ . '/vendor/autoload.php';
+use Workerman\Worker;
+
+// SSL context.
+$context = array(
+    'ssl' => array(
+        'local_cert' => '/your/path/of/server.pem',
+        'local_pk'   => '/your/path/of/server.key',
+    )
+);
+
+// Create a Websocket server with ssl context.
+$ws_worker = new Worker("websocket://0.0.0.0:2346", $context);
+
+// Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). 
+// The similar approaches for Https etc.
+$ws_worker->transport = 'ssl';
+
+$ws_worker->onMessage = function($connection, $data)
+{
+    // Send hello $data
+    $connection->send('hello ' . $data);
+};
+
+Worker::runAll();
+```
+
 ### Custom protocol
 Protocols/MyTextProtocol.php
 ```php

+ 3 - 1
Worker.php

@@ -33,7 +33,7 @@ class Worker
      *
      * @var string
      */
-    const VERSION = '3.3.7';
+    const VERSION = '3.3.9';
 
     /**
      * Status starting.
@@ -934,6 +934,8 @@ class Worker
             $worker->setUserAndGroup();
             $worker->id = $id;
             $worker->run();
+            $err = new Exception('event-loop exited');
+            self::log($err);
             exit(250);
         } else {
             throw new Exception("forkOneWorker fail");