diff --git a/server.php b/server.php index d79ba168..531c9fd0 100644 --- a/server.php +++ b/server.php @@ -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(); + diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index cb88d41e..627b86b3 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -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 = '') { diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 6ff6adc7..7e31c5eb 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -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'); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 32e2557f..bc88e1da 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -593,8 +593,10 @@ 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); - \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); + $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) diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index 3a611ab7..54ab194c 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -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; diff --git a/src/danog/MadelineProto/Server.php b/src/danog/MadelineProto/Server.php index bee2841d..a964a9ad 100644 --- a/src/danog/MadelineProto/Server.php +++ b/src/danog/MadelineProto/Server.php @@ -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(); } diff --git a/src/danog/MadelineProto/Server/Handler.php b/src/danog/MadelineProto/Server/Handler.php index 842b1298..e6319f75 100644 --- a/src/danog/MadelineProto/Server/Handler.php +++ b/src/danog/MadelineProto/Server/Handler.php @@ -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; } }