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