diff --git a/src/Socket.php b/src/Socket.php index 2e4603cd..cd2c4d05 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -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; } diff --git a/src/danog/MadelineProto/Server.php b/src/danog/MadelineProto/Server.php index e51f37f5..f11bc53c 100644 --- a/src/danog/MadelineProto/Server.php +++ b/src/danog/MadelineProto/Server.php @@ -39,12 +39,13 @@ 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); @@ -53,6 +54,7 @@ class Server } } } + private function handle($socket) { $pid = pcntl_fork(); diff --git a/src/danog/MadelineProto/Server/Handler.php b/src/danog/MadelineProto/Server/Handler.php index cd2f27f4..383bac26 100644 --- a/src/danog/MadelineProto/Server/Handler.php +++ b/src/danog/MadelineProto/Server/Handler.php @@ -38,20 +38,26 @@ class Handler extends \danog\MadelineProto\Connection $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) { pcntl_signal_dispatch(); $request_id = 0; + try { $message = $this->read_message(); } catch (\danog\MadelineProto\NothingInTheSocketException $e) { @@ -77,10 +83,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'); } @@ -88,11 +95,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') { @@ -102,12 +110,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'])); } } } @@ -118,23 +126,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])); } } diff --git a/src/danog/MadelineProto/Server/Stream.php b/src/danog/MadelineProto/Server/Stream.php index a9baca3d..34192213 100644 --- a/src/danog/MadelineProto/Server/Stream.php +++ b/src/danog/MadelineProto/Server/Stream.php @@ -13,7 +13,6 @@ If not, see . namespace danog\MadelineProto\Server; - class Stream { const WRAPPER_NAME = 'madelineSocket'; @@ -30,7 +29,8 @@ class Stream stream_wrapper_register(self::WRAPPER_NAME, get_class()); self::$_isRegistered = true; } - return stream_context_create(array(self::WRAPPER_NAME => ['handler' => $handler, $stream_id])); + + return stream_context_create([self::WRAPPER_NAME => ['handler' => $handler, $stream_id]]); } public function stream_open($path, $mode, $options, &$opened_path) @@ -38,11 +38,14 @@ class Stream $opt = stream_context_get_options($this->context); if (!is_array($opt[self::WRAPPER_NAME]) || !isset($opt[self::WRAPPER_NAME]['handler']) || - !($opt[self::WRAPPER_NAME]['handler'] instanceof Handler) + !($opt[self::WRAPPER_NAME]['handler'] instanceof Handler) || !isset($opt[self::WRAPPER_NAME]['stream_id']) || - !is_integer($opt[self::WRAPPER_NAME]['stream_id'])) return false; + !is_int($opt[self::WRAPPER_NAME]['stream_id'])) { + return false; + } $this->_handler = $opt[self::WRAPPER_NAME]['handler']; $this->_stream_id = $opt[self::WRAPPER_NAME]['stream_id']; + return true; } @@ -51,7 +54,7 @@ class Stream $this->handler->send_data($this->_stream_id, $data); } - public function stream_lock($mode) { - + public function stream_lock($mode) + { } -} \ No newline at end of file +}