mirror of
https://github.com/danog/MadelineProto.git
synced 2024-11-30 09:58:59 +01:00
Improvements to socket interface and TL parser
This commit is contained in:
parent
e9dc0ba6f6
commit
a2255fa7ad
@ -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();
|
||||
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
@ -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:
|
||||
|
@ -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') {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user