-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSocket.php
More file actions
135 lines (106 loc) · 3.25 KB
/
Socket.php
File metadata and controls
135 lines (106 loc) · 3.25 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace React\Datagram;
use React\EventLoop\LoopInterface;
use Evenement\EventEmitter;
use Exception;
class Socket extends EventEmitter implements SocketInterface
{
protected $loop;
protected $socket;
protected $buffer;
public $bufferSize = 65536;
public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null)
{
$this->loop = $loop;
$this->socket = $socket;
if ($buffer === null) {
$buffer = new Buffer($loop, $socket);
}
$this->buffer = $buffer;
$that = $this;
$this->buffer->on('error', function ($error) use ($that) {
$that->emit('error', array($error, $that));
});
$this->buffer->on('close', array($this, 'close'));
$this->resume();
}
public function getLocalAddress()
{
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false));
}
public function getRemoteAddress()
{
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, true));
}
public function send($data, $remoteAddress = null)
{
$this->buffer->send($data, $remoteAddress);
}
public function pause()
{
$this->loop->removeReadStream($this->socket);
}
public function resume()
{
if ($this->socket !== false) {
$this->loop->addReadStream($this->socket, array($this, 'onReceive'));
}
}
public function onReceive()
{
try {
list($data, $peer) = $this->handleReceive();
}
catch (Exception $e) {
// emit error message and local socket
$this->emit('error', array($e, $this));
return;
}
$this->emit('message', array($data, $peer, $this));
}
public function close()
{
if ($this->socket === false) {
return;
}
$this->emit('close', array($this));
$this->pause();
$this->handleClose();
$this->socket = false;
$this->buffer->close();
$this->removeAllListeners();
}
public function end()
{
$this->buffer->end();
}
private function sanitizeAddress($address)
{
if ($address === false) {
return null;
}
// this is an IPv6 address which includes colons but no square brackets
$pos = strrpos($address, ':');
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
$port = substr($address, $pos + 1);
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
}
return $address;
}
protected function handleReceive()
{
$data = stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peerAddress);
if ($data === false) {
// receiving data failed => remote side rejected one of our packets
// due to the nature of UDP, there's no way to tell which one exactly
// $peer is not filled either
throw new Exception('Invalid message');
}
$peerAddress = $this->sanitizeAddress($peerAddress);
return array($data, $peerAddress);
}
protected function handleClose()
{
fclose($this->socket);
}
}