@@ -1,8 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | class MongoClientAsyncCollection { |
3 | - /** |
|
4 | - * @var MongoClient |
|
5 | - */ |
|
3 | + /** |
|
4 | + * @var MongoClient |
|
5 | + */ |
|
6 | 6 | public $pool; |
7 | 7 | public $name; // Name of collection. |
8 | 8 | |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * @param array Hash of properties (offset, opts, where, col, fields, sort, hint, explain, snapshot, orderby, parse_oplog) |
37 | 37 | * @param string Optional. Distribution key. |
38 | 38 | * @return void |
39 | - */ |
|
39 | + */ |
|
40 | 40 | public function findOne($cb, $p = array(), $key = '') { |
41 | 41 | $p['col'] = $this->name; |
42 | 42 | |
@@ -138,34 +138,34 @@ discard block |
||
138 | 138 | return $this->pool->remove($this->name, $cond, $cb, $key); |
139 | 139 | } |
140 | 140 | |
141 | - /** |
|
142 | - * Evaluates a code on the server side |
|
143 | - * @param string Code |
|
144 | - * @param mixed Callback called when response received |
|
145 | - * @param string Optional. Distribution key |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - public function evaluate($code, $cb, $key = '') |
|
149 | - { |
|
150 | - $this->pool->evaluate($code, $cb, $key); |
|
151 | - } |
|
141 | + /** |
|
142 | + * Evaluates a code on the server side |
|
143 | + * @param string Code |
|
144 | + * @param mixed Callback called when response received |
|
145 | + * @param string Optional. Distribution key |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + public function evaluate($code, $cb, $key = '') |
|
149 | + { |
|
150 | + $this->pool->evaluate($code, $cb, $key); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Generation autoincrement |
|
155 | - * @param Closure $cb called when response received |
|
156 | - * @param string $key Optional. Distribution key |
|
157 | - * @return void |
|
158 | - */ |
|
159 | - public function autoincrement($cb, $key = '') |
|
160 | - { |
|
161 | - $this->evaluate( |
|
162 | - 'function () { ' |
|
163 | - . 'return db.autoincrement.findAndModify({ ' |
|
164 | - . 'query: {"_id":"' . $this->name . '"}, update: {$inc:{"id":1}}, new: true, upsert: true }); }', |
|
165 | - function ($res) use ($cb) { |
|
166 | - call_user_func($cb, $res); |
|
167 | - }, |
|
168 | - $key |
|
169 | - ); |
|
170 | - } |
|
153 | + /** |
|
154 | + * Generation autoincrement |
|
155 | + * @param Closure $cb called when response received |
|
156 | + * @param string $key Optional. Distribution key |
|
157 | + * @return void |
|
158 | + */ |
|
159 | + public function autoincrement($cb, $key = '') |
|
160 | + { |
|
161 | + $this->evaluate( |
|
162 | + 'function () { ' |
|
163 | + . 'return db.autoincrement.findAndModify({ ' |
|
164 | + . 'query: {"_id":"' . $this->name . '"}, update: {$inc:{"id":1}}, new: true, upsert: true }); }', |
|
165 | + function ($res) use ($cb) { |
|
166 | + call_user_func($cb, $res); |
|
167 | + }, |
|
168 | + $key |
|
169 | + ); |
|
170 | + } |
|
171 | 171 | } |
@@ -7,160 +7,160 @@ |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | class WebSocketProtocolVE extends WebSocketProtocol { |
10 | - const STRING = 0x00; |
|
11 | - const BINARY = 0x80; |
|
10 | + const STRING = 0x00; |
|
11 | + const BINARY = 0x80; |
|
12 | 12 | |
13 | - public function onHandshake() { |
|
14 | - return true; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * Returns handshaked data for reply |
|
19 | - * @param string Received data (no use in this class) |
|
20 | - * @return string Handshaked data |
|
21 | - */ |
|
22 | - |
|
23 | - public function getHandshakeReply($data) { |
|
24 | - if ($this->onHandshake()) { |
|
25 | - if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'])) { |
|
26 | - $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = ''; |
|
27 | - } |
|
28 | - |
|
29 | - $reply = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" |
|
30 | - . "Upgrade: WebSocket\r\n" |
|
31 | - . "Connection: Upgrade\r\n" |
|
32 | - . "Sec-WebSocket-Origin: " . $this->connection->server['HTTP_ORIGIN'] . "\r\n" |
|
33 | - . "Sec-WebSocket-Location: ws://" . $this->connection->server['HTTP_HOST'] . $this->connection->server['REQUEST_URI'] . "\r\n"; |
|
13 | + public function onHandshake() { |
|
14 | + return true; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * Returns handshaked data for reply |
|
19 | + * @param string Received data (no use in this class) |
|
20 | + * @return string Handshaked data |
|
21 | + */ |
|
22 | + |
|
23 | + public function getHandshakeReply($data) { |
|
24 | + if ($this->onHandshake()) { |
|
25 | + if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'])) { |
|
26 | + $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = ''; |
|
27 | + } |
|
28 | + |
|
29 | + $reply = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" |
|
30 | + . "Upgrade: WebSocket\r\n" |
|
31 | + . "Connection: Upgrade\r\n" |
|
32 | + . "Sec-WebSocket-Origin: " . $this->connection->server['HTTP_ORIGIN'] . "\r\n" |
|
33 | + . "Sec-WebSocket-Location: ws://" . $this->connection->server['HTTP_HOST'] . $this->connection->server['REQUEST_URI'] . "\r\n"; |
|
34 | 34 | if ($this->connection->pool->config->expose->value) { |
35 | 35 | $reply .= 'X-Powered-By: phpDaemon/' . Daemon::$version . "\r\n"; |
36 | 36 | } |
37 | - if (isset($this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'])) { |
|
38 | - $reply .= "Sec-WebSocket-Protocol: " . $this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'] . "\r\n"; |
|
39 | - } |
|
40 | - $reply .= "\r\n"; |
|
41 | - return $reply; |
|
42 | - } |
|
43 | - return false; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Computes key for Sec-WebSocket. |
|
48 | - * @param string Key |
|
49 | - * @return string Result |
|
50 | - */ |
|
51 | - |
|
52 | - protected function _computeKey($key) { |
|
53 | - $spaces = 0; |
|
54 | - $digits = ''; |
|
37 | + if (isset($this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'])) { |
|
38 | + $reply .= "Sec-WebSocket-Protocol: " . $this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'] . "\r\n"; |
|
39 | + } |
|
40 | + $reply .= "\r\n"; |
|
41 | + return $reply; |
|
42 | + } |
|
43 | + return false; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Computes key for Sec-WebSocket. |
|
48 | + * @param string Key |
|
49 | + * @return string Result |
|
50 | + */ |
|
51 | + |
|
52 | + protected function _computeKey($key) { |
|
53 | + $spaces = 0; |
|
54 | + $digits = ''; |
|
55 | 55 | |
56 | - for ($i = 0, $s = strlen($key); $i < $s; ++$i) { |
|
57 | - $c = binarySubstr($key, $i, 1); |
|
58 | - |
|
59 | - if ($c === "\x20") { |
|
60 | - ++$spaces; |
|
61 | - } |
|
62 | - elseif (ctype_digit($c)) { |
|
63 | - $digits .= $c; |
|
64 | - } |
|
65 | - } |
|
56 | + for ($i = 0, $s = strlen($key); $i < $s; ++$i) { |
|
57 | + $c = binarySubstr($key, $i, 1); |
|
58 | + |
|
59 | + if ($c === "\x20") { |
|
60 | + ++$spaces; |
|
61 | + } |
|
62 | + elseif (ctype_digit($c)) { |
|
63 | + $digits .= $c; |
|
64 | + } |
|
65 | + } |
|
66 | 66 | |
67 | - if ($spaces > 0) { |
|
68 | - $result = (float) floor($digits / $spaces); |
|
69 | - } else { |
|
70 | - $result = (float) $digits; |
|
71 | - } |
|
67 | + if ($spaces > 0) { |
|
68 | + $result = (float) floor($digits / $spaces); |
|
69 | + } else { |
|
70 | + $result = (float) $digits; |
|
71 | + } |
|
72 | 72 | |
73 | - return pack('N', $result); |
|
74 | - } |
|
75 | - |
|
76 | - public function encodeFrame($data, $type) { |
|
77 | - // Binary |
|
78 | - $type = $this->getFrameType($type); |
|
79 | - if (($type & self::BINARY) === self::BINARY) |
|
80 | - { |
|
81 | - $n = strlen($data); |
|
82 | - $len = ''; |
|
83 | - $pos = 0; |
|
84 | - |
|
85 | - char: |
|
86 | - |
|
87 | - ++$pos; |
|
88 | - $c = $n >> 0 & 0x7F; |
|
89 | - $n = $n >> 7; |
|
90 | - |
|
91 | - if ($pos != 1) |
|
92 | - { |
|
93 | - $c += 0x80; |
|
94 | - } |
|
73 | + return pack('N', $result); |
|
74 | + } |
|
75 | + |
|
76 | + public function encodeFrame($data, $type) { |
|
77 | + // Binary |
|
78 | + $type = $this->getFrameType($type); |
|
79 | + if (($type & self::BINARY) === self::BINARY) |
|
80 | + { |
|
81 | + $n = strlen($data); |
|
82 | + $len = ''; |
|
83 | + $pos = 0; |
|
84 | + |
|
85 | + char: |
|
86 | + |
|
87 | + ++$pos; |
|
88 | + $c = $n >> 0 & 0x7F; |
|
89 | + $n = $n >> 7; |
|
90 | + |
|
91 | + if ($pos != 1) |
|
92 | + { |
|
93 | + $c += 0x80; |
|
94 | + } |
|
95 | 95 | |
96 | - if ($c != 0x80) |
|
97 | - { |
|
98 | - $len = chr($c) . $len; |
|
99 | - goto char; |
|
100 | - }; |
|
96 | + if ($c != 0x80) |
|
97 | + { |
|
98 | + $len = chr($c) . $len; |
|
99 | + goto char; |
|
100 | + }; |
|
101 | 101 | |
102 | - return chr(self::BINARY) . $len . $data ; |
|
103 | - } |
|
104 | - // String |
|
105 | - else { |
|
106 | - return chr(self::STRING) . $data . "\xFF" ; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - public function onRead() { |
|
102 | + return chr(self::BINARY) . $len . $data ; |
|
103 | + } |
|
104 | + // String |
|
105 | + else { |
|
106 | + return chr(self::STRING) . $data . "\xFF" ; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + public function onRead() { |
|
111 | 111 | while ($this->connection && (($buflen = strlen($this->connection->buf)) >= 2)) { |
112 | 112 | $frametype = ord(binarySubstr($this->connection->buf, 0, 1)); |
113 | - if (($frametype & 0x80) === 0x80) { |
|
114 | - $len = 0 ; |
|
115 | - $i = 0 ; |
|
116 | - |
|
117 | - do { |
|
118 | - $b = ord(binarySubstr($this->connection->buf, ++$i, 1)); |
|
119 | - $n = $b & 0x7F ; |
|
120 | - $len *= 0x80 ; |
|
121 | - $len += $n ; |
|
122 | - } while ($b > 0x80) ; |
|
123 | - |
|
124 | - if ($this->connection->pool->maxAllowedPacket <= $len) |
|
125 | - { |
|
126 | - // Too big packet |
|
127 | - $this->connection->finish() ; |
|
128 | - return FALSE ; |
|
129 | - } |
|
130 | - |
|
131 | - if ($buflen < $len + 2) |
|
132 | - { |
|
133 | - // not enough data yet |
|
134 | - return FALSE ; |
|
135 | - } |
|
113 | + if (($frametype & 0x80) === 0x80) { |
|
114 | + $len = 0 ; |
|
115 | + $i = 0 ; |
|
116 | + |
|
117 | + do { |
|
118 | + $b = ord(binarySubstr($this->connection->buf, ++$i, 1)); |
|
119 | + $n = $b & 0x7F ; |
|
120 | + $len *= 0x80 ; |
|
121 | + $len += $n ; |
|
122 | + } while ($b > 0x80) ; |
|
123 | + |
|
124 | + if ($this->connection->pool->maxAllowedPacket <= $len) |
|
125 | + { |
|
126 | + // Too big packet |
|
127 | + $this->connection->finish() ; |
|
128 | + return FALSE ; |
|
129 | + } |
|
130 | + |
|
131 | + if ($buflen < $len + 2) |
|
132 | + { |
|
133 | + // not enough data yet |
|
134 | + return FALSE ; |
|
135 | + } |
|
136 | 136 | |
137 | - $decodedData = binarySubstr($this->connection->buf, 2, $len) ; |
|
138 | - $this->connection->buf = binarySubstr($this->connection->buf, 2 + $len) ; |
|
137 | + $decodedData = binarySubstr($this->connection->buf, 2, $len) ; |
|
138 | + $this->connection->buf = binarySubstr($this->connection->buf, 2 + $len) ; |
|
139 | 139 | |
140 | - $this->connection->onFrame($decodedData, $frametype); |
|
141 | - } |
|
142 | - else { |
|
143 | - if (($p = strpos($this->connection->buf, "\xFF")) !== FALSE) { |
|
144 | - if ($this->connection->pool->maxAllowedPacket <= $p - 1) { { |
|
145 | - // Too big packet |
|
146 | - $this->connection->finish() ; |
|
147 | - return FALSE ; |
|
148 | - } |
|
140 | + $this->connection->onFrame($decodedData, $frametype); |
|
141 | + } |
|
142 | + else { |
|
143 | + if (($p = strpos($this->connection->buf, "\xFF")) !== FALSE) { |
|
144 | + if ($this->connection->pool->maxAllowedPacket <= $p - 1) { { |
|
145 | + // Too big packet |
|
146 | + $this->connection->finish() ; |
|
147 | + return FALSE ; |
|
148 | + } |
|
149 | 149 | |
150 | - $decodedData = binarySubstr($this->connection->buf, 1, $p - 1) ; |
|
151 | - $this->connection->buf = binarySubstr($this->connection->buf, $p + 1) ; |
|
152 | - $this->connection->onFrame($decodedData, $frametype); |
|
150 | + $decodedData = binarySubstr($this->connection->buf, 1, $p - 1) ; |
|
151 | + $this->connection->buf = binarySubstr($this->connection->buf, $p + 1) ; |
|
152 | + $this->connection->onFrame($decodedData, $frametype); |
|
153 | 153 | |
154 | - } |
|
155 | - else { |
|
156 | - if ($this->connection->pool->maxAllowedPacket <= strlen($this->connection->buf)) |
|
157 | - { |
|
158 | - // Too big packet |
|
159 | - $this->connection->finish() ; |
|
160 | - return FALSE ; |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
154 | + } |
|
155 | + else { |
|
156 | + if ($this->connection->pool->maxAllowedPacket <= strlen($this->connection->buf)) |
|
157 | + { |
|
158 | + // Too big packet |
|
159 | + $this->connection->finish() ; |
|
160 | + return FALSE ; |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | 166 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | /** |
40 | 40 | * Called when the worker is ready to go. |
41 | 41 | * @return void |
42 | - */ |
|
42 | + */ |
|
43 | 43 | public function onReady() { |
44 | 44 | $this->enable(); |
45 | 45 | } |
@@ -119,11 +119,11 @@ discard block |
||
119 | 119 | |
120 | 120 | |
121 | 121 | /** |
122 | - * Process default config |
|
123 | - * @todo move it to Daemon_Config class |
|
124 | - * @param array {"setting": "value"} |
|
125 | - * @return void |
|
126 | - */ |
|
122 | + * Process default config |
|
123 | + * @todo move it to Daemon_Config class |
|
124 | + * @param array {"setting": "value"} |
|
125 | + * @return void |
|
126 | + */ |
|
127 | 127 | public function processDefaultConfig($settings = array()) { |
128 | 128 | foreach ($settings as $k => $v) { |
129 | 129 | $k = strtolower(str_replace('-', '', $k)); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | /** |
159 | 159 | * Enable socket events |
160 | 160 | * @return void |
161 | - */ |
|
161 | + */ |
|
162 | 162 | public function enable() { |
163 | 163 | if ($this->enabled) { |
164 | 164 | return; |
@@ -79,7 +79,7 @@ |
||
79 | 79 | Daemon::$reusePort = 1 === preg_match("~BSD~i", php_uname('s')); |
80 | 80 | |
81 | 81 | if (Daemon::$reusePort && !defined("SO_REUSEPORT")) |
82 | - define("SO_REUSEPORT", 0x200); // FIXME: this is a BSD-only hack |
|
82 | + define("SO_REUSEPORT", 0x200); // FIXME: this is a BSD-only hack |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | public static function loadModuleIfAbsent($mod) { |
@@ -337,7 +337,7 @@ |
||
337 | 337 | * Called when new data received |
338 | 338 | * @param string New data |
339 | 339 | * @return void |
340 | - */ |
|
340 | + */ |
|
341 | 341 | public function stdin($buf) { |
342 | 342 | Timer::setTimeout($this->keepaliveTimer); |
343 | 343 | $this->buf .= $buf; |
@@ -41,7 +41,7 @@ |
||
41 | 41 | /** |
42 | 42 | * Enable socket events |
43 | 43 | * @return void |
44 | - */ |
|
44 | + */ |
|
45 | 45 | public function enable() { |
46 | 46 | if ($this->enabled) { |
47 | 47 | return; |
@@ -234,9 +234,9 @@ discard block |
||
234 | 234 | * @param string $subject |
235 | 235 | */ |
236 | 236 | public function message($to, $body, $type = 'chat', $subject = null, $payload = null) { |
237 | - if (is_null($type)) { |
|
237 | + if (is_null($type)) { |
|
238 | 238 | $type = 'chat'; |
239 | - } |
|
239 | + } |
|
240 | 240 | |
241 | 241 | $to = htmlspecialchars($to); |
242 | 242 | $body = htmlspecialchars($body); |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | * Called when new data received |
325 | 325 | * @param string New data |
326 | 326 | * @return void |
327 | - */ |
|
327 | + */ |
|
328 | 328 | public function stdin($buf) { |
329 | 329 | Timer::setTimeout($this->keepaliveTimer); |
330 | 330 | //Daemon::log(Debug::dump(['read', $buf])); |
@@ -29,53 +29,53 @@ discard block |
||
29 | 29 | } |
30 | 30 | |
31 | 31 | public function onHandshake(){ |
32 | - if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_KEY']) || !isset($this->connection->server['HTTP_SEC_WEBSOCKET_VERSION'])) { |
|
32 | + if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_KEY']) || !isset($this->connection->server['HTTP_SEC_WEBSOCKET_VERSION'])) { |
|
33 | 33 | return false; |
34 | 34 | } |
35 | 35 | if ($this->connection->server['HTTP_SEC_WEBSOCKET_VERSION'] != '13' && $this->connection->server['HTTP_SEC_WEBSOCKET_VERSION'] != '8') { |
36 | - return false; |
|
37 | - } |
|
36 | + return false; |
|
37 | + } |
|
38 | 38 | |
39 | 39 | return true; |
40 | 40 | } |
41 | 41 | |
42 | - /** |
|
43 | - * Returns handshaked data for reply |
|
42 | + /** |
|
43 | + * Returns handshaked data for reply |
|
44 | 44 | * @param string Received data (no use in this class) |
45 | - * @return string Handshaked data |
|
46 | - */ |
|
45 | + * @return string Handshaked data |
|
46 | + */ |
|
47 | 47 | |
48 | - public function getHandshakeReply($data) { |
|
49 | - if ($this->onHandshake()) { |
|
50 | - if (isset($this->connection->server['HTTP_ORIGIN'])) { |
|
48 | + public function getHandshakeReply($data) { |
|
49 | + if ($this->onHandshake()) { |
|
50 | + if (isset($this->connection->server['HTTP_ORIGIN'])) { |
|
51 | 51 | $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = $this->connection->server['HTTP_ORIGIN']; |
52 | 52 | } |
53 | - if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'])) |
|
53 | + if (!isset($this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'])) |
|
54 | 54 | { |
55 | - $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = '' ; |
|
56 | - } |
|
57 | - $reply = "HTTP/1.1 101 Switching Protocols\r\n" |
|
58 | - . "Upgrade: WebSocket\r\n" |
|
59 | - . "connection: Upgrade\r\n" |
|
60 | - . "Date: ".date('r')."\r\n" |
|
61 | - . "Sec-WebSocket-Origin: " . $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] . "\r\n" |
|
62 | - . "Sec-WebSocket-Location: ws://" . $this->connection->server['HTTP_HOST'] . $this->connection->server['REQUEST_URI'] . "\r\n" |
|
63 | - . "Sec-WebSocket-Accept: " . base64_encode(sha1(trim($this->connection->server['HTTP_SEC_WEBSOCKET_KEY']) . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true)) . "\r\n"; |
|
55 | + $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = '' ; |
|
56 | + } |
|
57 | + $reply = "HTTP/1.1 101 Switching Protocols\r\n" |
|
58 | + . "Upgrade: WebSocket\r\n" |
|
59 | + . "connection: Upgrade\r\n" |
|
60 | + . "Date: ".date('r')."\r\n" |
|
61 | + . "Sec-WebSocket-Origin: " . $this->connection->server['HTTP_SEC_WEBSOCKET_ORIGIN'] . "\r\n" |
|
62 | + . "Sec-WebSocket-Location: ws://" . $this->connection->server['HTTP_HOST'] . $this->connection->server['REQUEST_URI'] . "\r\n" |
|
63 | + . "Sec-WebSocket-Accept: " . base64_encode(sha1(trim($this->connection->server['HTTP_SEC_WEBSOCKET_KEY']) . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true)) . "\r\n"; |
|
64 | 64 | |
65 | - if (isset($this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'])) { |
|
66 | - $reply .= "Sec-WebSocket-Protocol: " . $this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'] . "\r\n"; |
|
67 | - } |
|
65 | + if (isset($this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'])) { |
|
66 | + $reply .= "Sec-WebSocket-Protocol: " . $this->connection->server['HTTP_SEC_WEBSOCKET_PROTOCOL'] . "\r\n"; |
|
67 | + } |
|
68 | 68 | |
69 | 69 | if ($this->connection->pool->config->expose->value) { |
70 | 70 | $reply .= 'X-Powered-By: phpDaemon/' . Daemon::$version . "\r\n"; |
71 | 71 | } |
72 | 72 | |
73 | - $reply .= "\r\n"; |
|
74 | - return $reply ; |
|
75 | - } |
|
73 | + $reply .= "\r\n"; |
|
74 | + return $reply ; |
|
75 | + } |
|
76 | 76 | |
77 | 77 | return false; |
78 | - } |
|
78 | + } |
|
79 | 79 | |
80 | 80 | /** |
81 | 81 | * Data encoding, according to related IETF draft |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @see http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10#page-16 |
84 | 84 | */ |
85 | 85 | |
86 | - protected function encodeFrame($data, $type = 'STRING') { |
|
86 | + protected function encodeFrame($data, $type = 'STRING') { |
|
87 | 87 | $fin = 1; |
88 | 88 | $rsv1 = 0; |
89 | 89 | $rsv2 = 0; |
@@ -93,40 +93,40 @@ discard block |
||
93 | 93 | $rsv1 = 1; |
94 | 94 | } |
95 | 95 | return $this->encodeFragment($data, $type, $fin, $rsv1, $rsv2, $rsv3); |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | 98 | protected function encodeFragment($data, $type, $fin = 1, $rsv1 = 0, $rsv2 = 0, $rsv3 = 0) { |
99 | - $mask = chr(rand(0, 0xFF)) . |
|
99 | + $mask = chr(rand(0, 0xFF)) . |
|
100 | 100 | chr(rand(0, 0xFF)) . |
101 | 101 | chr(rand(0, 0xFF)) . |
102 | 102 | chr(rand(0, 0xFF)) ; |
103 | 103 | $packet = chr(bindec($fin . $rsv1 . $rsv2 . $rsv3 . str_pad(decbin($this->getFrameType($type)), 4, '0', STR_PAD_LEFT))); |
104 | - $dataLength = strlen($data); |
|
104 | + $dataLength = strlen($data); |
|
105 | 105 | $isMasked = false; |
106 | 106 | $isMaskedInt = $isMasked ? 128 : 0; |
107 | - if ($dataLength <= 125) |
|
108 | - { |
|
109 | - $packet .= chr($dataLength + $isMaskedInt); |
|
110 | - } |
|
111 | - elseif ($dataLength <= 65535) |
|
112 | - { |
|
113 | - $packet .= chr(126 + $isMaskedInt) . // 126 + 128 |
|
114 | - chr($dataLength >> 8) . |
|
115 | - chr($dataLength & 0xFF); |
|
116 | - } |
|
117 | - else { |
|
118 | - $packet .= chr(127 + $isMaskedInt) . // 127 + 128 |
|
119 | - chr($dataLength >> 56) . |
|
120 | - chr($dataLength >> 48) . |
|
121 | - chr($dataLength >> 40) . |
|
122 | - chr($dataLength >> 32) . |
|
123 | - chr($dataLength >> 24) . |
|
124 | - chr($dataLength >> 16) . |
|
125 | - chr($dataLength >> 8) . |
|
126 | - chr($dataLength & 0xFF); |
|
127 | - } |
|
128 | - if ($isMasked) { |
|
129 | - $packet .= $mask . $this->mask($data, $mask); |
|
107 | + if ($dataLength <= 125) |
|
108 | + { |
|
109 | + $packet .= chr($dataLength + $isMaskedInt); |
|
110 | + } |
|
111 | + elseif ($dataLength <= 65535) |
|
112 | + { |
|
113 | + $packet .= chr(126 + $isMaskedInt) . // 126 + 128 |
|
114 | + chr($dataLength >> 8) . |
|
115 | + chr($dataLength & 0xFF); |
|
116 | + } |
|
117 | + else { |
|
118 | + $packet .= chr(127 + $isMaskedInt) . // 127 + 128 |
|
119 | + chr($dataLength >> 56) . |
|
120 | + chr($dataLength >> 48) . |
|
121 | + chr($dataLength >> 40) . |
|
122 | + chr($dataLength >> 32) . |
|
123 | + chr($dataLength >> 24) . |
|
124 | + chr($dataLength >> 16) . |
|
125 | + chr($dataLength >> 8) . |
|
126 | + chr($dataLength & 0xFF); |
|
127 | + } |
|
128 | + if ($isMasked) { |
|
129 | + $packet .= $mask . $this->mask($data, $mask); |
|
130 | 130 | } else { |
131 | 131 | $packet .= $data; |
132 | 132 | } |
@@ -134,8 +134,8 @@ discard block |
||
134 | 134 | } |
135 | 135 | public function mask ($data, $mask) { |
136 | 136 | for ($i = 0, $l = strlen($data), $ml = strlen($mask); $i < $l; $i++) { |
137 | - $data[$i] = $data[$i] ^ $mask[$i % $ml] ; |
|
138 | - } |
|
137 | + $data[$i] = $data[$i] ^ $mask[$i % $ml] ; |
|
138 | + } |
|
139 | 139 | return $data; |
140 | 140 | } |
141 | 141 | /** |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | * @see http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10#page-16 |
194 | 194 | */ |
195 | 195 | |
196 | - public function onRead() { |
|
196 | + public function onRead() { |
|
197 | 197 | $data = ''; |
198 | 198 | while ($this->connection && (($buflen = strlen($this->connection->buf)) >= 1)) { |
199 | 199 | $p = 0; // offset |
@@ -204,15 +204,15 @@ discard block |
||
204 | 204 | $rsv3 = (bool) $firstBits[3]; |
205 | 205 | $opcode = (int) bindec(substr($firstBits, 4, 4)); |
206 | 206 | if ($opcode === 0x8) { // CLOSE |
207 | - $this->connection->finish(); |
|
208 | - return; |
|
207 | + $this->connection->finish(); |
|
208 | + return; |
|
209 | 209 | } |
210 | 210 | $opcodeName = $this->opcodes[$opcode]; |
211 | 211 | $second = ord(binarySubstr($this->connection->buf, $p++, 1)); // second byte integer (masked, payload length) |
212 | 212 | $fin = (bool) ($first >> 7); |
213 | - $isMasked = (bool) ($second >> 7); |
|
214 | - $dataLength = $second & 0x7f; |
|
215 | - if ($dataLength === 0x7e) { // 2 bytes-length |
|
213 | + $isMasked = (bool) ($second >> 7); |
|
214 | + $dataLength = $second & 0x7f; |
|
215 | + if ($dataLength === 0x7e) { // 2 bytes-length |
|
216 | 216 | if ($buflen < $p + 2) { |
217 | 217 | return; // not enough data yet |
218 | 218 | } |
@@ -223,9 +223,9 @@ discard block |
||
223 | 223 | if ($buflen < $p + 4) { |
224 | 224 | return; // not enough data yet |
225 | 225 | } |
226 | - $dataLength = $this->bytes2int(binarySubstr($this->connection->buf, $p, 4)); |
|
227 | - $p += 4; |
|
228 | - } |
|
226 | + $dataLength = $this->bytes2int(binarySubstr($this->connection->buf, $p, 4)); |
|
227 | + $p += 4; |
|
228 | + } |
|
229 | 229 | if ($this->connection->pool->maxAllowedPacket <= $dataLength) { |
230 | 230 | // Too big packet |
231 | 231 | $this->connection->finish(); |
@@ -262,5 +262,5 @@ discard block |
||
262 | 262 | } |
263 | 263 | } |
264 | 264 | } |
265 | - } |
|
265 | + } |
|
266 | 266 | } |
267 | 267 | \ No newline at end of file |
@@ -142,10 +142,10 @@ |
||
142 | 142 | |
143 | 143 | |
144 | 144 | /** |
145 | - * Get next ID |
|
146 | - * |
|
147 | - * @return integer |
|
148 | - */ |
|
145 | + * Get next ID |
|
146 | + * |
|
147 | + * @return integer |
|
148 | + */ |
|
149 | 149 | public function getId() { |
150 | 150 | $this->lastid++; |
151 | 151 | return $this->lastid; |