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