-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathSecureConnectionManager.php
33 lines (28 loc) · 1.09 KB
/
SecureConnectionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
namespace React\SocketClient;
use React\EventLoop\LoopInterface;
use React\Stream\Stream;
class SecureConnectionManager implements ConnectionManagerInterface
{
private $connectionManager;
private $loop;
private $streamEncryption;
public function __construct(ConnectionManagerInterface $connectionManager, LoopInterface $loop)
{
$this->connectionManager = $connectionManager;
$this->loop = $loop;
$this->streamEncryption = new StreamEncryption($loop);
}
public function getConnection($host, $port)
{
$streamEncryption = $this->streamEncryption;
return $this->connectionManager->getConnection($host, $port)->then(function (Stream $stream) use ($streamEncryption) {
// (unencrypted) connection succeeded => try to enable encryption
return $streamEncryption->enable($stream)->then(null, function ($error) use ($stream) {
// establishing encryption failed => close invalid connection and return error
$stream->close();
throw $error;
});
});
}
}