Debian Codegraph @

vendor/react/datagram/src/Socket.php

raw
1 <?php
2
3 namespace React\Datagram;
4
5 use React\EventLoop\LoopInterface;
6 use Evenement\EventEmitter;
7 use Exception;
8
9 class Socket extends EventEmitter implements SocketInterface
10 {
11 protected $loop;
12 protected $socket;
13
14 protected $buffer;
15
16 public $bufferSize = 65536;
17
18 /**
19 * @param LoopInterface $loop
20 * @param resource $socket
21 * @param ?Buffer $buffer
22 */
23 public function __construct(LoopInterface $loop, $socket, $buffer = null)
24 {
25 if ($buffer !== null && !$buffer instanceof Buffer) { // manual type check to support legacy PHP < 7.1
26 throw new \InvalidArgumentException('Argument #3 ($buffer) expected null|React\Datagram\Buffer');
27 }
28
29 $this->loop = $loop;
30 $this->socket = $socket;
31
32 if ($buffer === null) {
33 $buffer = new Buffer($loop, $socket);
34 }
35 $this->buffer = $buffer;
36
37 $that = $this;
38 $this->buffer->on('error', function ($error) use ($that) {
39 $that->emit('error', array($error, $that));
40 });
41 $this->buffer->on('close', array($this, 'close'));
42
43 $this->resume();
44 }
45
46 public function getLocalAddress()
47 {
48 if ($this->socket === false) {
49 return null;
50 }
51
52 return $this->sanitizeAddress(@\stream_socket_get_name($this->socket, false));
53 }
54
55 public function getRemoteAddress()
56 {
57 if ($this->socket === false) {
58 return null;
59 }
60
61 return $this->sanitizeAddress(@\stream_socket_get_name($this->socket, true));
62 }
63
64 public function send($data, $remoteAddress = null)
65 {
66 $this->buffer->send($data, $remoteAddress);
67 }
68
69 public function pause()
70 {
71 $this->loop->removeReadStream($this->socket);
72 }
73
74 public function resume()
75 {
76 if ($this->socket !== false) {
77 $this->loop->addReadStream($this->socket, array($this, 'onReceive'));
78 }
79 }
80
81 public function onReceive()
82 {
83 try {
84 $data = $this->handleReceive($peer);
85 }
86 catch (Exception $e) {
87 // emit error message and local socket
88 $this->emit('error', array($e, $this));
89 return;
90 }
91
92 $this->emit('message', array($data, $peer, $this));
93 }
94
95 public function close()
96 {
97 if ($this->socket === false) {
98 return;
99 }
100
101 $this->emit('close', array($this));
102 $this->pause();
103
104 $this->handleClose();
105 $this->socket = false;
106 $this->buffer->close();
107
108 $this->removeAllListeners();
109 }
110
111 public function end()
112 {
113 $this->buffer->end();
114 }
115
116 private function sanitizeAddress($address)
117 {
118 if ($address === false) {
119 return null;
120 }
121
122 // check if this is an IPv6 address which includes multiple colons but no square brackets (PHP < 7.3)
123 $pos = \strrpos($address, ':');
124 if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') {
125 $address = '[' . \substr($address, 0, $pos) . ']:' . \substr($address, $pos + 1); // @codeCoverageIgnore
126 }
127 return $address;
128 }
129
130 protected function handleReceive(&$peerAddress)
131 {
132 $data = \stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peerAddress);
133
134 if ($data === false) {
135 // receiving data failed => remote side rejected one of our packets
136 // due to the nature of UDP, there's no way to tell which one exactly
137 // $peer is not filled either
138
139 throw new Exception('Invalid message');
140 }
141
142 $peerAddress = $this->sanitizeAddress($peerAddress);
143
144 return $data;
145 }
146
147 protected function handleClose()
148 {
149 \fclose($this->socket);
150 }
151 }