From a2255fa7ad79f8eb1b536210b81b81ac004d7044 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 5 Mar 2018 15:57:35 +0100 Subject: [PATCH] Improvements to socket interface and TL parser --- socket.php | 2 +- src/danog/MadelineProto/Connection.php | 7 +++---- src/danog/MadelineProto/Server.php | 14 ++++++++------ src/danog/MadelineProto/Server/Handler.php | 10 ++++++---- src/danog/MadelineProto/TL/TL.php | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/socket.php b/socket.php index d79ba168..d0ffe04b 100644 --- a/socket.php +++ b/socket.php @@ -2,5 +2,5 @@ require 'vendor/autoload.php'; -$handler = new \danog\MadelineProto\Server(['type' => AF_INET, 'protocol' => 0, 'address' => 'localhost', 'port' => 8005]); +$handler = new \danog\MadelineProto\Server(['type' => AF_INET, 'protocol' => 0, 'address' => 'localhost', 'port' => 8009]); $handler->start(); diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index faf9b6a3..6e2f5bdd 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -166,7 +166,6 @@ class Connection throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); default: throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_invalid']); - break; } } @@ -188,7 +187,6 @@ class Connection throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); default: throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_invalid']); - break; } } @@ -275,7 +273,6 @@ class Connection throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); default: throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_invalid']); - break; } } @@ -322,6 +319,8 @@ class Connection return $response['body']; case 'udp': throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); + default: + throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_invalid']); } } @@ -358,7 +357,7 @@ class Connection case 'udp': throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); default: - break; + throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_invalid']); } } diff --git a/src/danog/MadelineProto/Server.php b/src/danog/MadelineProto/Server.php index 19fd8d0c..e51f37f5 100644 --- a/src/danog/MadelineProto/Server.php +++ b/src/danog/MadelineProto/Server.php @@ -20,12 +20,13 @@ class Server { private $settings; private $pids = []; + private $mypid; public function __construct($settings) { set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); $this->settings = $settings; - $this->main = getmypid(); + $this->mypid = getmypid(); } public function start() @@ -47,7 +48,7 @@ class Server try { if ($sock = $this->sock->accept()) { $this->handle($sock); - } + } } catch (\danog\MadelineProto\Exception $e) { } } @@ -67,14 +68,16 @@ class Server public function __destruct() { - if (!\danog\MadelineProto\Logger::$is_fork) { - \danog\MadelineProto\Logger::log('Shutting main process down'); + if ($this->mypid === getmypid()) { + \danog\MadelineProto\Logger::log('Shutting main process '.$this->mypid.' down'); + unset($this->sock); foreach ($this->pids as $pid) { + \danog\MadelineProto\Logger::log("Waiting for $pid"); pcntl_wait($pid); } + \danog\MadelineProto\Logger::log("Done, closing main process"); return; } - \danog\MadelineProto\Logger::log('Shutting fork '.getmypid().' down'); } public function sig_handler($sig) @@ -82,7 +85,6 @@ class Server switch ($sig) { case SIGTERM: case SIGINT: - Logger::log('Got SIGTERM/SIGINT in '.getmypid()); exit(); case SIGCHLD: diff --git a/src/danog/MadelineProto/Server/Handler.php b/src/danog/MadelineProto/Server/Handler.php index 9500641a..cd2f27f4 100644 --- a/src/danog/MadelineProto/Server/Handler.php +++ b/src/danog/MadelineProto/Server/Handler.php @@ -26,10 +26,14 @@ class Handler extends \danog\MadelineProto\Connection { $this->sock = $socket; $this->sock->setBlocking(true); - $this->protocol = $protocol; + $timeout = 2; + $this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout); + $this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout); + $this->protocol = $extra; $this->construct_TL(['socket' => __DIR__.'/../TL_socket.tl']); } public function __destruct() { + \danog\MadelineProto\Logger::log('Closing socket in fork '.getmypid()); unset($this->sock); $this->destruct_madeline(); exit(); @@ -46,15 +50,13 @@ class Handler extends \danog\MadelineProto\Connection public function loop() { while (true) { + pcntl_signal_dispatch(); $request_id = 0; try { $message = $this->read_message(); } catch (\danog\MadelineProto\NothingInTheSocketException $e) { continue; } - if ($message === null) { - continue; - } try { $message = $this->deserialize($message, ['type' => '', 'datacenter' => '']); if ($message['_'] !== 'socketMessageRequest') { diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 8031de4e..026d3f6b 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -101,7 +101,7 @@ trait TL } $clean = preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $line); $id = hash('crc32b', $clean); - if (preg_match('/^[^#]+#([a-f0-9]*)/i', $line, $matches)) { + if (preg_match('/^[^\s]+#([a-f0-9]*)/i', $line, $matches)) { $nid = str_pad($matches[1], 8, '0', \STR_PAD_LEFT); if ($id !== $nid && $scheme_type !== 'botAPI') { \danog\MadelineProto\Logger::log(sprintf(\danog\MadelineProto\Lang::$current_lang['crc32_mismatch'], $id, $nid, $line), \danog\MadelineProto\Logger::ERROR);