forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecureConnection.php
More file actions
57 lines (49 loc) · 1.46 KB
/
SecureConnection.php
File metadata and controls
57 lines (49 loc) · 1.46 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
<?php
namespace React\Socket;
/**
* A connection supporting transport layer security.
*/
class SecureConnection extends Connection
{
protected $isSecure = false;
protected $protocolNumber;
public function handleData($stream)
{
if (! $this->isSecure) {
$enabled = stream_socket_enable_crypto($stream, true, $this->protocolNumber);
if ($enabled === false) {
$this
->err('Failed to complete a secure handshake with the client.')
->end()
;
return;
} elseif ($enabled === 0) {
return;
}
$this->isSecure = true;
$this->emit('connection', array($this));
}
$data = fread($stream, $this->bufferSize);
if ('' !== $data && false !== $data) {
$this->emit('data', array($data, $this));
}
if (false === $data || !is_resource($stream) || feof($stream)) {
$this->end();
}
}
/**
* Set the STREAM_CRYPTO_METHOD_*_SERVER flags suitable for enabling TLS on
* the socket stream handled by this connection.
*
* @param int $protocolNumber
*/
public function setProtocol($protocolNumber = null)
{
$this->protocolNumber = $protocolNumber;
}
private function err($message)
{
$this->emit('error', array(new \RuntimeException($message)));
return $this;
}
}