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() public function accept()
{ {
if ($socket = socket_accept($this->sock)) { if ($socket = socket_accept($this->sock)) {
return new SocketBase($socket); return new self($socket);
} else { } else {
return $socket; return $socket;
} }

View File

@ -44,6 +44,7 @@ class Server
$this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
while (true) { while (true) {
pcntl_signal_dispatch(); pcntl_signal_dispatch();
try { try {
if ($sock = $this->sock->accept()) { if ($sock = $this->sock->accept()) {
$this->handle($sock); $this->handle($sock);
@ -52,6 +53,7 @@ class Server
} }
} }
} }
private function handle($socket) private function handle($socket)
{ {
$pid = pcntl_fork(); $pid = pcntl_fork();
@ -72,6 +74,7 @@ class Server
foreach ($this->pids as $pid) { foreach ($this->pids as $pid) {
pcntl_wait($pid); pcntl_wait($pid);
} }
return; return;
} }
\danog\MadelineProto\Logger::log('Shutting fork '.getmypid().' down'); \danog\MadelineProto\Logger::log('Shutting fork '.getmypid().' down');

View File

@ -29,24 +29,32 @@ class Handler extends \danog\MadelineProto\Connection
$this->protocol = $protocol; $this->protocol = $protocol;
$this->construct_TL(['socket' => __DIR__.'/../TL_socket.tl']); $this->construct_TL(['socket' => __DIR__.'/../TL_socket.tl']);
} }
public function __destruct() {
public function __destruct()
{
unset($this->sock); unset($this->sock);
$this->destruct_madeline(); $this->destruct_madeline();
exit(); exit();
} }
public function destruct_madeline() {
public function destruct_madeline()
{
if ($this->madeline !== null) { if ($this->madeline !== null) {
$this->madeline->settings['logger'] = ['logger' => 0]; $this->madeline->settings['logger'] = ['logger' => 0];
$this->madeline->serialize(); $this->madeline->serialize();
unset($this->madeline); unset($this->madeline);
return true; return true;
} }
return false; return false;
} }
public function loop() public function loop()
{ {
while (true) { while (true) {
$request_id = 0; $request_id = 0;
try { try {
$message = $this->read_message(); $message = $this->read_message();
} catch (\danog\MadelineProto\NothingInTheSocketException $e) { } catch (\danog\MadelineProto\NothingInTheSocketException $e) {
@ -55,6 +63,7 @@ class Handler extends \danog\MadelineProto\Connection
if ($message === null) { if ($message === null) {
continue; continue;
} }
try { try {
$message = $this->deserialize($message, ['type' => '', 'datacenter' => '']); $message = $this->deserialize($message, ['type' => '', 'datacenter' => '']);
if ($message['_'] !== 'socketMessageRequest') { if ($message['_'] !== 'socketMessageRequest') {
@ -75,10 +84,11 @@ class Handler extends \danog\MadelineProto\Connection
$this->send_exception($request_id, $e); $this->send_exception($request_id, $e);
continue; continue;
} }
}
}
} public function on_request($method, $args)
} {
public function on_request($method, $args) {
if (count($method) === 0 || count($method) > 2) { if (count($method) === 0 || count($method) > 2) {
throw new \danog\MadelineProto\Exception('Invalid method called'); 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])) { if (count($args) === 1 && is_array($args[0])) {
$args[0]['logger'] = ['logger' => 4, 'logger_param' => [$this, 'logger']]; $args[0]['logger'] = ['logger' => 4, 'logger_param' => [$this, 'logger']];
$args[0]['updates']['callback'] = [$this, 'update_handler']; $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]['logger'] = ['logger' => 4, 'logger_param' => [$this, 'logger']];
$args[1]['updates']['callback'] = [$this, 'update_handler']; $args[1]['updates']['callback'] = [$this, 'update_handler'];
} }
$this->madeline = new \danog\MadelineProto\API(...$args); $this->madeline = new \danog\MadelineProto\API(...$args);
return true; return true;
} }
if ($method[0] === '__destruct') { if ($method[0] === '__destruct') {
@ -100,12 +111,12 @@ class Handler extends \danog\MadelineProto\Connection
throw new \danog\MadelineProto\Exception('__construct was not called'); throw new \danog\MadelineProto\Exception('__construct was not called');
} }
foreach ($args as &$arg) { 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'])) { if ($arg['_'] === 'callback' && isset($arg['callback']) && !method_exists($this, $arg['callback'])) {
$arg = [$this, $arg['callback']]; $arg = [$this, $arg['callback']];
} }
if ($arg['_'] === 'stream' && isset($arg['stream_id'])) { 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); return $this->madeline->{$method[0]}->{$method[1]}(...$args);
} }
} }
public function send_exception($request_id, $e) {
public function send_exception($request_id, $e)
{
echo $e; echo $e;
//$this->send_message($this->serialize_object(['type' => 'socketMessageException'], ['request_id' => $request_id, 'exception' => $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])); $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])); $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])); $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])); $this->send_message($this->serialize_object(['type' => 'socketMessageRequest'], ['request_id' => 0, 'method' => $method, 'args' => $args]));
} }
} }