1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Core; |
3
|
|
|
use PHPDaemon\Structures\StackCallbacks; |
4
|
|
|
|
5
|
|
|
class EventLoop |
6
|
|
|
{ |
7
|
|
|
public static $instance; |
8
|
|
|
protected $base; |
9
|
|
|
protected $dnsBase; |
10
|
|
|
protected $callbacks; |
11
|
|
|
protected $stopped = true; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
public static function init() { |
15
|
|
|
if (self::$instance !== null) { |
16
|
|
|
self::$instance->reinit(); |
17
|
|
|
} else { |
18
|
|
|
self::$instance = new static; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function __construct() { |
23
|
|
|
$this->base = new \EventBase; |
24
|
|
|
$this->callbacks = new StackCallbacks; |
25
|
|
|
$this->dnsBase = new \EventDnsBase($this->base, false); // @TODO: test with true |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getBase() { |
29
|
|
|
return $this->base; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getDnsBase() { |
33
|
|
|
return $this->dnsBase; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function reinit() { |
37
|
|
|
$this->base->reinit(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function signal(...$args) { |
|
|
|
|
41
|
|
|
return \Event::signal($this->base, ...$args); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function timer(...$args) { |
|
|
|
|
45
|
|
|
return \Event::timer($this->base, ...$args); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function listener(...$args) { |
49
|
|
|
return new \EventListener($this->base, ...$args); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function bufferEvent(...$args) { |
53
|
|
|
return new \EventBufferEvent($this->base, ...$args); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function bufferEventSsl(...$args) { |
|
|
|
|
57
|
|
|
return \EventBufferEvent::sslSocket($this->base, ...$args); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function interrupt($cb = null) { |
61
|
|
|
if ($cb !== null) { |
62
|
|
|
$this->callbacks->push($cb); |
63
|
|
|
} |
64
|
|
|
$this->base->exit(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function stop() { |
68
|
|
|
$this->stopped = true; |
69
|
|
|
$this->interrupt(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function event(...$args) { |
73
|
|
|
return new \Event($this->base, ...$args); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function run() { |
77
|
|
|
$this->stopped = false; |
78
|
|
|
while (!$this->stopped) { |
79
|
|
|
$this->callbacks->executeAll($this); |
80
|
|
|
if (!$this->base->dispatch()) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |