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
|
|
|
public static function init() |
16
|
|
|
{ |
17
|
|
|
if (self::$instance !== null) { |
18
|
|
|
self::$instance->reinit(); |
19
|
|
|
} else { |
20
|
|
|
self::$instance = new static; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->base = new \EventBase; |
27
|
|
|
$this->callbacks = new StackCallbacks; |
28
|
|
|
$this->dnsBase = new \EventDnsBase($this->base, false); // @TODO: test with true |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getBase() |
32
|
|
|
{ |
33
|
|
|
return $this->base; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getDnsBase() |
37
|
|
|
{ |
38
|
|
|
return $this->dnsBase; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function reinit() |
42
|
|
|
{ |
43
|
|
|
$this->base->reinit(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function signal(...$args) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return \Event::signal($this->base, ...$args); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function timer(...$args) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return \Event::timer($this->base, ...$args); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function listener(...$args) |
57
|
|
|
{ |
58
|
|
|
return new \EventListener($this->base, ...$args); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function bufferEvent(...$args) |
62
|
|
|
{ |
63
|
|
|
return new \EventBufferEvent($this->base, ...$args); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function bufferEventSsl(...$args) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return \EventBufferEvent::sslSocket($this->base, ...$args); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function interrupt($cb = null) |
72
|
|
|
{ |
73
|
|
|
if ($cb !== null) { |
74
|
|
|
$this->callbacks->push($cb); |
75
|
|
|
} |
76
|
|
|
$this->base->exit(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function stop() |
80
|
|
|
{ |
81
|
|
|
$this->stopped = true; |
82
|
|
|
$this->interrupt(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function event(...$args) |
86
|
|
|
{ |
87
|
|
|
return new \Event($this->base, ...$args); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function run() |
91
|
|
|
{ |
92
|
|
|
$this->stopped = false; |
93
|
|
|
while (!$this->stopped) { |
94
|
|
|
$this->callbacks->executeAll($this); |
95
|
|
|
if (!$this->base->dispatch()) { |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |