1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSSC\Components; |
4
|
|
|
|
5
|
|
|
use WSSC\Contracts\CommonsContract; |
6
|
|
|
use WSSC\Contracts\ConnectionContract; |
7
|
|
|
use WSSC\Contracts\WebSocketServerContract; |
8
|
|
|
|
9
|
|
|
class Connection implements ConnectionContract, CommonsContract |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
private $socketConnection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param $sockConn |
16
|
|
|
* @return $this |
17
|
|
|
*/ |
18
|
|
|
public function getConnection($sockConn) : self |
19
|
|
|
{ |
20
|
|
|
$this->socketConnection = $sockConn; |
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Closes clients socket stream |
26
|
|
|
*/ |
27
|
|
|
public function close() : void |
28
|
|
|
{ |
29
|
|
|
if (is_resource($this->socketConnection)) { |
30
|
|
|
fclose($this->socketConnection); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This method is invoked when user implementation call $conn->send($data) |
36
|
|
|
* writes data to the clients stream socket |
37
|
|
|
* |
38
|
|
|
* @param string $data pure decoded data from server |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function send($data) : void |
42
|
|
|
{ |
43
|
|
|
fwrite($this->socketConnection, $this->encode($data)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Encodes data before writing to the client socket stream |
48
|
|
|
* |
49
|
|
|
* @param string $payload |
50
|
|
|
* @param string $type |
51
|
|
|
* @param boolean $masked |
52
|
|
|
* @return mixed |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
private function encode($payload, string $type = self::EVENT_TYPE_TEXT, bool $masked = false) |
56
|
|
|
{ |
57
|
|
|
$frameHead = []; |
58
|
|
|
$payloadLength = strlen($payload); |
59
|
|
|
|
60
|
|
|
switch ($type) { |
61
|
|
|
case self::EVENT_TYPE_TEXT: |
62
|
|
|
// first byte indicates FIN, Text-Frame (10000001): |
63
|
|
|
$frameHead[0] = self::ENCODE_TEXT; |
64
|
|
|
break; |
65
|
|
|
|
66
|
|
|
case self::EVENT_TYPE_CLOSE: |
67
|
|
|
// first byte indicates FIN, Close Frame(10001000): |
68
|
|
|
$frameHead[0] = self::ENCODE_CLOSE; |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case self::EVENT_TYPE_PING: |
72
|
|
|
// first byte indicates FIN, Ping frame (10001001): |
73
|
|
|
$frameHead[0] = self::ENCODE_PING; |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
case self::EVENT_TYPE_PONG: |
77
|
|
|
// first byte indicates FIN, Pong frame (10001010): |
78
|
|
|
$frameHead[0] = self::ENCODE_PONG; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// set mask and payload length (using 1, 3 or 9 bytes) |
83
|
|
|
if ($payloadLength > self::PAYLOAD_MAX_BITS) { |
84
|
|
|
$payloadLengthBin = str_split(sprintf('%064b', $payloadLength), self::PAYLOAD_CHUNK); |
85
|
|
|
$frameHead[1] = ($masked === true) ? self::MASK_255 : self::MASK_127; |
86
|
|
|
for ($i = 0; $i < 8; $i++) { |
87
|
|
|
$frameHead[$i + 2] = bindec($payloadLengthBin[$i]); |
88
|
|
|
} |
89
|
|
|
// most significant bit MUST be 0 |
90
|
|
|
if ($frameHead[2] > self::MASK_127) { |
91
|
|
|
return ['type' => $type, 'payload' => $payload, 'error' => WebSocketServerContract::ERR_FRAME_TOO_LARGE]; |
92
|
|
|
} |
93
|
|
|
} elseif ($payloadLength > self::MASK_125) { |
94
|
|
|
$payloadLengthBin = str_split(sprintf('%016b', $payloadLength), self::PAYLOAD_CHUNK); |
95
|
|
|
$frameHead[1] = ($masked === true) ? self::MASK_254 : self::MASK_126; |
96
|
|
|
$frameHead[2] = bindec($payloadLengthBin[0]); |
97
|
|
|
$frameHead[3] = bindec($payloadLengthBin[1]); |
98
|
|
|
} else { |
99
|
|
|
$frameHead[1] = ($masked === true) ? $payloadLength + self::MASK_128 : $payloadLength; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->getComposedFrame($frameHead, $payload, $payloadLength, $masked); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function getComposedFrame(array $frameHead, string $payload, int $payloadLength, bool $masked) |
106
|
|
|
{ |
107
|
|
|
// convert frame-head to string: |
108
|
|
|
foreach (array_keys($frameHead) as $i) { |
109
|
|
|
$frameHead[$i] = chr($frameHead[$i]); |
110
|
|
|
} |
111
|
|
|
// generate a random mask: |
112
|
|
|
$mask = []; |
113
|
|
|
if ($masked === true) { |
114
|
|
View Code Duplication |
for ($i = 0; $i < 4; $i++) { |
|
|
|
|
115
|
|
|
$mask[$i] = chr(random_int(0, self::MASK_255)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$frameHead = array_merge($frameHead, $mask); |
119
|
|
|
} |
120
|
|
|
$frame = implode('', $frameHead); |
121
|
|
|
|
122
|
|
|
// append payload to frame: |
123
|
|
View Code Duplication |
for ($i = 0; $i < $payloadLength; $i++) { |
|
|
|
|
124
|
|
|
$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $frame; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.