Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2017-04-21 11:27:50 +00:00 committed by StyleCI Bot
parent 1eb2fc0b4f
commit 18096de1e7
8 changed files with 65 additions and 43 deletions

View File

@ -14,4 +14,4 @@ namespace danog\MadelineProto;
class ConnectionSerializable extends VolatileSerializer
{
}
}

View File

@ -14,4 +14,4 @@ namespace danog\MadelineProto;
class DataCenterSerializable extends VolatileSerializer
{
}
}

View File

@ -14,4 +14,4 @@ namespace danog\MadelineProto;
class MTProtoSerializable extends VolatileSerializer
{
}
}

View File

@ -15,7 +15,7 @@ namespace danog\MadelineProto;
/**
* Manages connection to telegram servers.
*/
class Connection extends SerializableVolatile
class NewConnection extends SerializableVolatile
{
use \danog\MadelineProto\Tools;
public $sock = null;
@ -39,7 +39,9 @@ class Connection extends SerializableVolatile
public function __construct($ip, $port = null, $protocol = null, $timeout = null, $ipv6 = null)
{
if ($this->unserialized($ip)) return true;
if ($this->unserialized($ip)) {
return true;
}
// Can use:
/*
- tcp_full
@ -54,32 +56,32 @@ class Connection extends SerializableVolatile
$this->ipv6 = $ipv6;
$this->ip = $ip;
$this->port = $port;
$this->sock = new \Volatile;
$this->sock = new \Volatile();
switch ($this->protocol) {
case 'tcp_abridged':
$this->sock["pony"] = new \Volatile;
$this->sock["pony"]['socket'] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock["pony"]->connect($ip, $port)) {
$this->sock['pony'] = new \Volatile();
$this->sock['pony']['socket'] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock['pony']->connect($ip, $port)) {
throw new Exception("Connection: couldn't connect to socket.");
}
$this->write(chr(239));
break;
case 'tcp_intermediate':
$this->sock["pony"] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock["pony"]->connect($ip, $port)) {
$this->sock['pony'] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock['pony']->connect($ip, $port)) {
throw new Exception("Connection: couldn't connect to socket.");
}
$this->write(str_repeat(chr(238), 4));
break;
case 'tcp_full':
$this->sock["pony"] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
$this->sock['pony'] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname('tcp'));
//$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
//$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock["pony"]->connect($ip, $port)) {
if (!$this->sock['pony']->connect($ip, $port)) {
throw new Exception("Connection: couldn't connect to socket.");
}
$this->out_seq_no = -1;
@ -88,10 +90,10 @@ class Connection extends SerializableVolatile
case 'http':
case 'https':
$this->parsed = parse_url($ip);
$this->sock["pony"] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname($this->protocol === 'https' ? 'tls' : 'tcp'));
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock["pony"]->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock["pony"]->connect($this->parsed['host'], $port)) {
$this->sock['pony'] = new Socket($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname($this->protocol === 'https' ? 'tls' : 'tcp'));
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout);
$this->sock['pony']->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout);
if (!$this->sock['pony']->connect($this->parsed['host'], $port)) {
throw new Exception("Connection: couldn't connect to socket.");
}
break;
@ -112,7 +114,7 @@ class Connection extends SerializableVolatile
case 'http':
case 'https':
try {
unset($this->sock["pony"]);
unset($this->sock['pony']);
} catch (\danog\MadelineProto\Exception $e) {
}
break;
@ -129,6 +131,7 @@ class Connection extends SerializableVolatile
$this->__destruct();
$this->__construct($this->ip, $this->port, $this->protocol, $this->timeout, $this->ipv6);
}
public function __sleep()
{
$t = get_object_vars($this);
@ -155,7 +158,7 @@ class Connection extends SerializableVolatile
case 'tcp_full':
case 'http':
case 'https':
if (($wrote = $this->sock["pony"]->write($what)) !== strlen($what)) {
if (($wrote = $this->sock['pony']->write($what)) !== strlen($what)) {
throw new \danog\MadelineProto\Exception("WARNING: Wrong length was written (should've written ".strlen($what).', wrote '.$wrote.')!');
}
@ -172,15 +175,14 @@ class Connection extends SerializableVolatile
public function read($length)
{
switch ($this->protocol) {
case 'tcp_abridged':
case 'tcp_intermediate':
case 'tcp_full':
case 'http':
case 'https':
$packet = $this->sock["pony"]->read($length);
$packet = $this->sock['pony']->read($length);
if ($packet === false || strlen($packet) === 0) {
throw new \danog\MadelineProto\NothingInTheSocketException('Nothing in the socket!');
}

View File

@ -14,4 +14,4 @@ namespace danog\MadelineProto;
class RSASerializable extends VolatileSerializer
{
}
}

View File

@ -14,13 +14,19 @@ namespace danog\MadelineProto;
abstract class SerializableVolatile
{
public function unserialized($data) {
if (!isset($data['_']) || $data['_'] !== 'pony') return false;
public function unserialized($data)
{
if (!isset($data['_']) || $data['_'] !== 'pony') {
return false;
}
unset($data['_']);
foreach ($data as $key => $data) {
$this->{$key} = $data;
}
if (method_exists($this, '__wakeup')) $this->__wakeup();
if (method_exists($this, '__wakeup')) {
$this->__wakeup();
}
return true;
}
}
}

View File

@ -9,33 +9,45 @@ 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;
if (!extension_loaded("pthreads")) {
class Socket {
public function __construct(int $domain, int $type, int $protocol) {
if (!extension_loaded('pthreads')) {
class Socket
{
public function __construct(int $domain, int $type, int $protocol)
{
$this->sock = socket_create($domain, $type, $protocol);
}
public function setBlocking(bool $blocking) {
public function setBlocking(bool $blocking)
{
if ($blocking) {
return socket_set_block($this->sock);
}
return socket_set_nonblock($this->sock);
}
public function setOption(int $level, int $name, $value) {
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)];
$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) {
public function getOption(int $level, int $name)
{
return socket_get_option($this->sock, $level, $name);
}
public function __call($name, $args) {
public function __call($name, $args)
{
$name = 'socket_'.$name;
array_unshift($args, $this->sock);
return $name(...$args);
}
}

View File

@ -14,10 +14,12 @@ namespace danog\MadelineProto;
class VolatileSerializer
{
public function wakeup() {
public function wakeup()
{
$elements = $this;
$elements['_'] = 'pony';
$class = "\\".str_replace('Serializable', '', get_class($this));
$class = '\\'.str_replace('Serializable', '', get_class($this));
return new $class($elements);
}
}