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