From a02971dd00bf45ed125b721de5e6afef9b35e9c9 Mon Sep 17 00:00:00 2001 From: bingcool <2437667702@qq.com> Date: Sun, 26 Mar 2023 22:02:49 +0800 Subject: [PATCH] update goApp handle --- src/Core/Coroutine/GoWaitGroup.php | 13 ++++---- src/Core/Coroutine/Timer.php | 23 ++++++-------- src/Core/EventApp.php | 50 ++++++++++++++++-------------- src/Core/EventController.php | 35 ++++----------------- 4 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/Core/Coroutine/GoWaitGroup.php b/src/Core/Coroutine/GoWaitGroup.php index 982c7a5f..bcf165e8 100644 --- a/src/Core/Coroutine/GoWaitGroup.php +++ b/src/Core/Coroutine/GoWaitGroup.php @@ -11,7 +11,6 @@ namespace Swoolefy\Core\Coroutine; -use Swoole\Coroutine; use Swoole\Coroutine\Channel; use Swoolefy\Core\BaseServer; use Swoolefy\Exception\SystemException; @@ -50,9 +49,9 @@ public function __construct() * @param \Closure $callBack * @param mixed ...$params */ - public function go(\Closure $callBack, ...$params) + public function goApp(\Closure $callBack, ...$params) { - Coroutine::create(function (...$params) use ($callBack) { + goApp(function () use ($callBack, $params) { try { $this->count++; call_user_func($callBack, ...$params); @@ -60,7 +59,7 @@ public function go(\Closure $callBack, ...$params) $this->count--; BaseServer::catchException($throwable); } - }, ...$params); + }); } /** @@ -105,7 +104,7 @@ public static function multiCall(array $callBacks, float $timeOut = 3.0) $goWait = new static(); foreach ($callBacks as $key => $callBack) { $goWait->count++; - Coroutine::create(function () use ($key, $callBack, $goWait) { + goApp(function () use ($key, $callBack, $goWait) { try { $goWait->initResult($key, null); $result = call_user_func($callBack); @@ -138,11 +137,11 @@ public function start() */ public function done( string $key = null, - $data = null, + mixed $data = null, float $timeout = -1 ) { - if (!empty($key) && !is_null($data)) { + if (!empty($key) && !empty($data)) { $this->result[$key] = $data; } $count = $this->count -1; diff --git a/src/Core/Coroutine/Timer.php b/src/Core/Coroutine/Timer.php index da53486b..6ab12881 100644 --- a/src/Core/Coroutine/Timer.php +++ b/src/Core/Coroutine/Timer.php @@ -37,13 +37,14 @@ public static function tick(int $timeMs, callable $callable) $timeChannel->close(); break; } - Coroutine::create(function ($callable) use($timeChannel) { - try { + try { + goApp(function ($callable) use($timeChannel, $callable) { $callable($timeChannel); - }catch (\Throwable $exception) { - BaseServer::catchException($exception); - } - }, $callable); + }); + }catch (\Throwable $exception) + { + BaseServer::catchException($exception); + } } }, $second, $callable); @@ -76,13 +77,9 @@ public static function after(int $timeMs, callable $callable) Coroutine::create(function ($second, $callable) use ($timeChannel) { while (!$timeChannel->pop($second)) { - Coroutine::create(function ($callable) use($timeChannel) { - try { - $callable($timeChannel); - }catch (\Throwable $exception) { - BaseServer::catchException($exception); - } - }, $callable); + goApp(function ($callable) use($timeChannel, $callable) { + $callable($timeChannel); + }); break; } }, $second, $callable); diff --git a/src/Core/EventApp.php b/src/Core/EventApp.php index db0c6b8b..aa536101 100644 --- a/src/Core/EventApp.php +++ b/src/Core/EventApp.php @@ -48,18 +48,19 @@ class EventApp * class Close extends EventController { * // 继承于EventController,可以传入可变参数 * public function __construct($server, $fd) { - * // 必须执行父类__construct() - * parent::__construct(); + * // 必须执行父类__construct() + * parent::__construct(); * } * * public function close() { - * //TODO + * //TODO * } - * } - * 同时go创建协程中,创建应用实例可以使用这个类注册实例,\App\AbstractEventHandle\Gocoroutine继承于\Swoolefy\Core\EventController + * + * } + * 同时go创建协程中,创建应用实例可以使用这个类注册实例,\App\AbstractEventHandle\Goroutine继承于\Swoolefy\Core\EventController * registerApp的第二个参数args是class的__construct参数 * go(function() { - * $app = (new \Swoolefy\Core\EventApp)->registerApp(\App\AbstractEventHandle\Gocoroutine::class, ['name','id']); + * $app = (new \Swoolefy\Core\EventApp)->registerApp(\App\AbstractEventHandle\Goroutine::class, ['name','id']); * $app->test(); * }); * 也可以利用闭包形式,最后一个函数是传进来的闭包函数的形参,外部变量使用use引入 @@ -69,23 +70,35 @@ class EventApp * }); * }); * + * 强烈推荐使用 + * goApp(function(EventController $event) use($name, $id) { + * var_dump($event); //输出EventController 实例 + * }) + * * @param string|\Closure $class * @param array $args * @return $this - * @throws SystemException */ public function registerApp($class, array $args = []) { if ($class instanceof \Closure) { try { - $this->eventApp = new EventController(...$args); + $cid = \Swoole\Coroutine::getCid(); + if(Application::issetApp($cid)) { + $app = Application::getApp(); + if ($app instanceof EventController) { + $this->eventApp = $app; + }else { + $this->eventApp = new EventController(...$args); + } + }else { + $this->eventApp = new EventController(...$args); + } + call_user_func($class, $this->eventApp); + } catch (\Exception | \Throwable $throwable) { BaseServer::catchException($throwable); - } finally { - if (is_object($this->eventApp) && !$this->eventApp->isDefer()) { - $this->eventApp->end(); - } } } else { do { @@ -115,7 +128,7 @@ public function registerApp($class, array $args = []) * getCid * @return int */ - public function getCid() + public function getCid(): int { return $this->eventApp->getCid(); } @@ -123,7 +136,7 @@ public function getCid() /** * @return EventController */ - public function getEventApp() + public function getEventApp(): EventController { return $this->eventApp; } @@ -134,7 +147,6 @@ public function getEventApp() * @param string $action * @param array $args * @return mixed - * @throws \Exception */ public function __call(string $action, array $args = []) { @@ -146,13 +158,8 @@ public function __call(string $action, array $args = []) try { $this->isCall = true; return $this->eventApp->$action(...$args); - } catch (\Throwable $throwable) { BaseServer::catchException($throwable); - }finally { - if (is_object($this->eventApp) && !$this->eventApp->isDefer()) { - $this->eventApp->end(); - } } } @@ -161,11 +168,8 @@ public function __call(string $action, array $args = []) */ public function __destruct() { - $cid = null; if (is_object($this->eventApp) && $this->eventApp instanceof EventController) { - $cid = $this->eventApp->getCid(); unset($this->eventApp); } - Application::removeApp($cid); } } \ No newline at end of file diff --git a/src/Core/EventController.php b/src/Core/EventController.php index f1e5c7c6..a57411e6 100644 --- a/src/Core/EventController.php +++ b/src/Core/EventController.php @@ -11,9 +11,7 @@ namespace Swoolefy\Core; -use Swoolefy\Core\Coroutine\CoroutinePools; use Swoolefy\Core\Coroutine\CoroutineManager; -use Swoolefy\Exception\SystemException; /** * Class EventController @@ -67,19 +65,16 @@ public function __construct(...$args) $this->creatObject(); $this->appConf = Swfy::getAppConf(); $this->coroutineId = CoroutineManager::getInstance()->getCoroutineId(); - if ($this->canCreateApp($this->coroutineId)) { - Application::setApp($this); - $this->defer(); - } + Application::setApp($this); + $this->defer(); } /** * setApp * @param int $coroutineId * @return bool - * @throws \Exception */ - public function setApp(?int $coroutineId = null) + public function setApp(int $coroutineId): bool { if ($coroutineId) { Application::removeApp($this->coroutineId); @@ -87,26 +82,8 @@ public function setApp(?int $coroutineId = null) Application::setApp($this); return true; } - return false; - } - /** - * @param null $coroutineId - * @return bool - * @throws \Exception - */ - public function canCreateApp(int $coroutineId = null) - { - if (empty($coroutineId)) { - $coroutineId = CoroutineManager::getInstance()->getCoroutineId(); - } - - $exists = Application::issetApp($coroutineId); - - if ($exists) { - throw new SystemException("You had created EventApp Instance, yon can only registerApp once, so you can't create same coroutine"); - } - return true; + return false; } /** @@ -211,8 +188,8 @@ public function isDefer() } /** - * end 重新初始化一些静态变量 - * @return mixed + * end unset var + * @return bool|void */ public function end() {