Skip to content

[RFC] Improve compatibility with legacy versions #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"keywords": ["socket"],
"license": "MIT",
"require": {
"php": ">=5.4.0",
"react/dns": "0.4.*",
"react/event-loop": "0.4.*",
"react/stream": "0.4.*",
"react/promise": "~2.0"
"php": ">=5.3.0",
"react/dns": "0.4.*|0.3.*",
"react/event-loop": "0.4.*|0.3.*",
"react/stream": "0.4.*|0.3.*",
"react/promise": "~2.0|~1.1"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 3 additions & 2 deletions src/SecureConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function create($host, $port)
);
}

return $this->connector->create($host, $port)->then(function (Stream $stream) use ($context) {
$encryption = $this->streamEncryption;
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($context, $encryption) {
// (unencrypted) TCP/IP connection succeeded

// set required SSL/TLS context options
Expand All @@ -47,7 +48,7 @@ public function create($host, $port)
}

// try to enable encryption
return $this->streamEncryption->enable($stream)->then(null, function ($error) use ($stream) {
return $encryption->enable($stream)->then(null, function ($error) use ($stream) {
// establishing encryption failed => close invalid connection and return error
$stream->close();
throw $error;
Expand Down
24 changes: 12 additions & 12 deletions src/SecureStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Evenement\EventEmitterTrait;
use React\EventLoop\LoopInterface;
use React\Stream\DuplexStreamInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Stream;
use React\Stream\Util;

class SecureStream extends Stream implements DuplexStreamInterface
class SecureStream extends Stream
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about code that typehints against DuplexStreamInterface? Considering to be a BC tbh.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually amends #26 which in turn amends #14.

The documentation always stated this returns a Stream instance, never an instance implementing DuplexStreamInterface.

FWIW: This repo currently targets react/stream:0.4.*, while this interface has only been added with v0.4.2, so even if we decide not to support legacy versions, this is still a bug that needs fixing. If you happen to have v0.4.2+, then Stream implements the DuplexStreamInterface.

As such, I do not consider this a BC break.

Even despite this, this PR should probably be part of the v0.5.0 milestone, which already includes a (minor) BC break anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't aware DuplexStreamInterface was added later in 0.4.x, so yeah it isn't a BC break imo :).

{
// use EventEmitterTrait;

Expand All @@ -22,18 +21,19 @@ public function __construct(Stream $stream, LoopInterface $loop) {
$this->stream = $stream->stream;
$this->decorating = $stream;
$this->loop = $loop;
$that = $this;

$stream->on('error', function($error) {
$this->emit('error', [$error, $this]);
$stream->on('error', function($error) use ($that) {
$that->emit('error', array($error, $that));
});
$stream->on('end', function() {
$this->emit('end', [$this]);
$stream->on('end', function() use ($that) {
$that->emit('end', array($that));
});
$stream->on('close', function() {
$this->emit('close', [$this]);
$stream->on('close', function() use ($that) {
$that->emit('close', array($that));
});
$stream->on('drain', function() {
$this->emit('drain', [$this]);
$stream->on('drain', function() use ($that) {
$that->emit('drain', array($that));
});

$stream->pause();
Expand All @@ -45,7 +45,7 @@ public function handleData($stream)
{
$data = stream_get_contents($stream);

$this->emit('data', [$data, $this]);
$this->emit('data', array($data, $this));

if (!is_resource($stream) || feof($stream)) {
$this->end();
Expand All @@ -60,7 +60,7 @@ public function pause()
public function resume()
{
if ($this->isReadable()) {
$this->loop->addReadStream($this->decorating->stream, [$this, 'handleData']);
$this->loop->addReadStream($this->decorating->stream, array($this, 'handleData'));
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/StreamEncryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ public function toggle(Stream $stream, $toggle)
// 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 ($socket, $deferred, $toggle, $that) {
$that->toggleCrypto($socket, $deferred, $toggle);
};

$this->loop->addReadStream($socket, $toggleCrypto);
$toggleCrypto();

return $deferred->promise()->then(function () use ($stream, $toggle) {
if ($toggle && $this->wrapSecure) {
return new SecureStream($stream, $this->loop);
$wrap = $this->wrapSecure && $toggle;
$loop = $this->loop;

return $deferred->promise()->then(function () use ($stream, $wrap, $loop) {
if ($wrap) {
return new SecureStream($stream, $loop);
}

$stream->resume();
Expand Down