This commit is contained in:
Daniil Gentili 2020-03-14 17:29:49 +01:00
parent ce0e12c747
commit 93bd9073e1
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 17 additions and 115 deletions

View File

@ -25,8 +25,8 @@
"amphp/http-client": "^4", "amphp/http-client": "^4",
"amphp/socket": "^1", "amphp/socket": "^1",
"amphp/dns": "^1", "amphp/dns": "^1",
"amphp/file": "^1",
"amphp/byte-stream": "^1", "amphp/byte-stream": "^1",
"amphp/file": "^1",
"danog/dns-over-https": "^0.2", "danog/dns-over-https": "^0.2",
"amphp/http-client-cookies": "^1", "amphp/http-client-cookies": "^1",
"danog/tg-file-decoder": "^0.1", "danog/tg-file-decoder": "^0.1",
@ -49,7 +49,8 @@
"ext-ctype": "*", "ext-ctype": "*",
"danog/7to70": "^1", "danog/7to70": "^1",
"danog/7to5": "^1", "danog/7to5": "^1",
"vimeo/psalm": "dev-master" "vimeo/psalm": "dev-master",
"phpstan/phpstan": "^0.12.14"
}, },
"suggest": { "suggest": {
"ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)"

2
docs

@ -1 +1 @@
Subproject commit 1e1967391fe643b6ecc0e6f22a800386cbd22e5d Subproject commit eda40443bf6215bc8eff7842a5eaaae70b4c4ee3

View File

@ -20,7 +20,7 @@
require '../vendor/autoload.php'; require '../vendor/autoload.php';
$MadelineProto = new \danog\MadelineProto\API('session.madeline'); $MadelineProto = new \danog\MadelineProto\API('index.madeline');
$me = $MadelineProto->start(); $me = $MadelineProto->start();
$me = $MadelineProto->getSelf(); $me = $MadelineProto->getSelf();

View File

@ -39,7 +39,7 @@ interface SignalLoopInterface extends LoopInterface
/** /**
* Send a signal to the the loop. * Send a signal to the the loop.
* *
* @param \Throwable|any $data Signal to send * @param \Throwable|mixed $data Signal to send
* *
* @return void * @return void
*/ */

View File

@ -310,14 +310,17 @@ class Magic
// Even an empty handler is enough to catch ctrl+c // Even an empty handler is enough to catch ctrl+c
if (\defined('SIGINT')) { if (\defined('SIGINT')) {
//if (function_exists('pcntl_async_signals')) pcntl_async_signals(true); //if (function_exists('pcntl_async_signals')) pcntl_async_signals(true);
Loop::unreference(Loop::onSignal(SIGINT, static function () { try {
Logger::log('Got sigint', Logger::FATAL_ERROR); Loop::unreference(Loop::onSignal(SIGINT, static function () {
Magic::shutdown(1); Logger::log('Got sigint', Logger::FATAL_ERROR);
})); Magic::shutdown(1);
Loop::unreference(Loop::onSignal(SIGTERM, static function () { }));
Logger::log('Got sigterm', Logger::FATAL_ERROR); Loop::unreference(Loop::onSignal(SIGTERM, static function () {
Magic::shutdown(1); Logger::log('Got sigterm', Logger::FATAL_ERROR);
})); Magic::shutdown(1);
}));
} catch (\Throwable $e) {
}
} }
/*if (!self::$altervista && !self::$zerowebhost) { /*if (!self::$altervista && !self::$zerowebhost) {
$DohConfig = new DoHConfig( $DohConfig = new DoHConfig(

View File

@ -1,102 +0,0 @@
<?php
/**
* Server module.
*
* 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/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
/*
* Socket server for multi-language API
*/
class Server
{
private $settings;
private $pids = [];
private $mypid;
public function __construct($settings)
{
\set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
\danog\MadelineProto\Logger::constructor(3);
if (!\extension_loaded('sockets')) {
throw new Exception(['extension', 'sockets']);
}
if (!\extension_loaded('pcntl')) {
throw new Exception(['extension', 'pcntl']);
}
$this->settings = $settings;
$this->mypid = \getmypid();
}
public function start()
{
\pcntl_signal(SIGTERM, [$this, 'sigHandler']);
\pcntl_signal(SIGINT, [$this, 'sigHandler']);
\pcntl_signal(SIGCHLD, [$this, 'sigHandler']);
$this->sock = new \Socket($this->settings['type'], SOCK_STREAM, $this->settings['protocol']);
$this->sock->bind($this->settings['address'], $this->settings['port']);
$this->sock->listen();
$this->sock->setBlocking(true);
$timeout = 2;
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
\danog\MadelineProto\Logger::log('Server started! Listening on ' . $this->settings['address'] . ':' . $this->settings['port']);
while (true) {
\pcntl_signal_dispatch();
try {
if ($sock = $this->sock->accept()) {
$this->handle($sock);
}
} catch (\danog\MadelineProto\Exception $e) {
}
}
}
private function handle($socket)
{
$pid = \pcntl_fork();
if ($pid == -1) {
die('could not fork');
} elseif ($pid) {
return $this->pids[] = $pid;
}
$handler = new $this->settings['handler']($socket, $this->settings['extra'], null, null, null, null, null);
$handler->loop();
die;
}
public function __destruct()
{
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;
}
}
public function sigHandler($sig)
{
switch ($sig) {
case SIGTERM:
case SIGINT:
exit;
case SIGCHLD:
\pcntl_waitpid(-1, $status);
break;
}
}
}