|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Core; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Traits\EventLoopContainer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Timed event |
|
8
|
|
|
* @package PHPDaemon\Core |
|
9
|
|
|
* @author Vasily Zorin <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Timer |
|
12
|
|
|
{ |
|
13
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
14
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
15
|
|
|
use EventLoopContainer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Timer[] List of timers |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $list = []; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var integer Counter |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $counter = 1; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var integer|null Timer id |
|
27
|
|
|
*/ |
|
28
|
|
|
public $id; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var integer Current timeout holder |
|
31
|
|
|
*/ |
|
32
|
|
|
public $lastTimeout; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var boolean Is the timer finished? |
|
35
|
|
|
*/ |
|
36
|
|
|
public $finished = false; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var callable Callback |
|
39
|
|
|
*/ |
|
40
|
|
|
public $cb; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var integer Priority |
|
43
|
|
|
*/ |
|
44
|
|
|
public $priority; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var \EventBufferEvent Event resource |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $ev; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor |
|
52
|
|
|
* @param callable $cb Callback |
|
53
|
|
|
* @param integer $timeout Timeout |
|
|
|
|
|
|
54
|
|
|
* @param integer|string $id Timer ID |
|
|
|
|
|
|
55
|
|
|
* @param integer $priority Priority |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct($cb, $timeout = null, $id = null, $priority = null) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($id === null) { |
|
60
|
|
|
$id = ++self::$counter; |
|
61
|
|
|
} else { |
|
62
|
|
|
$id = (string)$id; |
|
63
|
|
|
} |
|
64
|
|
|
$this->id = $id; |
|
|
|
|
|
|
65
|
|
|
$this->cb = $cb; |
|
66
|
|
|
if ($this->eventLoop === null) { |
|
67
|
|
|
$this->eventLoop = EventLoop::$instance; |
|
68
|
|
|
} |
|
69
|
|
|
$this->ev = $this->eventLoop->timer([$this, 'eventCall']); |
|
70
|
|
|
if ($priority !== null) { |
|
71
|
|
|
$this->setPriority($priority); |
|
72
|
|
|
} |
|
73
|
|
|
if ($timeout !== null) { |
|
74
|
|
|
$this->timeout($timeout); |
|
75
|
|
|
} |
|
76
|
|
|
self::$list[$id] = $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set prioriry |
|
81
|
|
|
* @param integer $priority Priority |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setPriority($priority) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->priority = $priority; |
|
87
|
|
|
$this->ev->priority = $priority; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sets timeout |
|
92
|
|
|
* @param integer $timeout Timeout |
|
|
|
|
|
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function timeout($timeout = null) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($timeout !== null) { |
|
98
|
|
|
$this->lastTimeout = $timeout; |
|
99
|
|
|
} |
|
100
|
|
|
$this->ev->add($this->lastTimeout / 1e6); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Adds timer |
|
105
|
|
|
* @param callable $cb Callback |
|
106
|
|
|
* @param integer $timeout Timeout |
|
|
|
|
|
|
107
|
|
|
* @param integer|string $id Timer ID |
|
|
|
|
|
|
108
|
|
|
* @param integer $priority Priority |
|
|
|
|
|
|
109
|
|
|
* @return integer|string Timer ID |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
|
|
public static function add($cb, $timeout = null, $id = null, $priority = null) |
|
112
|
|
|
{ |
|
113
|
|
|
$obj = new self($cb, $timeout, $id, $priority); |
|
114
|
|
|
return $obj->id; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets timeout |
|
119
|
|
|
* @param integer|string $id Timer ID |
|
120
|
|
|
* @param integer $timeout Timeout |
|
|
|
|
|
|
121
|
|
|
* @return boolean |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function setTimeout($id, $timeout = null) |
|
124
|
|
|
{ |
|
125
|
|
|
if (isset(self::$list[$id])) { |
|
126
|
|
|
self::$list[$id]->timeout($timeout); |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Removes timer by ID |
|
134
|
|
|
* @param integer|string $id Timer ID |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function remove($id) |
|
138
|
|
|
{ |
|
139
|
|
|
if (isset(self::$list[$id])) { |
|
140
|
|
|
self::$list[$id]->free(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Frees the timer |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
public function free() |
|
149
|
|
|
{ |
|
150
|
|
|
unset(self::$list[$this->id]); |
|
151
|
|
|
if ($this->ev !== null) { |
|
152
|
|
|
$this->ev->free(); |
|
153
|
|
|
$this->ev = null; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Cancels timer by ID |
|
159
|
|
|
* @param integer|string $id Timer ID |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function cancelTimeout($id) |
|
163
|
|
|
{ |
|
164
|
|
|
if (isset(self::$list[$id])) { |
|
165
|
|
|
self::$list[$id]->cancel(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Cancels timer |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
public function cancel() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->ev->del(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Called when timer is triggered |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function eventCall() |
|
183
|
|
|
{ |
|
184
|
|
|
try { |
|
185
|
|
|
//Daemon::log('cb - '.Debug::zdump($this->cb)); |
|
|
|
|
|
|
186
|
|
|
$func = $this->cb; |
|
187
|
|
|
$func($this); |
|
188
|
|
|
} catch (\Exception $e) { |
|
189
|
|
|
Daemon::uncaughtExceptionHandler($e); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Finishes timer |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
public function finish() |
|
198
|
|
|
{ |
|
199
|
|
|
$this->free(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Destructor |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
|
|
public function __destruct() |
|
207
|
|
|
{ |
|
208
|
|
|
$this->free(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.