1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
13
|
|
|
use AsyncSockets\RequestExecutor\Metadata\SocketBag; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Pipeline |
17
|
|
|
*/ |
18
|
|
|
class Pipeline |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* DisconnectStage |
22
|
|
|
* |
23
|
|
|
* @var PipelineStageInterface |
24
|
|
|
*/ |
25
|
|
|
private $disconnectStage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Connect stage |
29
|
|
|
* |
30
|
|
|
* @var PipelineStageInterface |
31
|
|
|
*/ |
32
|
|
|
private $connectStage; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* PipelineStageInterface |
36
|
|
|
* |
37
|
|
|
* @var PipelineStageInterface[] |
38
|
|
|
*/ |
39
|
|
|
private $stages; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Pipeline constructor |
43
|
|
|
* |
44
|
|
|
* @param PipelineStageInterface $connectStage Connect stage |
45
|
|
|
* @param PipelineStageInterface[] $stages Pipeline stages |
46
|
|
|
* @param PipelineStageInterface $disconnectStage Disconnect stages |
47
|
|
|
*/ |
48
|
62 |
|
public function __construct( |
49
|
|
|
PipelineStageInterface $connectStage, |
50
|
|
|
array $stages, |
51
|
|
|
PipelineStageInterface $disconnectStage |
52
|
|
|
) { |
53
|
62 |
|
$this->connectStage = $connectStage; |
54
|
62 |
|
$this->stages = $stages; |
55
|
62 |
|
$this->disconnectStage = $disconnectStage; |
56
|
62 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process I/O operations on sockets |
60
|
|
|
* |
61
|
|
|
* @param SocketBag $socketBag Socket bag |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
62 |
|
public function process(SocketBag $socketBag) |
67
|
|
|
{ |
68
|
|
|
do { |
69
|
62 |
|
$activeOperations = $this->connectStage->processStage($socketBag->getItems()); |
70
|
54 |
|
if (!$activeOperations) { |
71
|
28 |
|
break; |
72
|
|
|
} |
73
|
|
|
|
74
|
48 |
|
foreach ($this->stages as $stage) { |
75
|
48 |
|
$activeOperations = $stage->processStage($activeOperations); |
76
|
26 |
|
} |
77
|
24 |
|
} while (true); |
78
|
28 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Disconnect given list of sockets |
82
|
|
|
* |
83
|
|
|
* @param RequestDescriptor[] $items Sockets' operations |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
9 |
|
public function disconnectSockets(array $items) |
88
|
|
|
{ |
89
|
9 |
|
$this->disconnectStage->processStage($items); |
90
|
9 |
|
} |
91
|
|
|
} |
92
|
|
|
|