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 class ConnectionSerializable extends VolatileSerializer
{ {
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -14,13 +14,19 @@ namespace danog\MadelineProto;
abstract class SerializableVolatile abstract class SerializableVolatile
{ {
public function unserialized($data) { public function unserialized($data)
if (!isset($data['_']) || $data['_'] !== 'pony') return false; {
if (!isset($data['_']) || $data['_'] !== 'pony') {
return false;
}
unset($data['_']); unset($data['_']);
foreach ($data as $key => $data) { foreach ($data as $key => $data) {
$this->{$key} = $data; $this->{$key} = $data;
} }
if (method_exists($this, '__wakeup')) $this->__wakeup(); if (method_exists($this, '__wakeup')) {
$this->__wakeup();
}
return true; 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. You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace danog\MadelineProto; namespace danog\MadelineProto;
if (!extension_loaded("pthreads")) { if (!extension_loaded('pthreads')) {
class Socket { class Socket
public function __construct(int $domain, int $type, int $protocol) { {
public function __construct(int $domain, int $type, int $protocol)
{
$this->sock = socket_create($domain, $type, $protocol); $this->sock = socket_create($domain, $type, $protocol);
} }
public function setBlocking(bool $blocking) {
public function setBlocking(bool $blocking)
{
if ($blocking) { if ($blocking) {
return socket_set_block($this->sock); return socket_set_block($this->sock);
} }
return socket_set_nonblock($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])) { 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); 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); return socket_get_option($this->sock, $level, $name);
} }
public function __call($name, $args) {
public function __call($name, $args)
{
$name = 'socket_'.$name; $name = 'socket_'.$name;
array_unshift($args, $this->sock); array_unshift($args, $this->sock);
return $name(...$args); return $name(...$args);
} }
} }

View File

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