Completed
Branch 0.4-dev (54e67b)
by Evgenij
02:52
created

ReadIoHandler::isEof()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\RequestExecutor\Pipeline;
11
12
use AsyncSockets\Event\AcceptEvent;
13
use AsyncSockets\Event\ReadEvent;
14
use AsyncSockets\Exception\AcceptException;
15
use AsyncSockets\Frame\AcceptedFrame;
16
use AsyncSockets\Frame\FramePickerInterface;
17
use AsyncSockets\Frame\PartialFrame;
18
use AsyncSockets\Operation\OperationInterface;
19
use AsyncSockets\Operation\ReadOperation;
20
use AsyncSockets\RequestExecutor\EventHandlerInterface;
21
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
22
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
23
24
/**
25
 * Class ReadIoHandler
26
 */
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 81
    public function supports(OperationInterface $operation)
45
    {
46 81
        return $operation instanceof ReadOperation;
47
    }
48
49
    /** {@inheritdoc} */
50 48
    protected function handleOperation(
51
        RequestDescriptor $descriptor,
52
        RequestExecutorInterface $executor,
53
        EventHandlerInterface $eventHandler
54
    ) {
55
        /** @var ReadOperation $operation */
56 48
        $operation = $descriptor->getOperation();
57 48
        $socket    = $descriptor->getSocket();
58
59 48
        $meta    = $executor->socketBag()->getSocketMetaData($socket);
60 48
        $context = $meta[RequestExecutorInterface::META_USER_CONTEXT];
61 48
        $result  = null;
62
63 48
        $this->bytesRead       = 0;
64 48
        $this->realFramePicker = $operation->getFramePicker();
65
66
        try {
67 48
            $response = $socket->read($this);
68 44
            switch (true) {
69 44
                case $response instanceof PartialFrame:
70 1
                    $result = $operation;
71 1
                    break;
72 43
                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 38
                default:
85 38
                    $event = new ReadEvent(
86 38
                        $executor,
87 38
                        $socket,
88 38
                        $context,
89 38
                        $response,
90
                        false
91 38
                    );
92
93 38
                    $eventHandler->invokeEvent($event);
94 23
                    $result = $event->getNextOperation();
95 23
                    break;
96 38
            }
97 48
        } 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 26
        $this->appendReadBytes($descriptor, $this->bytesRead);
106 26
        unset($this->realFramePicker, $this->bytesRead);
107
108 26
        return $result;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function isEof()
115
    {
116
        return $this->realFramePicker->isEof();
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122 2
    public function pickUpData($chunk, $remoteAddress)
123
    {
124 2
        $this->bytesRead += strlen($chunk);
125 2
        return $this->realFramePicker->pickUpData($chunk, $remoteAddress);
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function createFrame()
132
    {
133
        return $this->realFramePicker->createFrame();
134
    }
135
136
    /**
137
     * Append given mount of read bytes to descriptor
138
     *
139
     * @param RequestDescriptor $descriptor The descriptor
140
     * @param int               $bytesRead Amount of read bytes
141
     *
142
     * @return void
143
     */
144 48
    private function appendReadBytes(RequestDescriptor $descriptor, $bytesRead)
145
    {
146 48
        $meta = $descriptor->getMetadata();
147 48
        $descriptor->setMetadata(
148 48
            RequestExecutorInterface::META_BYTES_RECEIVED,
149 48
            $meta[RequestExecutorInterface::META_BYTES_RECEIVED] + $bytesRead
150 48
        );
151 48
    }
152
}
153