walkor 2 жил өмнө
parent
commit
ff6fedcbd4

+ 8 - 8
src/Connection/TcpConnection.php

@@ -714,14 +714,14 @@ class TcpConnection extends ConnectionInterface implements \JsonSerializable
      */
     public function baseWrite()
     {
-        \set_error_handler(function () {
-        });
-        if ($this->transport === 'ssl') {
-            $len = @\fwrite($this->socket, $this->sendBuffer, 8192);
-        } else {
-            $len = @\fwrite($this->socket, $this->sendBuffer);
-        }
-        \restore_error_handler();
+        $len = 0;
+        try {
+            if ($this->transport === 'ssl') {
+                $len = @\fwrite($this->socket, $this->sendBuffer, 8192);
+            } else {
+                $len = @\fwrite($this->socket, $this->sendBuffer);
+            }
+        } catch (\Throwable $e) {}
         if ($len === \strlen($this->sendBuffer)) {
             $this->bytesWritten += $len;
             Worker::$globalEvent->offWritable($this->socket);

+ 5 - 0
src/Events/Swoole.php

@@ -39,6 +39,11 @@ class Swoole implements EventInterface
     protected $writeEvents = [];
 
     /**
+     * @var int
+     */
+    protected $mapId = 0;
+
+    /**
      * {@inheritdoc}
      */
     public function delay(float $delay, $func, $args)

+ 25 - 15
src/Timer.php

@@ -15,9 +15,11 @@ namespace Workerman;
 
 use Revolt\EventLoop;
 use Workerman\Events\EventInterface;
+use Workerman\Events\Revolt;
 use Workerman\Events\Select;
-use Workerman\Worker;
-use \Exception;
+use Workerman\Events\Swoole;
+use Swoole\Coroutine\System;
+use Exception;
 
 /**
  * Timer.
@@ -143,22 +145,30 @@ class Timer
     }
 
     /**
+     * Coroutine sleep.
+     *
      * @param float $delay
-     * @param $func
-     * @param array $args
-     * @return bool|int|null
+     * @return null
      */
-    public static function delay(float $delay, $func = null, array $args = [])
+    public static function sleep(float $delay)
     {
-        if (!$func) {
-            $suspension = EventLoop::getSuspension();
-            static::add($delay, function () use ($suspension) {
-                $suspension->resume();
-            }, $args, false);
-            $suspension->suspend();
-            return null;
-        }
-        return static::add($delay, $func, $args, false);
+        switch (Worker::$eventLoopClass) {
+            // Fiber
+            case Revolt::class:
+                $suspension = EventLoop::getSuspension();
+                static::add($delay, function () use ($suspension) {
+                    $suspension->resume();
+                }, null, false);
+                $suspension->suspend();
+                return null;
+            // Swoole
+            case Swoole::class:
+                System::sleep($delay);
+                return null;
+        }
+        // Swow or non coroutine environment
+        msleep($delay * 1000);
+        return null;
     }
 
     /**