Skip to content

Commit

Permalink
update goApp handle
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Mar 26, 2023
1 parent ca28224 commit a02971d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 72 deletions.
13 changes: 6 additions & 7 deletions src/Core/Coroutine/GoWaitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Swoolefy\Core\Coroutine;

use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoolefy\Core\BaseServer;
use Swoolefy\Exception\SystemException;
Expand Down Expand Up @@ -50,17 +49,17 @@ 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);
} catch (\Throwable $throwable) {
$this->count--;
BaseServer::catchException($throwable);
}
}, ...$params);
});
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 10 additions & 13 deletions src/Core/Coroutine/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
50 changes: 27 additions & 23 deletions src/Core/EventApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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引入
Expand All @@ -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 {
Expand Down Expand Up @@ -115,15 +128,15 @@ public function registerApp($class, array $args = [])
* getCid
* @return int
*/
public function getCid()
public function getCid(): int
{
return $this->eventApp->getCid();
}

/**
* @return EventController
*/
public function getEventApp()
public function getEventApp(): EventController
{
return $this->eventApp;
}
Expand All @@ -134,7 +147,6 @@ public function getEventApp()
* @param string $action
* @param array $args
* @return mixed
* @throws \Exception
*/
public function __call(string $action, array $args = [])
{
Expand All @@ -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();
}
}
}

Expand All @@ -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);
}
}
35 changes: 6 additions & 29 deletions src/Core/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

namespace Swoolefy\Core;

use Swoolefy\Core\Coroutine\CoroutinePools;
use Swoolefy\Core\Coroutine\CoroutineManager;
use Swoolefy\Exception\SystemException;

/**
* Class EventController
Expand Down Expand Up @@ -67,46 +65,25 @@ 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);
$this->coroutineId = $coroutineId;
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;
}

/**
Expand Down Expand Up @@ -211,8 +188,8 @@ public function isDefer()
}

/**
* end 重新初始化一些静态变量
* @return mixed
* end unset var
* @return bool|void
*/
public function end()
{
Expand Down

0 comments on commit a02971d

Please sign in to comment.