|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Async sockets |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2017, 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
|
|
|
|
|
11
|
|
|
namespace AsyncSockets\Operation; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ReadWriteOperation. Provides reading and writing at the same time |
|
15
|
|
|
*/ |
|
16
|
|
|
class ReadWriteOperation implements OperationInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Flag if read operations must be processed before write |
|
20
|
|
|
*/ |
|
21
|
|
|
const READ_FIRST = true; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Flag if write operations must be processed before read |
|
25
|
|
|
*/ |
|
26
|
|
|
const WRITE_FIRST = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Operation queue indexed by operation type |
|
30
|
|
|
* |
|
31
|
|
|
* @var OperationInterface[][] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $queue = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Flag whether read operation must be fired before write |
|
37
|
|
|
* |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
private $isReadFirst; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* ReadWriteOperation constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param bool $isReadFirst Flag whether read operation must be fired before write |
|
46
|
|
|
* @param OperationInterface[] $operations Array of operations to schedule |
|
47
|
|
|
*/ |
|
48
|
15 |
|
public function __construct($isReadFirst, array $operations = []) |
|
49
|
|
|
{ |
|
50
|
15 |
|
$this->isReadFirst = $isReadFirst; |
|
51
|
15 |
|
foreach ($operations as $operation) { |
|
52
|
14 |
|
$this->scheduleOperation($operation); |
|
53
|
15 |
|
} |
|
54
|
15 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set read or write operation |
|
58
|
|
|
* |
|
59
|
|
|
* @param OperationInterface $operation Operation to set |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
15 |
|
public function scheduleOperation(OperationInterface $operation) |
|
64
|
|
|
{ |
|
65
|
15 |
|
$key = spl_object_hash($operation); |
|
66
|
15 |
|
switch (true) { |
|
67
|
15 |
|
case $operation instanceof ReadOperation: |
|
68
|
11 |
|
$this->queue[OperationInterface::OPERATION_READ][$key] = $operation; |
|
69
|
11 |
|
break; |
|
70
|
12 |
|
case $operation instanceof WriteOperation: |
|
71
|
11 |
|
$this->queue[OperationInterface::OPERATION_WRITE][$key] = $operation; |
|
72
|
11 |
|
break; |
|
73
|
1 |
|
default: |
|
74
|
|
|
// no action |
|
75
|
1 |
|
} |
|
76
|
15 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Mark operation as completed |
|
80
|
|
|
* |
|
81
|
|
|
* @param OperationInterface $operation Operation to mark as done |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
10 |
|
public function markCompleted(OperationInterface $operation) |
|
86
|
|
|
{ |
|
87
|
10 |
|
$key = spl_object_hash($operation); |
|
88
|
10 |
|
switch (true) { |
|
89
|
10 |
|
case $operation instanceof ReadOperation: |
|
90
|
7 |
|
unset($this->queue[OperationInterface::OPERATION_READ][$key]); |
|
91
|
7 |
|
break; |
|
92
|
8 |
|
case $operation instanceof WriteOperation: |
|
93
|
7 |
|
unset($this->queue[OperationInterface::OPERATION_WRITE][$key]); |
|
94
|
7 |
|
break; |
|
95
|
1 |
|
default: |
|
96
|
|
|
// no action |
|
97
|
1 |
|
} |
|
98
|
10 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return current read operation |
|
102
|
|
|
* |
|
103
|
|
|
* @return ReadOperation|null |
|
104
|
|
|
*/ |
|
105
|
11 |
|
public function getReadOperation() |
|
106
|
|
|
{ |
|
107
|
11 |
|
return !empty($this->queue[OperationInterface::OPERATION_READ]) ? |
|
108
|
11 |
|
reset($this->queue[OperationInterface::OPERATION_READ]) : |
|
109
|
11 |
|
null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return current write operation |
|
114
|
|
|
* |
|
115
|
|
|
* @return WriteOperation|null |
|
116
|
|
|
*/ |
|
117
|
11 |
|
public function getWriteOperation() |
|
118
|
|
|
{ |
|
119
|
11 |
|
return !empty($this->queue[OperationInterface::OPERATION_WRITE]) ? |
|
120
|
11 |
|
reset($this->queue[OperationInterface::OPERATION_WRITE]) : |
|
121
|
11 |
|
null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Return true if read must be handled before write |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
5 |
|
public function isReadFirst() |
|
130
|
|
|
{ |
|
131
|
5 |
|
return $this->isReadFirst; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @inheritDoc |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function getTypes() |
|
138
|
|
|
{ |
|
139
|
3 |
|
$result = []; |
|
140
|
3 |
|
foreach ($this->queue as $type => $operations) { |
|
141
|
3 |
|
if (!empty($operations)) { |
|
142
|
3 |
|
$result[] = $type; |
|
143
|
3 |
|
} |
|
144
|
3 |
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return $result; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|