1 | <?php |
||
24 | abstract class AbstractOobHandler implements IoHandlerInterface |
||
25 | { |
||
26 | /** |
||
27 | * Operation to descriptor state map |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $stateMap = [ |
||
32 | OperationInterface::OPERATION_READ => RequestDescriptor::RDS_READ, |
||
33 | OperationInterface::OPERATION_WRITE => RequestDescriptor::RDS_WRITE, |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 111 | final public function handle( |
|
40 | RequestDescriptor $descriptor, |
||
41 | RequestExecutorInterface $executor, |
||
42 | EventHandlerInterface $eventHandler |
||
43 | ) { |
||
44 | 111 | $result = $this->handleOobData($descriptor, $executor, $eventHandler); |
|
45 | 111 | if ($result) { |
|
46 | 5 | return $result; |
|
47 | } |
||
48 | |||
49 | 106 | $state = $this->getInvokeState($descriptor->getOperation()); |
|
50 | 106 | if ($descriptor->hasState($state)) { |
|
51 | 101 | $descriptor->clearState($state); |
|
52 | |||
53 | 101 | $result = $this->handleOperation($descriptor, $executor, $eventHandler); |
|
54 | 59 | } |
|
55 | |||
56 | 64 | return $result; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Process given operation |
||
61 | * |
||
62 | * @param RequestDescriptor $descriptor Request descriptor |
||
63 | * @param RequestExecutorInterface $executor Executor, processing operation |
||
64 | * @param EventHandlerInterface $eventHandler Event handler for this operation |
||
65 | * |
||
66 | * @return OperationInterface|null Next operation to pass in socket. Return null, |
||
67 | * if next operation is not required. Return $operation parameter, if operation is not completed yet |
||
68 | */ |
||
69 | abstract protected function handleOperation( |
||
74 | |||
75 | /** |
||
76 | * Return state which should invoke this operation |
||
77 | * |
||
78 | * @param OperationInterface $operation Operation object |
||
79 | * |
||
80 | * @return int RequestDescriptor::RDS_* constant |
||
81 | */ |
||
82 | 106 | private function getInvokeState(OperationInterface $operation) |
|
88 | |||
89 | /** |
||
90 | * Handle OOB data |
||
91 | * |
||
92 | * @param RequestDescriptor $descriptor Request descriptor |
||
93 | * @param RequestExecutorInterface $executor Executor, processing operation |
||
94 | * @param EventHandlerInterface $eventHandler Event handler for this operation |
||
95 | * |
||
96 | * @return OperationInterface|null Operation to return to user or null to continue normal processing |
||
97 | */ |
||
98 | 111 | private function handleOobData( |
|
125 | |||
126 | /** |
||
127 | * Creates transfer rate counter with given parameters |
||
128 | * |
||
129 | * @param string $name Counter name to create |
||
130 | * @param int $minRate Minimum speed setting for counter |
||
131 | * @param int $duration Max duration of low speed |
||
132 | * @param RequestDescriptor $descriptor Request descriptor for counter |
||
133 | * |
||
134 | * @return SpeedRateCounter |
||
135 | */ |
||
136 | 79 | private function createRateCounter($name, $minRate, $duration, RequestDescriptor $descriptor) |
|
151 | |||
152 | /** |
||
153 | * Process transfer bytes counter |
||
154 | * |
||
155 | * @param string $name Counter name operating this transfer |
||
156 | * @param RequestDescriptor $descriptor Socket descriptor |
||
157 | * @param int $bytes Amount of bytes processed by transfer |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | 79 | protected function handleTransferCounter($name, RequestDescriptor $descriptor, $bytes) |
|
193 | |||
194 | /** |
||
195 | * Return metadata for transfer rate counter |
||
196 | * |
||
197 | * @param string $name Counter name |
||
198 | * @param array $meta Socket metadata |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 79 | private function getTransferCounterMeta($name, array $meta) |
|
229 | } |
||
230 |