1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-05 19:41:55 +02:00

Move from React 0.4.0 to 0.3.4 for PHP 5.3 compatibility reasons

This commit is contained in:
Afterster 2014-04-13 08:58:34 +02:00
parent d3f01a3bc7
commit 7f82bea13e
50 changed files with 608 additions and 1937 deletions

View file

@ -5,7 +5,7 @@ namespace React\SocketClient;
use React\EventLoop\LoopInterface;
use React\Dns\Resolver\Resolver;
use React\Stream\Stream;
use React\Promise;
use React\Promise\When;
use React\Promise\Deferred;
class Connector implements ConnectorInterface
@ -21,10 +21,12 @@ class Connector implements ConnectorInterface
public function create($host, $port)
{
$that = $this;
return $this
->resolveHostname($host)
->then(function ($address) use ($port) {
return $this->createSocketForAddress($address, $port);
->then(function ($address) use ($port, $that) {
return $that->createSocketForAddress($address, $port);
});
}
@ -35,7 +37,7 @@ class Connector implements ConnectorInterface
$socket = stream_socket_client($url, $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
if (!$socket) {
return Promise\reject(new \RuntimeException(
return When::reject(new \RuntimeException(
sprintf("connection to %s:%d failed: %s", $address, $port, $errstr),
$errno
));
@ -71,10 +73,10 @@ class Connector implements ConnectorInterface
// The following hack looks like the only way to
// detect connection refused errors with PHP's stream sockets.
if (false === stream_socket_get_name($socket, true)) {
return Promise\reject(new ConnectionException('Connection refused'));
return When::reject(new ConnectionException('Connection refused'));
}
return Promise\resolve($socket);
return When::resolve($socket);
}
public function handleConnectedSocket($socket)
@ -94,7 +96,7 @@ class Connector implements ConnectorInterface
protected function resolveHostname($host)
{
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
return Promise\resolve($host);
return When::resolve($host);
}
return $this->resolver->resolve($host);

View file

@ -5,8 +5,9 @@ Async Connector to open TCP/IP and SSL/TLS based connections.
## Introduction
Think of this library as an async version of
[`fsockopen()`](http://www.php.net/function.fsockopen) or
[`stream_socket_client()`](http://php.net/function.stream-socket-client).
[`fsockopen()`](http://php.net/manual/en/function.fsockopen.php) or
[`stream_socket_client()`](http://php.net/manual/en/function.stream-socket-
client.php).
Before you can actually transmit and receive data to/from a remote server, you
have to establish a connection to the remote end. Establishing this connection

View file

@ -4,6 +4,7 @@ namespace React\SocketClient;
use React\EventLoop\LoopInterface;
use React\Stream\Stream;
use React\Promise\When;
class SecureConnector implements ConnectorInterface
{
@ -18,9 +19,10 @@ class SecureConnector implements ConnectorInterface
public function create($host, $port)
{
return $this->connector->create($host, $port)->then(function (Stream $stream) {
$streamEncryption = $this->streamEncryption;
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($streamEncryption) {
// (unencrypted) connection succeeded => try to enable encryption
return $this->streamEncryption->enable($stream)->then(null, function ($error) use ($stream) {
return $streamEncryption->enable($stream)->then(null, function ($error) use ($stream) {
// establishing encryption failed => close invalid connection and return error
$stream->close();
throw $error;

View file

@ -2,6 +2,7 @@
namespace React\SocketClient;
use React\Promise\ResolverInterface;
use React\Promise\Deferred;
use React\Stream\Stream;
use React\EventLoop\LoopInterface;
@ -46,15 +47,16 @@ class StreamEncryption
// get actual stream socket from stream instance
$socket = $stream->stream;
$toggleCrypto = function () use ($socket, $deferred, $toggle) {
$this->toggleCrypto($socket, $deferred, $toggle);
$that = $this;
$toggleCrypto = function () use ($that, $socket, $deferred, $toggle) {
$that->toggleCrypto($socket, $deferred, $toggle);
};
$this->loop->addWriteStream($socket, $toggleCrypto);
$this->loop->addReadStream($socket, $toggleCrypto);
$toggleCrypto();
return $deferred->promise()->then(function () use ($stream) {
return $deferred->then(function () use ($stream) {
$stream->resume();
return $stream;
}, function($error) use ($stream) {
@ -63,7 +65,7 @@ class StreamEncryption
});
}
public function toggleCrypto($socket, Deferred $deferred, $toggle)
public function toggleCrypto($socket, ResolverInterface $resolver, $toggle)
{
set_error_handler(array($this, 'handleError'));
$result = stream_socket_enable_crypto($socket, $toggle, $this->method);
@ -73,12 +75,12 @@ class StreamEncryption
$this->loop->removeWriteStream($socket);
$this->loop->removeReadStream($socket);
$deferred->resolve();
$resolver->resolve();
} else if (false === $result) {
$this->loop->removeWriteStream($socket);
$this->loop->removeReadStream($socket);
$deferred->reject(new UnexpectedValueException(
$resolver->reject(new UnexpectedValueException(
sprintf("Unable to complete SSL/TLS handshake: %s", $this->errstr),
$this->errno
));

View file

@ -4,17 +4,18 @@
"keywords": ["socket"],
"license": "MIT",
"require": {
"php": ">=5.4.0",
"react/dns": "0.4.*",
"react/event-loop": "0.4.*",
"react/promise": "~2.0"
"php": ">=5.3.3",
"react/dns": "0.3.*",
"react/event-loop": "0.3.*",
"react/promise": "~1.0"
},
"autoload": {
"psr-4": { "React\\SocketClient\\": "" }
"psr-0": { "React\\SocketClient": "" }
},
"target-dir": "React/SocketClient",
"extra": {
"branch-alias": {
"dev-master": "0.4-dev"
"dev-master": "0.3-dev"
}
}
}