Start implementing socket API
This commit is contained in:
parent
0994ef99bd
commit
9014506ef5
1
.gitignore
vendored
1
.gitignore
vendored
@ -109,3 +109,4 @@ tests/500mb
|
||||
phar7
|
||||
phar5
|
||||
madeline.phar
|
||||
madeline.phar.version
|
||||
|
4
socket.php
Normal file
4
socket.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
68
src/danog/MadelineProto/Server.php
Normal file
68
src/danog/MadelineProto/Server.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright 2016-2018 Daniil Gentili
|
||||
(https://daniil.it)
|
||||
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.
|
||||
The PWRTelegram API 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/>.
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto;
|
||||
|
||||
/*
|
||||
* Socket server for multi-language API
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
private $settings;
|
||||
private $pids = [];
|
||||
public function __construct($settings) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
public function start() {
|
||||
pcntl_signal(SIGTERM, [$this, 'sig_handler']);
|
||||
pcntl_signal(SIGINT, [$this, 'sig_handler']);
|
||||
pcntl_signal(SIGCHLD, [$this, 'sig_handler']);
|
||||
|
||||
$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);
|
||||
while (true) {
|
||||
$this->handle($this->sock->accept());
|
||||
}
|
||||
}
|
||||
private function handle($socket) {
|
||||
$pid = pcntl_fork();
|
||||
if ($pid == -1) {
|
||||
die('could not fork');
|
||||
} else if ($pid) {
|
||||
return $this->pids[] = $pid;
|
||||
}
|
||||
$handler = new \danog\MadelineProto\Server\Handler($socket);
|
||||
$handler->loop();
|
||||
die;
|
||||
}
|
||||
public function __destruct() {
|
||||
foreach ($this->pid as $pid) {
|
||||
pcntl_wait($pid);
|
||||
}
|
||||
}
|
||||
public function sig_handler($sig)
|
||||
{
|
||||
switch($sig)
|
||||
{
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
exit();
|
||||
|
||||
case SIGCHLD:
|
||||
pcntl_waitpid(-1, $status);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
34
src/danog/MadelineProto/Server/Handler.php
Normal file
34
src/danog/MadelineProto/Server/Handler.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
Copyright 2016-2018 Daniil Gentili
|
||||
(https://daniil.it)
|
||||
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.
|
||||
The PWRTelegram API 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/>.
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto\Server;
|
||||
|
||||
/*
|
||||
* Socket handler for server
|
||||
*/
|
||||
class Handler
|
||||
{
|
||||
private $socket;
|
||||
public function __construct($socket) {
|
||||
$this->socket = $socket;
|
||||
}
|
||||
public function loop() {
|
||||
|
||||
}
|
||||
public function read_payload() {
|
||||
|
||||
}
|
||||
public function write_payload($payload) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user