Server bugfixes

This commit is contained in:
Daniil Gentili 2018-04-16 13:56:00 +02:00
parent 965feb3ba4
commit 5c88cc3e2e
7 changed files with 38 additions and 19 deletions

View File

@ -2,5 +2,6 @@
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' => 8011]);
$handler->start();

View File

@ -170,6 +170,12 @@ class API extends APIFactory
$this->API->wrapper = $this;
}
}
public function get_all_methods()
{
$methods = [];
foreach ($this->API->methods->by_id as $method) { $methods []= $method['method']; }
return array_merge($methods, get_class_methods($this->API));
}
public function serialize($params = '')
{

View File

@ -95,11 +95,6 @@ class Logger
}
if (!is_string($param)) {
$param = json_encode($param, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
/*if ($parame == '') {
$param = var_export($param, true);
} else {
$param = $parame;
}*/
}
if ($file === null) {
$file = basename(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], '.php');

View File

@ -593,9 +593,11 @@ class MTProto
Exception::$rollbar = false;
RPCErrorException::$rollbar = false;
}
$this->logger = new \danog\MadelineProto\Logger($this->settings['logger']['logger'], $this->settings['logger']['logger_param'], isset($this->authorization['user']) ? isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id'] : '', isset($this->settings['logger']['logger_level']) ? $this->settings['logger']['logger_level'] : Logger::VERBOSE);
$this->logger = new \danog\MadelineProto\Logger($this->settings['logger']['logger'], isset($this->settings['logger']['logger_param']) ? $this->settings['logger']['logger_param'] : '', isset($this->authorization['user']) ? isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id'] : '', isset($this->settings['logger']['logger_level']) ? $this->settings['logger']['logger_level'] : Logger::VERBOSE);
if (!\danog\MadelineProto\Logger::$default) {
\danog\MadelineProto\Logger::constructor($this->settings['logger']['logger'], $this->settings['logger']['logger_param'], isset($this->authorization['user']) ? isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id'] : '', isset($this->settings['logger']['logger_level']) ? $this->settings['logger']['logger_level'] : Logger::VERBOSE);
}
}
public function reset_session($de = true, $auth_key = false)
{

View File

@ -24,7 +24,7 @@ class Magic
public static $can_getmypid = true;
public static $processed_fork = false;
public static $ipv6;
private static $pid;
public static $pid;
public static $inited = false;

View File

@ -25,6 +25,7 @@ class Server
public function __construct($settings)
{
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
\danog\MadelineProto\Logger::constructor(3);
$this->settings = $settings;
$this->mypid = getmypid();
}

View File

@ -28,12 +28,14 @@ class Handler extends \danog\MadelineProto\Connection
public function __magic_construct($socket, $extra, $ip, $port, $protocol, $timeout, $ipv6)
{
\danog\MadelineProto\Magic::$pid = getmypid();
$this->sock = $socket;
$this->sock->setBlocking(true);
$this->must_open = false;
$timeout = 2;
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
$this->logger = new \danog\MadelineProto\Logger(3);
$this->construct_TL(['socket' => __DIR__.'/../TL_socket.tl']);
}
@ -42,14 +44,13 @@ class Handler extends \danog\MadelineProto\Connection
echo 'Closing socket in fork '.getmypid().PHP_EOL;
unset($this->sock);
$this->destruct_madeline();
exit();
}
public function destruct_madeline()
{
if ($this->madeline !== null) {
$this->madeline->settings['logger'] = ['logger' => 0];
$this->madeline->serialize();
$this->madeline->API->settings['logger'] = ['logger' => 0, 'logger_param' => ''];
$this->madeline->API->settings['updates']['callback'] = [];
unset($this->madeline);
return true;
@ -96,12 +97,13 @@ class Handler extends \danog\MadelineProto\Connection
$message = $buffer;
$buffer = '';
} else {
$time = time();
$message = $this->read_message();
}
} catch (\danog\MadelineProto\NothingInTheSocketException $e) {
if (time() - $time < 2) $this->sock = null;
continue;
}
try {
$message = $this->deserialize($message, ['type' => '', 'datacenter' => '']);
if ($message['_'] !== 'socketMessageRequest') {
@ -143,21 +145,32 @@ class Handler extends \danog\MadelineProto\Connection
return true;
}
if ($method[0] === '__destruct') {
return $this->__destruct();
$this->__destruct();
exit();
}
if ($this->madeline === null) {
throw new \danog\MadelineProto\Exception('__construct was not called');
}
foreach ($args as &$arg) {
array_walk_recursive($args, function (&$arg, $zis) {
if (is_array($arg) && isset($arg['_'])) {
if ($arg['_'] === 'callback' && isset($arg['callback']) && !method_exists($this, $arg['callback'])) {
$arg = [$this, $arg['callback']];
if ($arg['_'] === 'fileCallback' && isset($arg['callback']) && isset($arg['file']) && !method_exists($zis, $arg['callback']['callback'])) {
if (isset($arg['file']['_']) && $arg['file']['_'] === 'stream') {
$arg['file'] = fopen('madelineSocket://', 'r+b', false, Stream::getContext($zis, $arg['file']['stream_id']));
}
$arg = new \danog\MadelineProto\FileCallback($arg['file'], [$zis, $arg['callback']['callback']]);
return;
}
if ($arg['_'] === 'callback' && isset($arg['callback']) && !method_exists($zis, $arg['callback'])) {
$arg = [$zis, $arg['callback']];
return;
}
if ($arg['_'] === 'stream' && isset($arg['stream_id'])) {
$arg = fopen('madelineSocket://', 'r+b', false, self::getContext($this, $arg['stream_id']));
}
$arg = fopen('madelineSocket://', 'r+b', false, Stream::getContext($zis, $arg['stream_id']));
return;
}
}
}, $this);
if (count($method) === 1) {
return $this->madeline->{$method[0]}(...$args);
}
@ -254,6 +267,7 @@ class Handler extends \danog\MadelineProto\Connection
$this->send_message($message);
} catch (\danog\MadelineProto\Exception $e) {
$this->__destruct();
die;
}
}