This commit is contained in:
Daniil Gentili 2018-03-05 15:58:36 +01:00
commit bf5443daa4
4 changed files with 45 additions and 21 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

@ -39,12 +39,13 @@ class Server
$this->sock->bind($this->settings['address'], $this->settings['port']); $this->sock->bind($this->settings['address'], $this->settings['port']);
$this->sock->listen(); $this->sock->listen();
$this->sock->setBlocking(true); $this->sock->setBlocking(true);
$timeout = 2; $timeout = 2;
$this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout); $this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$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);
@ -53,6 +54,7 @@ class Server
} }
} }
} }
private function handle($socket) private function handle($socket)
{ {
$pid = pcntl_fork(); $pid = pcntl_fork();

View File

@ -38,20 +38,26 @@ class Handler extends \danog\MadelineProto\Connection
$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) {
pcntl_signal_dispatch(); pcntl_signal_dispatch();
$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) {
@ -77,10 +83,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');
} }
@ -88,11 +95,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') {
@ -102,12 +110,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']));
} }
} }
} }
@ -118,23 +126,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]));
} }
} }

View File

@ -13,7 +13,6 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto\Server; namespace danog\MadelineProto\Server;
class Stream class Stream
{ {
const WRAPPER_NAME = 'madelineSocket'; const WRAPPER_NAME = 'madelineSocket';
@ -30,7 +29,8 @@ class Stream
stream_wrapper_register(self::WRAPPER_NAME, get_class()); stream_wrapper_register(self::WRAPPER_NAME, get_class());
self::$_isRegistered = true; 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) public function stream_open($path, $mode, $options, &$opened_path)
@ -38,11 +38,14 @@ class Stream
$opt = stream_context_get_options($this->context); $opt = stream_context_get_options($this->context);
if (!is_array($opt[self::WRAPPER_NAME]) || if (!is_array($opt[self::WRAPPER_NAME]) ||
!isset($opt[self::WRAPPER_NAME]['handler']) || !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']) || !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->_handler = $opt[self::WRAPPER_NAME]['handler'];
$this->_stream_id = $opt[self::WRAPPER_NAME]['stream_id']; $this->_stream_id = $opt[self::WRAPPER_NAME]['stream_id'];
return true; return true;
} }
@ -51,7 +54,7 @@ class Stream
$this->handler->send_data($this->_stream_id, $data); $this->handler->send_data($this->_stream_id, $data);
} }
public function stream_lock($mode) { public function stream_lock($mode)
{
} }
} }