1 | <?php |
||
28 | class ReadIoHandler extends AbstractOobHandler implements FramePickerInterface |
||
29 | { |
||
30 | /** |
||
31 | * Amount of bytes read by last operation |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | private $bytesRead; |
||
36 | |||
37 | /** |
||
38 | * Actual frame picker |
||
39 | * |
||
40 | * @var FramePickerInterface |
||
41 | */ |
||
42 | private $realFramePicker; |
||
43 | |||
44 | /** {@inheritdoc} */ |
||
45 | 83 | public function supports(OperationInterface $operation) |
|
49 | |||
50 | /** {@inheritdoc} */ |
||
51 | 51 | protected function handleOperation( |
|
52 | OperationInterface $operation, |
||
53 | RequestDescriptor $descriptor, |
||
54 | RequestExecutorInterface $executor, |
||
55 | EventHandlerInterface $eventHandler, |
||
56 | ExecutionContext $executionContext |
||
57 | ) { |
||
58 | /** @var ReadOperation $operation */ |
||
59 | 51 | $socket = $descriptor->getSocket(); |
|
60 | |||
61 | 51 | $meta = $executor->socketBag()->getSocketMetaData($socket); |
|
62 | 51 | $context = $meta[RequestExecutorInterface::META_USER_CONTEXT]; |
|
63 | 51 | $result = null; |
|
64 | |||
65 | 51 | $this->bytesRead = 0; |
|
66 | 51 | $this->realFramePicker = $operation->getFramePicker(); |
|
67 | |||
68 | try { |
||
69 | 51 | $response = $socket->read($this); |
|
70 | 47 | switch (true) { |
|
71 | 47 | case $response instanceof PartialFrame: |
|
72 | 2 | $result = $operation; |
|
73 | 2 | break; |
|
74 | 45 | case $response instanceof AcceptedFrame: |
|
75 | 5 | $event = new AcceptEvent( |
|
76 | 5 | $executor, |
|
77 | 5 | $socket, |
|
78 | 5 | $context, |
|
79 | 5 | $response->getClientSocket(), |
|
80 | 5 | $response->getRemoteAddress() |
|
81 | 5 | ); |
|
82 | |||
83 | 5 | $eventHandler->invokeEvent($event, $executor, $socket, $executionContext); |
|
84 | 1 | $result = new ReadOperation(); |
|
85 | 1 | break; |
|
86 | 40 | default: |
|
87 | 40 | $event = new ReadEvent( |
|
88 | 40 | $executor, |
|
89 | 40 | $socket, |
|
90 | 40 | $context, |
|
91 | 40 | $response, |
|
92 | false |
||
93 | 40 | ); |
|
94 | |||
95 | 40 | $eventHandler->invokeEvent($event, $executor, $socket, $executionContext); |
|
96 | 25 | $result = $event->getNextOperation(); |
|
97 | 25 | break; |
|
98 | 40 | } |
|
99 | 51 | } catch (AcceptException $e) { |
|
100 | 1 | $result = new ReadOperation(); |
|
101 | 23 | } catch (\Exception $e) { |
|
102 | 22 | $this->appendReadBytes($descriptor, $this->bytesRead); |
|
103 | 22 | unset($this->realFramePicker, $this->bytesRead); |
|
104 | 22 | throw $e; |
|
105 | } |
||
106 | |||
107 | 29 | $this->appendReadBytes($descriptor, $this->bytesRead); |
|
108 | 28 | unset($this->realFramePicker, $this->bytesRead); |
|
109 | |||
110 | 28 | return $result; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Append given mount of read bytes to descriptor |
||
115 | * |
||
116 | * @param RequestDescriptor $descriptor The descriptor |
||
117 | * @param int $bytesRead Amount of read bytes |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | 51 | private function appendReadBytes(RequestDescriptor $descriptor, $bytesRead) |
|
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | public function isEof() |
||
133 | |||
134 | /** |
||
135 | * {@inheritDoc} |
||
136 | */ |
||
137 | 3 | public function pickUpData($chunk, $remoteAddress) |
|
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | 1 | public function createFrame() |
|
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | */ |
||
154 | 52 | protected function getHandlerType() |
|
158 | } |
||
159 |