1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, 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
|
83 |
|
public function supports(OperationInterface $operation) |
45
|
|
|
{ |
46
|
83 |
|
return $operation instanceof ReadOperation; |
47
|
|
|
} |
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) |
120
|
|
|
{ |
121
|
51 |
|
$this->handleTransferCounter(RequestDescriptor::COUNTER_RECV_MIN_RATE, $descriptor, $bytesRead); |
122
|
50 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function isEof() |
128
|
|
|
{ |
129
|
|
|
return $this->realFramePicker->isEof(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
3 |
|
public function pickUpData($chunk, $remoteAddress) |
136
|
|
|
{ |
137
|
3 |
|
$this->bytesRead += strlen($chunk); |
138
|
3 |
|
return $this->realFramePicker->pickUpData($chunk, $remoteAddress); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function createFrame() |
145
|
|
|
{ |
146
|
1 |
|
return $this->realFramePicker->createFrame(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|