1 | <?php |
||
26 | class WriteIoHandler extends AbstractOobHandler |
||
27 | { |
||
28 | /** {@inheritdoc} */ |
||
29 | 42 | public function supports(OperationInterface $operation) |
|
33 | |||
34 | /** {@inheritdoc} */ |
||
35 | 47 | protected function handleOperation( |
|
36 | OperationInterface $operation, |
||
37 | RequestDescriptor $descriptor, |
||
38 | RequestExecutorInterface $executor, |
||
39 | EventHandlerInterface $eventHandler, |
||
40 | ExecutionContext $executionContext |
||
41 | ) { |
||
42 | 47 | $socket = $descriptor->getSocket(); |
|
43 | |||
44 | /** @var WriteOperation $operation */ |
||
45 | 47 | $fireEvent = !($operation instanceof InProgressWriteOperation); |
|
46 | |||
47 | 47 | if ($fireEvent) { |
|
48 | 46 | $meta = $executor->socketBag()->getSocketMetaData($socket); |
|
49 | 46 | $event = new WriteEvent( |
|
50 | 46 | $operation, |
|
51 | 46 | $executor, |
|
52 | 46 | $socket, |
|
53 | 46 | $meta[ RequestExecutorInterface::META_USER_CONTEXT ] |
|
54 | 46 | ); |
|
55 | 46 | $eventHandler->invokeEvent($event, $executor, $descriptor->getSocket(), $executionContext); |
|
56 | 31 | $nextOperation = $event->getNextOperation(); |
|
57 | 31 | } else { |
|
58 | 1 | $nextOperation = $operation; |
|
59 | } |
||
60 | |||
61 | 32 | $result = $this->writeDataToSocket($operation, $socket, $nextOperation, $bytesWritten); |
|
62 | 28 | $this->handleTransferCounter(RequestDescriptor::COUNTER_SEND_MIN_RATE, $descriptor, $bytesWritten); |
|
63 | |||
64 | 28 | return $result; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Perform data writing to socket and return suitable next socket operation |
||
69 | * |
||
70 | * @param WriteOperation $operation Current write operation instance |
||
71 | * @param SocketInterface $socket Socket object |
||
72 | * @param OperationInterface $nextOperation Desirable next operation |
||
73 | * @param int $bytesWritten Amount of written bytes |
||
74 | * |
||
75 | * @return OperationInterface Actual next operation |
||
76 | */ |
||
77 | 32 | private function writeDataToSocket( |
|
113 | |||
114 | /** |
||
115 | * Marks write operation as in progress |
||
116 | * |
||
117 | * @param WriteOperation $operation Current operation object |
||
118 | * @param OperationInterface $nextOperation Next planned operation |
||
119 | * |
||
120 | * @return InProgressWriteOperation Next operation object |
||
121 | */ |
||
122 | 2 | private function makeInProgressWriteOperation(WriteOperation $operation, OperationInterface $nextOperation = null) |
|
131 | |||
132 | /** |
||
133 | * Return iterator for writing operation |
||
134 | * |
||
135 | * @param WriteOperation $operation The operation |
||
136 | * |
||
137 | * @return PushbackIterator |
||
138 | */ |
||
139 | 32 | private function resolveDataIterator(WriteOperation $operation) |
|
155 | |||
156 | /** |
||
157 | * Converts data to Traversable object |
||
158 | * |
||
159 | * @param mixed $data Data to convert into object |
||
160 | * |
||
161 | * @return \Iterator |
||
162 | * @throws \LogicException If data can not be converted to \Traversable |
||
163 | */ |
||
164 | 28 | private function dataToIterator($data) |
|
182 | |||
183 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | 48 | protected function getHandlerType() |
|
190 | } |
||
191 |