Bugfixes to server, logging and serialization

This commit is contained in:
Daniil Gentili 2018-04-17 10:39:55 +02:00
parent 1a48e8224e
commit e600aa0c43
8 changed files with 77 additions and 27 deletions

View File

@ -2,5 +2,5 @@
require 'vendor/autoload.php';
$handler = new \danog\MadelineProto\Server(['type' => AF_INET, 'protocol' => 0, 'address' => 'localhost', 'port' => 8011]);
$handler = new \danog\MadelineProto\Server(['type' => AF_INET, 'protocol' => 0, 'address' => 'localhost', 'port' => 8002]);
$handler->start();

View File

@ -31,6 +31,7 @@ class FileCallback implements FileCallbackInterface
public function __invoke($percent)
{
$this->callback($percent);
$callback = $this->callback;
$callback($percent);
}
}

View File

@ -48,6 +48,7 @@ class MTProto
use \danog\MadelineProto\Wrappers\Callback;
use \danog\MadelineProto\Wrappers\Login;
use \danog\MadelineProto\Wrappers\Loop;
use \danog\MadelineProto\Wrappers\Noop;
use \danog\MadelineProto\Wrappers\Start;
use \danog\MadelineProto\Wrappers\Templates;

View File

@ -20,7 +20,10 @@ trait Files
{
public function upload($file, $file_name = '', $cb = null, $encrypted = false, $datacenter = null)
{
if (is_object($file) && class_implements($file)['\danog\MadelineProto\FileCallbackInterface']) {
if (is_object($file)) {
if (!isset(class_implements($file)['danog\MadelineProto\FileCallbackInterface'])) {
throw new \danog\MadelineProto\Exception('Provided object does not implement FileCallbackInterface');
}
$cb = $file;
$file = $file->getFile();
}

View File

@ -71,9 +71,19 @@ class Serialization
\danog\MadelineProto\Logger::log('Lock acquired, serializing');
try {
$update_closure = $instance->API->settings['updates']['callback'];
if ($instance->API->settings['updates']['callback'] instanceof \Closure) {
$instance->API->settings['updates']['callback'] = [$instance->API, 'noop'];
}
$logger_closure = $instance->API->settings['logger']['logger_param'];
if ($instance->API->settings['logger']['logger_param'] instanceof \Closure) {
$instance->API->settings['logger']['logger_param'] = [$instance->API, 'noop'];
}
$wrote = file_put_contents($realpaths['tempfile'], serialize($instance));
rename($realpaths['tempfile'], $realpaths['file']);
} finally {
$instance->API->settings['updates']['callback'] = $update_closure;
$instance->API->settings['logger']['logger_param'] = $logger_closure;
flock($realpaths['lockfile'], LOCK_UN);
fclose($realpaths['lockfile']);
}

View File

@ -48,7 +48,7 @@ class Handler extends \danog\MadelineProto\Connection
public function destruct_madeline()
{
if ($this->madeline !== null) {
if (isset($this->madeline) && $this->madeline !== null) {
$this->madeline->API->settings['logger'] = ['logger' => 0, 'logger_param' => ''];
$this->madeline->API->settings['updates']['callback'] = [];
unset($this->madeline);
@ -154,28 +154,8 @@ class Handler extends \danog\MadelineProto\Connection
if ($this->madeline === null) {
throw new \danog\MadelineProto\Exception('__construct was not called');
}
array_walk_recursive($args, function (&$arg, $zis) {
if (is_array($arg) && isset($arg['_'])) {
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, Stream::getContext($zis, $arg['stream_id']));
return;
}
}
}, $this);
array_walk($args, [$this, 'walker']);
if (count($method) === 1) {
return $this->madeline->{$method[0]}(...$args);
@ -185,8 +165,34 @@ class Handler extends \danog\MadelineProto\Connection
}
}
private function walker(&$arg) {
if (is_array($arg)) {
if (isset($arg['_'])) {
if ($arg['_'] === 'fileCallback' && isset($arg['callback']) && isset($arg['file']) && !method_exists($this, $arg['callback']['callback'])) {
if (isset($arg['file']['_']) && $arg['file']['_'] === 'stream') {
$arg['file'] = fopen('madelineSocket://', 'r+b', false, Stream::getContext($this, $arg['file']['stream_id']));
}
$arg = new \danog\MadelineProto\FileCallback($arg['file'], [$this, $arg['callback']['callback']]);
return;
} else if ($arg['_'] === 'callback' && isset($arg['callback']) && !method_exists($this, $arg['callback'])) {
$arg = [$this, $arg['callback']];
return;
} else if ($arg['_'] === 'stream' && isset($arg['stream_id'])) {
$arg = fopen('madelineSocket://', 'r+b', false, Stream::getContext($this, $arg['stream_id']));
return;
} else {
array_walk($arg, [$this, 'walker']);
}
} else {
array_walk($arg, [$this, 'walker']);
}
}
}
public function send_exception($request_id, $e)
{
echo $e.PHP_EOL;
if ($e instanceof \danog\MadelineProto\RPCErrorException) {
$exception = ['_' => 'socketRPCErrorException'];
if ($e->getMessage() === $e->rpc) {
@ -209,7 +215,7 @@ class Handler extends \danog\MadelineProto\Connection
$tl_frame = ['_' => 'socketTLFrame'];
if (isset($frame['function']) && in_array($frame['function'], ['serialize_params', 'serialize_object'])) {
if ($frame['args'][2] !== '') {
$tl_frame['tl_param'] = $frame['args'][2];
$tl_frame['tl_param'] = (string) $frame['args'][2];
$tl = true;
}
} else {
@ -284,6 +290,6 @@ class Handler extends \danog\MadelineProto\Connection
public function __call($method, $args)
{
$this->send_message_safe($this->serialize_object(['type' => ''], ['_' => 'socketMessageRequest', 'request_id' => 0, 'method' => $method, 'args' => $args], 'method'));
$this->send_message_safe($this->serialize_object(['type' => ''], ['_' => 'socketMessageRequest', 'request_id' => 0, 'method' => [$method], 'args' => $args], 'method'));
}
}

View File

@ -503,6 +503,11 @@ trait TL
if (in_array($current_argument['type'], ['DataJSON', '%DataJSON'])) {
$arguments[$current_argument['name']] = ['_' => 'dataJSON', 'data' => json_encode($arguments[$current_argument['name']])];
}
if (isset($current_argument['subtype']) && in_array($current_argument['subtype'], ['DataJSON', '%DataJSON'])) {
array_walk($arguments[$current_argument['name']], function (&$arg) { $arg = ['_' => 'dataJSON', 'data' => json_encode($arg)]; });
}
if (!is_array($arguments[$current_argument['name']]) && $current_argument['type'] === 'InputFile' && $this->settings['upload']['allow_automatic_upload']) {
$arguments[$current_argument['name']] = $this->upload($arguments[$current_argument['name']]);
}

View File

@ -0,0 +1,24 @@
<?php
/*
Copyright 2016-2018 Daniil Gentili
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
namespace danog\MadelineProto\Wrappers;
trait Noop
{
public function setNoop()
{
$this->settings['updates']['callback'] = [$this, 'noop'];
$this->settings['updates']['handle_updates'] = true;
}
public function noop() {}
}