MadelineProto/src/Socket.php

373 lines
12 KiB
PHP
Raw Normal View History

2017-04-21 13:27:04 +02:00
<?php
/*
Copyright 2016-2018 Daniil Gentili
2017-04-21 13:27:04 +02:00
(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.
MadelineProto 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/>.
*/
2018-02-20 12:24:58 +01:00
class FSocket
{
private $sock;
private $protocol;
private $timeout = ['sec' => 0, 'usec' => 0];
private $blocking = false;
private $domain;
private $type;
public function __construct(int $domain, int $type, int $protocol)
2017-10-05 17:54:27 +02:00
{
$this->domain = $domain;
$this->type = $type;
$this->protocol = $protocol === PHP_INT_MAX ? 'tls' : ($protocol === PHP_INT_MAX - 1 ? 'tcp' : getprotobynumber($protocol));
}
2017-10-05 17:54:27 +02:00
public function __destruct()
{
if ($this->sock !== null) {
fclose($this->sock);
2017-10-05 17:54:27 +02:00
}
}
2017-12-16 20:01:35 +01:00
public function setOption(int $level, int $name, $value)
{
if (in_array($name, [\SO_RCVTIMEO, \SO_SNDTIMEO])) {
$this->timeout = ['sec' => (int) $value, 'usec' => (int) (($value - (int) $value) * 1000000)];
return true;
}
2017-10-05 17:54:27 +02:00
throw new \danog\MadelineProto\Exception('Not supported');
}
2017-10-05 17:54:27 +02:00
public function getOption(int $level, int $name)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function setBlocking(bool $blocking)
{
return stream_set_blocking($this->sock, $blocking);
}
public function bind(string $address, int $port = 0)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function listen(int $backlog = 0)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function accept()
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function connect(string $address, int $port = -1)
{
if ($this->domain === AF_INET6 && strpos($address, ':') !== false) {
$address = '['.$address.']';
2017-10-05 17:54:27 +02:00
}
2018-02-19 12:48:43 +01:00
$errno = 0;
$errstr = '';
2018-02-19 13:23:53 +01:00
$this->sock = fsockopen($this->protocol.'://'.$address, $port, $errno, $errstr, $this->timeout['sec'] + ($this->timeout['usec'] / 1000000));
2018-02-19 12:48:43 +01:00
stream_set_timeout($this->sock, $this->timeout['sec'], $this->timeout['usec']);
2017-12-28 11:57:55 +01:00
return true;
}
public static function select(array &$read, array &$write, array &$except, int $tv_sec, int $tv_usec = 0)
{
$actual_read = [];
foreach ($read as $key => $resource) {
$actual_read[$key] = $resource->getResource();
}
$actual_write = [];
foreach ($write as $key => $resource) {
$actual_write[$key] = $resource->getResource();
}
$actual_except = [];
foreach ($except as $key => $resource) {
$actual_except[$key] = $resource->getResource();
}
$res = stream_select($actual_read, $actual_write, $actual_except, $tv_sec, $tv_usec);
foreach ($read as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_read[$key])) {
unset($read[$key]);
}
}
foreach ($write as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_write[$key])) {
unset($write[$key]);
}
}
foreach ($except as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_except[$key])) {
unset($except[$key]);
}
}
2018-03-29 13:25:42 +02:00
return $res;
}
public function read(int $length, int $flags = 0)
{
$packet = '';
while (strlen($packet) < $length) {
$read = stream_get_contents($this->sock, $length - strlen($packet));
2018-04-18 16:12:32 +02:00
if ($read === false || strlen($read) === 0) {
throw new \danog\MadelineProto\NothingInTheSocketException();
}
$packet .= $read;
}
2018-04-18 16:12:32 +02:00
return $packet;
}
public function write(string $buffer, int $length = -1)
{
if ($length === -1) {
$length = strlen($buffer);
} else {
$buffer = substr($buffer, 0, $length);
}
$wrote = 0;
if (($wrote += fwrite($this->sock, $buffer, $length)) !== $length) {
2018-04-18 16:12:32 +02:00
while (($wrote += fwrite($this->sock, substr($buffer, $wrote), $length - $wrote)) !== $length) {
}
}
return $wrote;
}
public function send(string $data, int $length, int $flags)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
public function close()
{
2018-02-20 16:06:39 +01:00
fclose($this->sock);
$this->sock = null;
}
public function getPeerName(bool $port = true)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
2017-10-05 17:54:27 +02:00
public function getSockName(bool $port = true)
{
throw new \danog\MadelineProto\Exception('Not supported');
}
2018-03-16 15:10:29 +01:00
public function setExtra(array $extra = [])
{
}
2018-03-16 15:10:29 +01:00
public function getProxyHeaders()
{
return '';
}
2018-03-29 13:25:42 +02:00
public function getResource()
{
return $this->sock;
}
}
2018-02-20 12:24:58 +01:00
if (!extension_loaded('pthreads')) {
if (extension_loaded('sockets')) {
2018-03-04 17:42:48 +01:00
class SocketBase
2018-02-20 12:24:58 +01:00
{
private $sock;
2018-03-04 17:42:48 +01:00
public function __construct($sock)
2018-02-20 12:24:58 +01:00
{
2018-03-04 17:42:48 +01:00
$this->sock = $sock;
2018-02-20 12:24:58 +01:00
}
public function __destruct()
{
socket_close($this->sock);
unset($this->sock);
2018-02-20 12:24:58 +01:00
}
public function setOption(int $level, int $name, $value)
{
if (in_array($name, [\SO_RCVTIMEO, \SO_SNDTIMEO])) {
$value = ['sec' => (int) $value, 'usec' => (int) (($value - (int) $value) * 1000000)];
}
return socket_set_option($this->sock, $level, $name, $value);
}
public function getOption(int $level, int $name)
{
return socket_get_option($this->sock, $level, $name);
}
public function setBlocking(bool $blocking)
{
if ($blocking) {
return socket_set_block($this->sock);
}
return socket_set_nonblock($this->sock);
}
public function bind(string $address, int $port = 0)
{
return socket_bind($this->sock, $address, $port);
}
public function listen(int $backlog = 0)
{
return socket_listen($this->sock, $backlog);
}
public function accept()
{
2018-03-04 17:42:48 +01:00
if ($socket = socket_accept($this->sock)) {
2018-03-04 17:43:06 +01:00
return new self($socket);
2018-03-04 17:42:48 +01:00
} else {
return $socket;
}
2018-02-20 12:24:58 +01:00
}
public function connect(string $address, int $port = 0)
{
return socket_connect($this->sock, $address, $port);
}
public static function select(array &$read, array &$write, array &$except, int $tv_sec, int $tv_usec = 0)
2018-02-20 12:24:58 +01:00
{
$actual_read = [];
foreach ($read as $key => $resource) {
$actual_read[$key] = $resource->getResource();
}
$actual_write = [];
foreach ($write as $key => $resource) {
$actual_write[$key] = $resource->getResource();
}
$actual_except = [];
foreach ($except as $key => $resource) {
$actual_except[$key] = $resource->getResource();
}
$res = socket_select($actual_read, $actual_write, $actual_except, $tv_sec, $tv_usec);
foreach ($read as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_read[$key])) {
unset($read[$key]);
}
}
foreach ($write as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_write[$key])) {
unset($write[$key]);
}
}
foreach ($except as $key => $resource) {
2018-03-29 13:25:42 +02:00
if (!isset($actual_except[$key])) {
unset($except[$key]);
}
}
2018-03-29 13:25:42 +02:00
return $res;
2018-02-20 12:24:58 +01:00
}
public function read(int $length, int $flags = 0)
{
$packet = '';
while (strlen($packet) < $length) {
$read = socket_read($this->sock, $length - strlen($packet), $flags);
2018-04-18 16:12:32 +02:00
if ($read === false || strlen($read) === false) {
throw new \danog\MadelineProto\NothingInTheSocketException();
}
$packet .= $read;
}
2018-04-18 16:12:32 +02:00
return $packet;
2018-02-20 12:24:58 +01:00
}
public function write(string $buffer, int $length = -1)
{
if ($length === -1) {
$length = strlen($buffer);
} else {
$buffer = substr($buffer, 0, $length);
}
$wrote = 0;
if (($wrote += socket_write($this->sock, $buffer, $length)) !== $length) {
2018-04-18 16:12:32 +02:00
while (($wrote += socket_write($this->sock, substr($buffer, $wrote), $length - $wrote)) !== $length) {
}
}
return $wrote;
2018-02-20 12:24:58 +01:00
}
public function send(string $data, int $length, int $flags)
{
return socket_send($data, $length, $flags);
}
public function close()
{
2018-02-20 16:06:39 +01:00
socket_close($this->sock);
$this->sock = null;
2018-02-20 12:24:58 +01:00
}
public function getPeerName(bool $port = true)
{
$address = '';
$port = 0;
$port ? socket_getpeername($this->sock, $address, $ip) : socket_getpeername($this->sock, $address);
return $port ? ['host' => $address, 'port' => $port] : ['host' => $address];
}
public function getSockName(bool $port = true)
{
$address = '';
$port = 0;
$port ? socket_getsockname($this->sock, $address, $ip) : socket_getsockname($this->sock, $address);
return $port ? ['host' => $address, 'port' => $port] : ['host' => $address];
}
2018-03-16 15:10:29 +01:00
public function getProxyHeaders()
{
return '';
}
2018-03-29 13:25:42 +02:00
public function getResource()
{
return $this->sock;
}
2018-02-20 12:24:58 +01:00
}
2018-03-04 17:42:48 +01:00
class Socket extends SocketBase
{
public function __construct(int $domain, int $type, int $protocol)
{
parent::__construct(socket_create($domain, $type, $protocol));
}
}
2018-02-20 12:24:58 +01:00
} else {
define('AF_INET', 0);
define('AF_INET6', 1);
define('SOCK_STREAM', 2);
define('SOL_SOCKET', 3);
define('SO_RCVTIMEO', 4);
define('SO_SNDTIMEO', 5);
class Socket extends FSocket
{
}
}
2017-04-21 13:27:04 +02:00
}