1 | <?php |
||
25 | abstract class AbstractOobHandler implements IoHandlerInterface |
||
26 | { |
||
27 | /** |
||
28 | * Operation to descriptor state map |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private static $stateMap = [ |
||
33 | OperationInterface::OPERATION_READ => RequestDescriptor::RDS_READ, |
||
34 | OperationInterface::OPERATION_WRITE => RequestDescriptor::RDS_WRITE, |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 112 | final public function handle( |
|
41 | OperationInterface $operation, |
||
42 | RequestDescriptor $descriptor, |
||
43 | RequestExecutorInterface $executor, |
||
44 | EventHandlerInterface $eventHandler, |
||
45 | ExecutionContext $executionContext |
||
46 | ) { |
||
47 | 112 | $result = $this->handleOobData($descriptor, $executor, $eventHandler, $executionContext); |
|
48 | 112 | if ($result) { |
|
49 | 5 | return $result; |
|
50 | } |
||
51 | |||
52 | 107 | $state = $this->getInvokeState($descriptor->getOperation()); |
|
53 | 107 | if ($descriptor->hasState($state)) { |
|
54 | 102 | $descriptor->clearState($state); |
|
55 | |||
56 | 102 | $result = $this->handleOperation( |
|
57 | 102 | $descriptor->getOperation(), |
|
58 | 102 | $descriptor, |
|
59 | 102 | $executor, |
|
60 | 102 | $eventHandler, |
|
61 | $executionContext |
||
62 | 102 | ); |
|
63 | 59 | } |
|
64 | |||
65 | 64 | return $result; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Return type of this handler |
||
70 | * |
||
71 | * @return int One of RequestDescriptor::RDS_* constant |
||
72 | */ |
||
73 | abstract protected function getHandlerType(); |
||
74 | |||
75 | /** |
||
76 | * Process given operation |
||
77 | * |
||
78 | * @param OperationInterface $operation Operation to process |
||
79 | * @param RequestDescriptor $descriptor Request descriptor |
||
80 | * @param RequestExecutorInterface $executor Executor, processing operation |
||
81 | * @param EventHandlerInterface $eventHandler Event handler for this operation |
||
82 | * @param ExecutionContext $executionContext Execution context |
||
83 | * |
||
84 | * @return OperationInterface|null Next operation to pass in socket. Return null, |
||
85 | * if next operation is not required. Return $operation parameter, if operation is not completed yet |
||
86 | */ |
||
87 | abstract protected function handleOperation( |
||
94 | |||
95 | /** |
||
96 | * Return state which should invoke this operation |
||
97 | * |
||
98 | * @param OperationInterface $operation Operation object |
||
99 | * |
||
100 | * @return int Set of RequestDescriptor::RDS_* constant |
||
101 | */ |
||
102 | 107 | private function getInvokeState(OperationInterface $operation) |
|
111 | |||
112 | /** |
||
113 | * Handle OOB data |
||
114 | * |
||
115 | * @param RequestDescriptor $descriptor Request descriptor |
||
116 | * @param RequestExecutorInterface $executor Executor, processing operation |
||
117 | * @param EventHandlerInterface $eventHandler Event handler for this operation |
||
118 | * @param ExecutionContext $executionContext Execution context |
||
119 | * |
||
120 | * @return OperationInterface|null Operation to return to user or null to continue normal processing |
||
121 | */ |
||
122 | 112 | private function handleOobData( |
|
150 | |||
151 | /** |
||
152 | * Creates transfer rate counter with given parameters |
||
153 | * |
||
154 | * @param string $name Counter name to create |
||
155 | * @param int $minRate Minimum speed setting for counter |
||
156 | * @param int $duration Max duration of low speed |
||
157 | * @param RequestDescriptor $descriptor Request descriptor for counter |
||
158 | * |
||
159 | * @return SpeedRateCounter |
||
160 | */ |
||
161 | 79 | private function createRateCounter($name, $minRate, $duration, RequestDescriptor $descriptor) |
|
176 | |||
177 | /** |
||
178 | * Process transfer bytes counter |
||
179 | * |
||
180 | * @param string $name Counter name operating this transfer |
||
181 | * @param RequestDescriptor $descriptor Socket descriptor |
||
182 | * @param int $bytes Amount of bytes processed by transfer |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | 79 | protected function handleTransferCounter($name, RequestDescriptor $descriptor, $bytes) |
|
218 | |||
219 | /** |
||
220 | * Return metadata for transfer rate counter |
||
221 | * |
||
222 | * @param string $name Counter name |
||
223 | * @param array $meta Socket metadata |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | 79 | private function getTransferCounterMeta($name, array $meta) |
|
254 | } |
||
255 |