|
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\PartialFrame; |
|
17
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
|
18
|
|
|
use AsyncSockets\RequestExecutor\IoHandlerInterface; |
|
19
|
|
|
use AsyncSockets\Operation\OperationInterface; |
|
20
|
|
|
use AsyncSockets\Operation\ReadOperation; |
|
21
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
|
22
|
|
|
use AsyncSockets\Socket\SocketInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class ReadIoHandler |
|
26
|
|
|
*/ |
|
27
|
|
|
class ReadIoHandler implements IoHandlerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** {@inheritdoc} */ |
|
30
|
73 |
|
public function supports(OperationInterface $operation) |
|
31
|
|
|
{ |
|
32
|
73 |
|
return $operation instanceof ReadOperation; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** {@inheritdoc} */ |
|
36
|
44 |
|
public function handle( |
|
37
|
|
|
OperationInterface $operation, |
|
38
|
|
|
SocketInterface $socket, |
|
39
|
|
|
RequestExecutorInterface $executor, |
|
40
|
|
|
EventHandlerInterface $eventHandler |
|
41
|
|
|
) { |
|
42
|
44 |
|
$meta = $executor->socketBag()->getSocketMetaData($socket); |
|
43
|
44 |
|
$context = $meta[RequestExecutorInterface::META_USER_CONTEXT]; |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
/** @var ReadOperation $operation */ |
|
47
|
44 |
|
$response = $socket->read($operation->getFramePicker()); |
|
48
|
40 |
|
switch (true) { |
|
49
|
40 |
|
case $response instanceof PartialFrame: |
|
50
|
1 |
|
return $operation; |
|
51
|
39 |
|
case $response instanceof AcceptedFrame: |
|
52
|
5 |
|
$event = new AcceptEvent( |
|
53
|
5 |
|
$executor, |
|
54
|
5 |
|
$socket, |
|
55
|
5 |
|
$context, |
|
56
|
5 |
|
$response->getClientSocket(), |
|
57
|
5 |
|
$response->getRemoteAddress() |
|
58
|
5 |
|
); |
|
59
|
|
|
|
|
60
|
5 |
|
$eventHandler->invokeEvent($event); |
|
61
|
1 |
|
return new ReadOperation(); |
|
62
|
34 |
|
default: |
|
63
|
34 |
|
$event = new ReadEvent( |
|
64
|
34 |
|
$executor, |
|
65
|
34 |
|
$socket, |
|
66
|
34 |
|
$context, |
|
67
|
|
|
$response |
|
68
|
34 |
|
); |
|
69
|
|
|
|
|
70
|
34 |
|
$eventHandler->invokeEvent($event); |
|
71
|
23 |
|
return $event->getNextOperation(); |
|
72
|
34 |
|
} |
|
73
|
19 |
|
} catch (AcceptException $e) { |
|
74
|
1 |
|
return new ReadOperation(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|