Skip to content

Commit df513f3

Browse files
committed
Fix swoole
1 parent 9611101 commit df513f3

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

Events/Swoole.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Workerman\Worker;
1717
use Swoole\Event;
1818
use Swoole\Timer;
19+
use Swoole\Coroutine;
1920

2021
class Swoole implements EventInterface
2122
{
@@ -33,6 +34,10 @@ class Swoole implements EventInterface
3334

3435
protected $_hasSignal = false;
3536

37+
protected $_readEvents = array();
38+
39+
protected $_writeEvents = array();
40+
3641
/**
3742
*
3843
* {@inheritdoc}
@@ -61,7 +66,7 @@ function () {
6166
$mapId = $this->mapId++;
6267
$t = (int)($fd * 1000);
6368
if ($t < 1) {
64-
$t = 1;
69+
$t = 1;
6570
}
6671
$timer_id = Timer::$method($t,
6772
function ($timer_id = null) use ($func, $args, $mapId) {
@@ -92,9 +97,14 @@ function ($timer_id = null) use ($func, $args, $mapId) {
9297
case self::EV_READ:
9398
case self::EV_WRITE:
9499
$fd_key = (int) $fd;
95-
if (! isset($this->_fd[$fd_key])) {
100+
if ($flag === self::EV_READ) {
101+
$this->_readEvents[$fd_key] = $func;
102+
} else {
103+
$this->_writeEvents[$fd_key] = $func;
104+
}
105+
if (!isset($this->_fd[$fd_key])) {
96106
if ($flag === self::EV_READ) {
97-
$res = Event::add($fd, $func, null, SWOOLE_EVENT_READ);
107+
$res = Event::add($fd, [$this, 'callRead'], null, SWOOLE_EVENT_READ);
98108
$fd_type = SWOOLE_EVENT_READ;
99109
} else {
100110
$res = Event::add($fd, null, $func, SWOOLE_EVENT_WRITE);
@@ -124,6 +134,42 @@ function ($timer_id = null) use ($func, $args, $mapId) {
124134
}
125135
}
126136

137+
/**
138+
* @param $fd
139+
* @return void
140+
*/
141+
protected function callRead($stream)
142+
{
143+
$fd = (int) $stream;
144+
if (isset($this->_readEvents[$fd])) {
145+
try {
146+
\call_user_func($this->_readEvents[$fd], $stream);
147+
} catch (\Exception $e) {
148+
Worker::stopAll(250, $e);
149+
} catch (\Error $e) {
150+
Worker::stopAll(250, $e);
151+
}
152+
}
153+
}
154+
155+
/**
156+
* @param $fd
157+
* @return void
158+
*/
159+
protected function callWrite($stream)
160+
{
161+
$fd = (int) $stream;
162+
if (isset($this->_writeEvents[$fd])) {
163+
try {
164+
\call_user_func($this->_writeEvents[$fd], $stream);
165+
} catch (\Exception $e) {
166+
Worker::stopAll(250, $e);
167+
} catch (\Error $e) {
168+
Worker::stopAll(250, $e);
169+
}
170+
}
171+
}
172+
127173
/**
128174
*
129175
* {@inheritdoc}
@@ -153,6 +199,11 @@ public function del($fd, $flag)
153199
case self::EV_READ:
154200
case self::EV_WRITE:
155201
$fd_key = (int) $fd;
202+
if ($flag === self::EV_READ) {
203+
unset($this->_readEvents[$fd_key]);
204+
} elseif ($flag === self::EV_WRITE) {
205+
unset($this->_writeEvents[$fd_key]);
206+
}
156207
if (isset($this->_fd[$fd_key])) {
157208
$fd_val = $this->_fd[$fd_key];
158209
if ($flag === self::EV_READ) {
@@ -213,8 +264,10 @@ public function loop()
213264
*/
214265
public function destroy()
215266
{
267+
foreach (Coroutine::listCoroutines() as $coroutine) {
268+
Coroutine::cancel($coroutine);
269+
}
216270
Event::exit();
217-
posix_kill(posix_getpid(), SIGINT);
218271
}
219272

220273
/**

0 commit comments

Comments
 (0)