1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Core; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Structures\StackCallbacks; |
5
|
|
|
|
6
|
|
|
class EventLoop |
7
|
|
|
{ |
8
|
|
|
public static $instance; |
9
|
|
|
protected $base; |
10
|
|
|
protected $dnsBase; |
11
|
|
|
protected $callbacks; |
12
|
|
|
protected $stopped = true; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* EventLoop constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->base = new \EventBase; |
20
|
|
|
$this->callbacks = new StackCallbacks; |
21
|
|
|
$this->dnsBase = new \EventDnsBase($this->base, false); // @TODO: test with true |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Init |
26
|
|
|
*/ |
27
|
|
|
public static function init() |
28
|
|
|
{ |
29
|
|
|
if (self::$instance !== null) { |
30
|
|
|
self::$instance->reinit(); |
31
|
|
|
} else { |
32
|
|
|
self::$instance = new static; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Free |
38
|
|
|
*/ |
39
|
|
|
public function free() { |
40
|
|
|
$this->base->free(); |
41
|
|
|
$this->dnsBase->free(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return \EventBase |
46
|
|
|
*/ |
47
|
|
|
public function getBase() |
48
|
|
|
{ |
49
|
|
|
return $this->base; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \EventDnsBase |
54
|
|
|
*/ |
55
|
|
|
public function getDnsBase() |
56
|
|
|
{ |
57
|
|
|
return $this->dnsBase; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Reinit |
62
|
|
|
*/ |
63
|
|
|
public function reinit() |
64
|
|
|
{ |
65
|
|
|
$this->base->reinit(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array ...$args |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function signal(...$args) |
73
|
|
|
{ |
74
|
|
|
return \Event::signal($this->base, ...$args); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array ...$args |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function timer(...$args) |
82
|
|
|
{ |
83
|
|
|
return \Event::timer($this->base, ...$args); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array ...$args |
88
|
|
|
* @return \EventListener |
89
|
|
|
*/ |
90
|
|
|
public function listener(...$args) |
91
|
|
|
{ |
92
|
|
|
return new \EventListener($this->base, ...$args); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array ...$args |
97
|
|
|
* @return \EventBufferEvent |
98
|
|
|
*/ |
99
|
|
|
public function bufferEvent(...$args) |
100
|
|
|
{ |
101
|
|
|
return new \EventBufferEvent($this->base, ...$args); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array ...$args |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function bufferEventSsl(...$args) |
109
|
|
|
{ |
110
|
|
|
return \EventBufferEvent::sslSocket($this->base, ...$args); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Stop |
115
|
|
|
*/ |
116
|
|
|
public function stop() |
117
|
|
|
{ |
118
|
|
|
$this->stopped = true; |
119
|
|
|
$this->interrupt(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param null $cb |
124
|
|
|
*/ |
125
|
|
|
public function interrupt($cb = null) |
126
|
|
|
{ |
127
|
|
|
if ($cb !== null) { |
128
|
|
|
$this->callbacks->push($cb); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
$this->base->exit(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array ...$args |
135
|
|
|
* @return \Event |
136
|
|
|
*/ |
137
|
|
|
public function event(...$args) |
138
|
|
|
{ |
139
|
|
|
return new \Event($this->base, ...$args); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Run |
144
|
|
|
*/ |
145
|
|
|
public function run() |
146
|
|
|
{ |
147
|
|
|
$this->stopped = false; |
148
|
|
|
while (!$this->stopped) { |
149
|
|
|
$this->callbacks->executeAll($this); |
150
|
|
|
if (!$this->base->dispatch()) { |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |