From e5128bcd86f88370fe9aaebfef160a715864eb12 Mon Sep 17 00:00:00 2001 From: laogui Date: Mon, 1 Apr 2019 20:04:00 +0800 Subject: [PATCH 01/13] Add get main socket resource function UDP application can init UdpConnection class and send message where have a client remote address --- Worker.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Worker.php b/Worker.php index eb539361a..8dbbb57f6 100644 --- a/Worker.php +++ b/Worker.php @@ -667,6 +667,14 @@ public static function getEventLoop() { return static::$globalEvent; } + + /** + * Get main socket resource + * @return resource + */ + public function getMainSocket(){ + return $this->_mainSocket; + } /** * Init idMap. From 4e5c24073b431fd950287efbfb5cc9b4c0fc7367 Mon Sep 17 00:00:00 2001 From: walkor Date: Fri, 5 Apr 2019 18:35:31 +0800 Subject: [PATCH 02/13] remove default timezone #411 --- Lib/Constants.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/Constants.php b/Lib/Constants.php index 5048a3970..1b3561b11 100644 --- a/Lib/Constants.php +++ b/Lib/Constants.php @@ -12,10 +12,6 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -// Date.timezone -if (!ini_get('date.timezone')) { - date_default_timezone_set('Asia/Shanghai'); -} // Display errors. ini_set('display_errors', 'on'); // Reporting all. From 13649907f05014fcfffcfccaef01e63ad3339351 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 30 Apr 2019 09:51:25 +0800 Subject: [PATCH 03/13] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0d4ddb3a..33869e560 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,13 @@ [![License](https://poser.pugx.org/workerman/workerman/license)](https://packagist.org/packages/workerman/workerman) ## What is it -Workerman is an asynchronous event driven PHP framework with high performance for easily building fast, scalable network applications. Supports HTTP, Websocket, SSL and other custom protocols. Supports libevent, [HHVM](https://github.com/facebook/hhvm) , [ReactPHP](https://github.com/reactphp/react). +Workerman is an asynchronous event driven PHP framework with high performance for easily building fast, scalable network applications. Supports HTTP, Websocket, SSL and other custom protocols. Supports libevent/event extension, [HHVM](https://github.com/facebook/hhvm) , [ReactPHP](https://github.com/reactphp/react). ## Requires PHP 5.3 or Higher A POSIX compatible operating system (Linux, OSX, BSD) -POSIX and PCNTL extensions for PHP +POSIX and PCNTL extensions required +Event extension recommended for better performance ## Installation From 0506d795e3d99cd5270186ee6311b21307517c9f Mon Sep 17 00:00:00 2001 From: walkor Date: Fri, 21 Jun 2019 14:44:34 +0800 Subject: [PATCH 04/13] check closed state on ssl handshake --- Connection/AsyncTcpConnection.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Connection/AsyncTcpConnection.php b/Connection/AsyncTcpConnection.php index 8800d19bd..e785bf775 100644 --- a/Connection/AsyncTcpConnection.php +++ b/Connection/AsyncTcpConnection.php @@ -318,6 +318,9 @@ public function checkConnection() // SSL handshake. if ($this->transport === 'ssl') { $this->_sslHandshakeCompleted = $this->doSslHandshake($this->_socket); + if ($this->_sslHandshakeCompleted === false) { + return; + } } else { // There are some data waiting to send. if ($this->_sendBuffer) { From e23ffda31bc1241a50cc3c0635e866c27ecee2e5 Mon Sep 17 00:00:00 2001 From: walkor Date: Fri, 21 Jun 2019 14:46:07 +0800 Subject: [PATCH 05/13] check closed state after ssl handshake --- Connection/TcpConnection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Connection/TcpConnection.php b/Connection/TcpConnection.php index c6f563724..b05a43c04 100644 --- a/Connection/TcpConnection.php +++ b/Connection/TcpConnection.php @@ -777,7 +777,7 @@ public function doSslHandshake($socket){ return false; } elseif (0 === $ret) { // There isn't enough data and should try again. - return false; + return 0; } if (isset($this->onSslHandshake)) { try { From 6127e345e967780ed78d144b21ed1546cc8d9d9a Mon Sep 17 00:00:00 2001 From: walkor Date: Fri, 21 Jun 2019 14:47:01 +0800 Subject: [PATCH 06/13] Update Worker.php --- Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Worker.php b/Worker.php index 8dbbb57f6..97efa5151 100644 --- a/Worker.php +++ b/Worker.php @@ -33,7 +33,7 @@ class Worker * * @var string */ - const VERSION = '3.5.19'; + const VERSION = '3.5.20'; /** * Status starting. From b5145b12f9ceddce36a69fa50d0dca0abbe78218 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 2 Jul 2019 18:19:42 +0800 Subject: [PATCH 07/13] Update Worker.php --- Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Worker.php b/Worker.php index 97efa5151..bd4b7c084 100644 --- a/Worker.php +++ b/Worker.php @@ -1334,7 +1334,7 @@ protected static function forkWorkersForWindows() if(count(static::$_workers) > 1) { static::safeEcho("@@@ Error: multi workers init in one php file are not support @@@\r\n"); - static::safeEcho("@@@ Please visit http://wiki.workerman.net/Multi_woker_for_win @@@\r\n"); + static::safeEcho("@@@ See http://doc.workerman.net/faq/multi-woker-for-windows.html @@@\r\n"); } elseif(count(static::$_workers) <= 0) { From 4c173c30505d49656b71d315963f960280ba3d41 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 2 Jul 2019 18:22:09 +0800 Subject: [PATCH 08/13] websocket tips --- Protocols/Websocket.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Protocols/Websocket.php b/Protocols/Websocket.php index 146063f96..7d1bd9e63 100644 --- a/Protocols/Websocket.php +++ b/Protocols/Websocket.php @@ -356,7 +356,7 @@ protected static function dealHandshake($buffer, $connection) if (preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) { $Sec_WebSocket_Key = $match[1]; } else { - $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n400 Bad Request
Sec-WebSocket-Key not found.
This is a WebSocket service and can not be accessed via HTTP.
See http://wiki.workerman.net/Error1 for detail.", + $connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n", true); $connection->close(); return 0; @@ -441,7 +441,7 @@ protected static function dealHandshake($buffer, $connection) return 0; } // Bad websocket handshake request. - $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n400 Bad Request
Invalid handshake data for websocket.
See http://wiki.workerman.net/Error1 for detail.", + $connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n", true); $connection->close(); return 0; From 4d590130310a8d7632f807120c3ca1c0f55ed0d7 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 2 Jul 2019 18:23:18 +0800 Subject: [PATCH 09/13] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 95c30972c..fdd4808a2 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "email": "walkor@workerman.net", "issues": "https://github.com/walkor/workerman/issues", "forum": "http://wenda.workerman.net/", - "wiki": "http://doc3.workerman.net/index.html", + "wiki": "http://doc.workerman.net/", "source": "https://github.com/walkor/workerman" }, "require": { From fe7f9c30780871738823af582ce9b7f17e9bf18b Mon Sep 17 00:00:00 2001 From: Ares Date: Thu, 11 Jul 2019 00:59:03 +0800 Subject: [PATCH 10/13] Remove forced ws client header 'Origin'. This is not needed in common cases and may cause 4xx response. The default value '*' is also invalid. --- Protocols/Ws.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Protocols/Ws.php b/Protocols/Ws.php index 917ca02e3..8b3aae74a 100644 --- a/Protocols/Ws.php +++ b/Protocols/Ws.php @@ -383,7 +383,7 @@ public static function sendHandshake($connection) (!preg_match("/\nHost:/i", $user_header_str) ? "Host: $host\r\n" : ''). "Connection: Upgrade\r\n". "Upgrade: websocket\r\n". - "Origin: ". (isset($connection->websocketOrigin) ? $connection->websocketOrigin : '*') ."\r\n". + (isset($connection->websocketOrigin) ? "Origin: ".$connection->websocketOrigin."\r\n":''). (isset($connection->WSClientProtocol)?"Sec-WebSocket-Protocol: ".$connection->WSClientProtocol."\r\n":''). "Sec-WebSocket-Version: 13\r\n". "Sec-WebSocket-Key: " . $connection->websocketSecKey . $user_header_str . "\r\n\r\n"; From bc5d3a4db2002f116fc403c71ccee0058a5d4f13 Mon Sep 17 00:00:00 2001 From: Ares Date: Thu, 11 Jul 2019 01:21:08 +0800 Subject: [PATCH 11/13] Fix sendHandshake() child override --- Protocols/Ws.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Protocols/Ws.php b/Protocols/Ws.php index 8b3aae74a..caeaff785 100644 --- a/Protocols/Ws.php +++ b/Protocols/Ws.php @@ -234,7 +234,7 @@ public static function encode($payload, $connection) } $payload = (string)$payload; if (empty($connection->handshakeStep)) { - self::sendHandshake($connection); + static::sendHandshake($connection); } $mask = 1; $mask_key = "\x00\x00\x00\x00"; @@ -330,7 +330,7 @@ public static function decode($bytes, $connection) */ public static function onConnect($connection) { - self::sendHandshake($connection); + static::sendHandshake($connection); } /** From 01ea5037f268fcc465f61a6039327e4212fe3463 Mon Sep 17 00:00:00 2001 From: Ares Date: Wed, 24 Jul 2019 16:48:06 +0800 Subject: [PATCH 12/13] Discard sending data when status is connecting on close. --- Connection/TcpConnection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Connection/TcpConnection.php b/Connection/TcpConnection.php index b05a43c04..c0297ac67 100644 --- a/Connection/TcpConnection.php +++ b/Connection/TcpConnection.php @@ -836,6 +836,10 @@ public function consumeRecvBuffer($length) */ public function close($data = null, $raw = false) { + if($this->_status === self::STATUS_CONNECTING){ + $this->destroy(); + return; + } if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) { return; } else { From c3f33c4354fa3a87784d00d3b7c8d761f5c56f50 Mon Sep 17 00:00:00 2001 From: Ares Date: Fri, 26 Jul 2019 12:35:55 +0800 Subject: [PATCH 13/13] Bump websocket decode performance to %40000 or more in heavy traffic. --- Protocols/Websocket.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Protocols/Websocket.php b/Protocols/Websocket.php index 7d1bd9e63..6237f2153 100644 --- a/Protocols/Websocket.php +++ b/Protocols/Websocket.php @@ -304,7 +304,6 @@ public static function encode($buffer, ConnectionInterface $connection) */ public static function decode($buffer, ConnectionInterface $connection) { - $masks = $data = $decoded = ''; $len = ord($buffer[1]) & 127; if ($len === 126) { $masks = substr($buffer, 4, 4); @@ -318,9 +317,9 @@ public static function decode($buffer, ConnectionInterface $connection) $data = substr($buffer, 6); } } - for ($index = 0; $index < strlen($data); $index++) { - $decoded .= $data[$index] ^ $masks[$index % 4]; - } + $dataLength = strlen($data); + $masks = str_repeat($masks, floor($dataLength / 4)) . substr($masks, 0, $dataLength % 4); + $decoded = $data ^ $masks; if ($connection->websocketCurrentFrameLength) { $connection->websocketDataBuffer .= $decoded; return $connection->websocketDataBuffer;