This repository was archived by the owner on May 16, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSelectLoop.php
More file actions
152 lines (126 loc) · 4.31 KB
/
Copy pathSelectLoop.php
File metadata and controls
152 lines (126 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of Icicle, a library for writing asynchronous code in PHP using coroutines built with awaitables.
*
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved.
* @license MIT See the LICENSE file that was distributed with this source code for more information.
*/
namespace Icicle\Loop;
use Icicle\Loop\Exception\SignalHandlingDisabledError;
use Icicle\Loop\Manager\{IoManager, SignalManager, TimerManager};
use Icicle\Loop\Manager\Select\{SelectIoManager, SelectSignalManager, SelectTimerManager};
/**
* Uses stream_select(), time_nanosleep(), and pcntl_signal_dispatch() (if available) to implement an event loop that
* can poll sockets for I/O, create timers, and handle signals.
*/
class SelectLoop extends AbstractLoop
{
const MICROSEC_PER_SEC = 1e6;
const DEFAULT_SIGNAL_INTERVAL = 0.25;
/**
* @var \Icicle\Loop\Manager\Select\SelectIoManager
*/
private $pollManager;
/**
* @var \Icicle\Loop\Manager\Select\SelectIoManager
*/
private $awaitManager;
/**
* @var \Icicle\Loop\Manager\Select\SelectTimerManager
*/
private $timerManager;
/**
* @var \Icicle\Loop\Manager\Select\SelectSignalManager|null
*/
private $signalManager;
/**
* @var \Icicle\Loop\Watcher\Timer|null
*/
private $signalTimer;
/**
* {@inheritdoc}
*/
public function reInit() { /* Nothing to be done after fork. */ }
/**
* {@inheritdoc}
*/
protected function dispatch(bool $blocking)
{
$timeout = $blocking ? $this->timerManager->getInterval() : 0;
// Use stream_select() if there are any streams in the loop.
if (!$this->pollManager->isEmpty() || !$this->awaitManager->isEmpty()) {
$seconds = (int) $timeout;
$microseconds = ($timeout - $seconds) * self::MICROSEC_PER_SEC;
$read = $this->pollManager->getPending();
$write = $this->awaitManager->getPending();
$except = null;
// Error reporting suppressed since stream_select() emits an E_WARNING if it is interrupted by a signal.
$count = @stream_select($read, $write, $except, null === $timeout ? null : $seconds, $microseconds);
if ($count) {
$this->pollManager->handle($read);
$this->awaitManager->handle($write);
}
} elseif (0 < $timeout) { // Otherwise sleep with usleep() if $timeout > 0.
usleep($timeout * self::MICROSEC_PER_SEC);
}
$this->timerManager->tick(); // Call any pending timers.
}
/**
* {@inheritdoc}
*/
protected function createPollManager(): IoManager
{
return $this->pollManager = new SelectIoManager($this);
}
/**
* {@inheritdoc}
*/
protected function createAwaitManager(): IoManager
{
return $this->awaitManager = new SelectIoManager($this);
}
/**
* {@inheritdoc}
*/
protected function createTimerManager(): TimerManager
{
return $this->timerManager = new SelectTimerManager($this);
}
/**
* {@inheritdoc}
*/
protected function createSignalManager(): SignalManager
{
$this->signalManager = new SelectSignalManager($this);
$this->signalTimer = $this->timer(self::DEFAULT_SIGNAL_INTERVAL, true, [$this->signalManager, 'tick']);
$this->signalTimer->unreference();
return $this->signalManager;
}
/**
* @param float|int $interval
*
* @throws \Icicle\Loop\Exception\SignalHandlingDisabledError
*/
public function signalInterval(float $interval)
{
// @codeCoverageIgnoreStart
if (null === $this->signalTimer) {
throw new SignalHandlingDisabledError();
} // @codeCoverageIgnoreEnd
$this->signalTimer->stop();
$this->signalTimer = $this->timer($interval, true, [$this->signalManager, 'tick']);
$this->signalTimer->unreference();
}
/**
* {@inheritdoc}
*/
public function clear()
{
parent::clear();
if (null !== $this->signalTimer) {
$this->signalTimer->stop();
$this->signalTimer = $this->timer($this->signalTimer->getInterval(), true, [$this->signalManager, 'tick']);
$this->signalTimer->unreference();
}
}
}