-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathOpportunisticTlsConnection.php
More file actions
109 lines (89 loc) · 2.64 KB
/
OpportunisticTlsConnection.php
File metadata and controls
109 lines (89 loc) · 2.64 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace React\Socket;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use React\Stream\DuplexResourceStream;
use React\Stream\Util;
use React\Stream\WritableResourceStream;
use React\Stream\WritableStreamInterface;
/**
* The actual connection implementation for StartTlsConnectionInterface
*
* This class should only be used internally, see StartTlsConnectionInterface instead.
*
* @see OpportunisticTlsConnectionInterface
* @internal
*/
class OpportunisticTlsConnection extends EventEmitter implements OpportunisticTlsConnectionInterface
{
/** @var Connection */
private $connection;
/** @var StreamEncryption */
private $streamEncryption;
/** @var string */
private $uri;
public function __construct(Connection $connection, StreamEncryption $streamEncryption, $uri)
{
$this->connection = $connection;
$this->streamEncryption = $streamEncryption;
$this->uri = $uri;
Util::forwardEvents($connection, $this, array('data', 'end', 'error', 'close'));
}
public function getRemoteAddress()
{
return $this->connection->getRemoteAddress();
}
public function getLocalAddress()
{
return $this->connection->getLocalAddress();
}
public function isReadable()
{
return $this->connection->isReadable();
}
public function pause()
{
$this->connection->pause();
}
public function resume()
{
$this->connection->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
return $this->connection->pipe($dest, $options);
}
public function close()
{
$this->connection->close();
}
public function enableEncryption()
{
$that = $this;
$connection = $this->connection;
$uri = $this->uri;
return $this->streamEncryption->enable($connection)->then(function () use ($that) {
return $that;
}, function ($error) use ($connection, $uri) {
// establishing encryption failed => close invalid connection and return error
$connection->close();
throw new \RuntimeException(
'Connection to ' . $uri . ' failed during TLS handshake: ' . $error->getMessage(),
$error->getCode()
);
});
}
public function isWritable()
{
return $this->connection->isWritable();
}
public function write($data)
{
return $this->connection->write($data);
}
public function end($data = null)
{
$this->connection->end($data);
}
}