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\Exception\SslHandshakeException; |
13
|
|
|
use AsyncSockets\Operation\OperationInterface; |
14
|
|
|
use AsyncSockets\Operation\SslHandshakeOperation; |
15
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
16
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
17
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class SslHandshakeIoHandler |
21
|
|
|
*/ |
22
|
|
|
class SslHandshakeIoHandler extends AbstractOobHandler |
23
|
|
|
{ |
24
|
|
|
/** {@inheritdoc} */ |
25
|
|
|
public function supports(OperationInterface $operation) |
26
|
1 |
|
{ |
27
|
|
|
return $operation instanceof SslHandshakeOperation; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
31
|
|
|
protected function handleOperation( |
32
|
3 |
|
RequestDescriptor $descriptor, |
33
|
|
|
RequestExecutorInterface $executor, |
34
|
|
|
EventHandlerInterface $eventHandler |
35
|
|
|
) { |
36
|
|
|
$operation = $descriptor->getOperation(); |
37
|
|
|
$socket = $descriptor->getSocket(); |
38
|
|
|
|
39
|
3 |
|
/** @var SslHandshakeOperation $operation */ |
40
|
3 |
|
$resource = $descriptor->getSocket()->getStreamResource(); |
41
|
3 |
|
$result = stream_socket_enable_crypto($resource, true, $operation->getCipher()); |
42
|
1 |
|
if ($result === true) { |
43
|
2 |
|
return $operation->getNextOperation(); |
44
|
1 |
|
} elseif ($result === false) { |
45
|
|
|
throw new SslHandshakeException($socket, 'SSL handshake failed.'); |
46
|
|
|
} |
47
|
1 |
|
|
48
|
|
|
return $operation; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|