Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2018-03-04 16:43:06 +00:00 committed by StyleCI Bot
parent e9dc0ba6f6
commit 9f428b3e4c
3 changed files with 41 additions and 16 deletions

View File

@ -171,7 +171,7 @@ if (!extension_loaded('pthreads')) {
public function accept()
{
if ($socket = socket_accept($this->sock)) {
return new SocketBase($socket);
return new self($socket);
} else {
return $socket;
}

View File

@ -38,20 +38,22 @@ class Server
$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);
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();
@ -72,6 +74,7 @@ class Server
foreach ($this->pids as $pid) {
pcntl_wait($pid);
}
return;
}
\danog\MadelineProto\Logger::log('Shutting fork '.getmypid().' down');

View File

@ -29,24 +29,32 @@ class Handler extends \danog\MadelineProto\Connection
$this->protocol = $protocol;
$this->construct_TL(['socket' => __DIR__.'/../TL_socket.tl']);
}
public function __destruct() {
public function __destruct()
{
unset($this->sock);
$this->destruct_madeline();
exit();
}
public function destruct_madeline() {
public function destruct_madeline()
{
if ($this->madeline !== null) {
$this->madeline->settings['logger'] = ['logger' => 0];
$this->madeline->serialize();
unset($this->madeline);
return true;
}
return false;
}
public function loop()
{
while (true) {
$request_id = 0;
try {
$message = $this->read_message();
} catch (\danog\MadelineProto\NothingInTheSocketException $e) {
@ -55,6 +63,7 @@ class Handler extends \danog\MadelineProto\Connection
if ($message === null) {
continue;
}
try {
$message = $this->deserialize($message, ['type' => '', 'datacenter' => '']);
if ($message['_'] !== 'socketMessageRequest') {
@ -75,10 +84,11 @@ class Handler extends \danog\MadelineProto\Connection
$this->send_exception($request_id, $e);
continue;
}
}
}
public function on_request($method, $args) {
public function on_request($method, $args)
{
if (count($method) === 0 || count($method) > 2) {
throw new \danog\MadelineProto\Exception('Invalid method called');
}
@ -86,11 +96,12 @@ class Handler extends \danog\MadelineProto\Connection
if (count($args) === 1 && is_array($args[0])) {
$args[0]['logger'] = ['logger' => 4, 'logger_param' => [$this, 'logger']];
$args[0]['updates']['callback'] = [$this, 'update_handler'];
} else if (count($args) === 2 && is_array($args[1])) {
} elseif (count($args) === 2 && is_array($args[1])) {
$args[1]['logger'] = ['logger' => 4, 'logger_param' => [$this, 'logger']];
$args[1]['updates']['callback'] = [$this, 'update_handler'];
}
$this->madeline = new \danog\MadelineProto\API(...$args);
return true;
}
if ($method[0] === '__destruct') {
@ -100,12 +111,12 @@ class Handler extends \danog\MadelineProto\Connection
throw new \danog\MadelineProto\Exception('__construct was not called');
}
foreach ($args as &$arg) {
if (is_array($arg) && isset($arg['_'])){
if (is_array($arg) && isset($arg['_'])) {
if ($arg['_'] === 'callback' && isset($arg['callback']) && !method_exists($this, $arg['callback'])) {
$arg = [$this, $arg['callback']];
}
if ($arg['_'] === 'stream' && isset($arg['stream_id'])) {
$arg = fopen('madelineSocket://', 'r+b', false, Handler::getContext($this, $arg['stream_id']));
$arg = fopen('madelineSocket://', 'r+b', false, self::getContext($this, $arg['stream_id']));
}
}
}
@ -116,23 +127,34 @@ class Handler extends \danog\MadelineProto\Connection
return $this->madeline->{$method[0]}->{$method[1]}(...$args);
}
}
public function send_exception($request_id, $e) {
public function send_exception($request_id, $e)
{
echo $e;
//$this->send_message($this->serialize_object(['type' => 'socketMessageException'], ['request_id' => $request_id, 'exception' => $e]));
}
public function send_response($request_id, $response) {
public function send_response($request_id, $response)
{
$this->send_message($this->serialize_object(['type' => 'socketMessageResponse'], ['request_id' => $request_id, 'data' => $response]));
}
public function send_data($stream_id, $data) {
public function send_data($stream_id, $data)
{
$this->send_message($this->serialize_object(['type' => 'socketMessageRawData'], ['stream_id' => $stream_id, 'data' => $data]));
}
public function logger($message, $level) {
public function logger($message, $level)
{
}
public function update_handler($update) {
public function update_handler($update)
{
$this->send_message($this->serialize_object(['type' => 'socketMessageUpdate'], ['data' => $update]));
}
public function __call($method, $args) {
public function __call($method, $args)
{
$this->send_message($this->serialize_object(['type' => 'socketMessageRequest'], ['request_id' => 0, 'method' => $method, 'args' => $args]));
}
}