Skip to content

Commit

Permalink
Update for amphp/socket v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 1, 2023
1 parent 4d32aa2 commit 2043047
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/proxy/socks4.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//use Amp\Http\Client\HttpException;
//use Amp\Http\Client\Request;
//use Amp\Socket\ConnectContext;
//use Amp\Socket\EncryptableSocket;
//use Amp\Socket\Socket;
//use Amp\Socket\ResourceSocket;
//use Amp\Socket\SocketAddress;
//use Amp\Socket\SocketConnector;
Expand All @@ -26,7 +26,7 @@
// string $uri,
// ?ConnectContext $context = null,
// ?Cancellation $token = null
// ): EncryptableSocket {
// ): Socket {
// $context = $context ?? (new ConnectContext);
//
// $options = $context->toStreamContextArray();
Expand Down
4 changes: 2 additions & 2 deletions examples/proxy/socks5.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//use Amp\Http\Client\HttpException;
//use Amp\Http\Client\Request;
//use Amp\Socket\ConnectContext;
//use Amp\Socket\EncryptableSocket;
//use Amp\Socket\Socket;
//use Amp\Socket\ResourceSocket;
//use Amp\Socket\SocketConnector;
//
Expand All @@ -23,7 +23,7 @@
// string $uri,
// ?ConnectContext $context = null,
// ?Cancellation $token = null
// ): EncryptableSocket {
// ): Socket {
// $context = $context ?? new ConnectContext;
//
// $options = $context->toStreamContextArray();
Expand Down
16 changes: 11 additions & 5 deletions src/Connection/Http1Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Amp\Http\Client\Connection;

use Amp\ByteStream\ReadableIterableStream;
use Amp\ByteStream\ResourceStream;
use Amp\ByteStream\StreamException;
use Amp\Cancellation;
use Amp\CancelledException;
Expand All @@ -26,7 +27,7 @@
use Amp\Http\InvalidHeaderException;
use Amp\Http\Rfc7230;
use Amp\Pipeline\Queue;
use Amp\Socket\EncryptableSocket;
use Amp\Socket\Socket;
use Amp\Socket\SocketAddress;
use Amp\Socket\TlsInfo;
use Amp\TimeoutCancellation;
Expand All @@ -48,7 +49,7 @@ final class Http1Connection implements Connection
private const MAX_KEEP_ALIVE_TIMEOUT = 60;
private const PROTOCOL_VERSIONS = ['1.0', '1.1'];

private ?EncryptableSocket $socket;
private ?Socket $socket;

private bool $busy = false;

Expand Down Expand Up @@ -78,7 +79,7 @@ final class Http1Connection implements Connection

private ?Future $idleRead = null;

public function __construct(EncryptableSocket $socket, float $timeoutGracePeriod = 2)
public function __construct(Socket $socket, float $timeoutGracePeriod = 2)
{
$this->socket = $socket;
$this->localAddress = $socket->getLocalAddress();
Expand Down Expand Up @@ -192,7 +193,9 @@ private function request(Request $request, Cancellation $cancellation, Stream $s
{
++$this->requestCounter;

$this->socket?->reference();
if ($this->socket instanceof ResourceStream) {
$this->socket->reference();
}

if ($this->timeoutWatcher !== null) {
EventLoop::cancel($this->timeoutWatcher);
Expand Down Expand Up @@ -671,7 +674,10 @@ private function generateRawHeader(Request $request, string $protocolVersion): s

private function watchIdleConnection(): void
{
$this->socket?->unreference();
if ($this->socket instanceof ResourceStream) {
$this->socket->unreference();
}

$this->idleRead = async(function (): ?string {
$chunk = null;
try {
Expand Down
9 changes: 3 additions & 6 deletions src/Connection/Http2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Amp\Http\Client\Connection\Internal\Http2ConnectionProcessor;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Socket\EncryptableSocket;
use Amp\Socket\Socket;
use Amp\Socket\SocketAddress;
use Amp\Socket\TlsInfo;
use Amp\TimeoutCancellation;
Expand All @@ -21,15 +21,12 @@ final class Http2Connection implements Connection

private const PROTOCOL_VERSIONS = ['2'];

private EncryptableSocket $socket;

private Http2ConnectionProcessor $processor;
private readonly Http2ConnectionProcessor $processor;

private int $requestCount = 0;

public function __construct(EncryptableSocket $socket)
public function __construct(private readonly Socket $socket)
{
$this->socket = $socket;
$this->processor = new Http2ConnectionProcessor($socket);
}

Expand Down
11 changes: 7 additions & 4 deletions src/Connection/Internal/Http2ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Amp\ByteStream\ReadableBuffer;
use Amp\ByteStream\ReadableIterableStream;
use Amp\ByteStream\ResourceStream;
use Amp\ByteStream\StreamException;
use Amp\Cancellation;
use Amp\CancelledException;
Expand All @@ -31,8 +32,8 @@
use Amp\Http\HttpStatus;
use Amp\Http\InvalidHeaderException;
use Amp\Pipeline\Queue;
use Amp\Socket\EncryptableSocket;
use Amp\Socket\InternetAddress;
use Amp\Socket\Socket;
use League\Uri;
use Revolt\EventLoop;
use function Amp\async;
Expand Down Expand Up @@ -98,7 +99,7 @@ final class Http2ConnectionProcessor implements Http2Processor
private readonly Queue $frameQueue;

public function __construct(
private readonly EncryptableSocket $socket,
private readonly Socket $socket,
) {
$this->hpack = new HPack();
$this->frameQueue = new Queue();
Expand Down Expand Up @@ -956,7 +957,9 @@ public function request(Request $request, Cancellation $cancellation, Stream $st
throw $exception;
}

$this->socket->reference();
if ($this->socket instanceof ResourceStream) {
$this->socket->reference();
}

// Assign a stream ID just before sending the first frame so another request cannot send a frame with
// a higher ID prior to the initial frame of this stream.
Expand Down Expand Up @@ -1357,7 +1360,7 @@ private function releaseStream(int $streamId, ?\Throwable $exception = null): vo
}
}

if (!$this->streams && !$this->socket->isClosed()) {
if (!$this->streams && !$this->socket->isClosed() && $this->socket instanceof ResourceStream) {
$this->socket->unreference();
}
}
Expand Down
23 changes: 15 additions & 8 deletions src/Connection/UpgradedSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
namespace Amp\Http\Client\Connection;

use Amp\ByteStream\ReadableStreamIteratorAggregate;
use Amp\ByteStream\ResourceStream;
use Amp\Cancellation;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Socket\EncryptableSocket;
use Amp\Socket\Socket;
use Amp\Socket\SocketAddress;
use Amp\Socket\TlsInfo;
use Amp\Socket\TlsState;

/**
* @implements \IteratorAggregate<int, string>
*/
final class UpgradedSocket implements EncryptableSocket, \IteratorAggregate
final class UpgradedSocket implements Socket, ResourceStream, \IteratorAggregate
{
use ForbidCloning;
use ForbidSerialization;
Expand All @@ -25,7 +26,7 @@ final class UpgradedSocket implements EncryptableSocket, \IteratorAggregate
/**
* @param string $buffer Remaining buffer previously read from the socket.
*/
public function __construct(private readonly EncryptableSocket $socket, string $buffer)
public function __construct(private readonly Socket $socket, string $buffer)
{
$this->buffer = $buffer !== '' ? $buffer : null;
}
Expand Down Expand Up @@ -71,12 +72,16 @@ public function end(): void

public function reference(): void
{
$this->socket->reference();
if ($this->socket instanceof ResourceStream) {
$this->socket->reference();
}
}

public function unreference(): void
{
$this->socket->unreference();
if ($this->socket instanceof ResourceStream) {
$this->socket->unreference();
}
}

public function isClosed(): bool
Expand Down Expand Up @@ -109,9 +114,9 @@ public function shutdownTls(?Cancellation $cancellation = null): void
$this->socket->shutdownTls();
}

public function isTlsAvailable(): bool
public function isTlsConfigurationAvailable(): bool
{
return $this->socket->isTlsAvailable();
return $this->socket->isTlsConfigurationAvailable();
}

public function getTlsState(): TlsState
Expand All @@ -136,6 +141,8 @@ public function isWritable(): bool

public function getResource()
{
return $this->socket->getResource();
return $this->socket instanceof ResourceStream
? $this->socket->getResource()
: null;
}
}
2 changes: 1 addition & 1 deletion test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class ClientHttpBinIntegrationTest extends AsyncTestCase
{
private Socket\SocketServer $socket;
private Socket\ServerSocket $socket;

private HttpClient $client;

Expand Down
2 changes: 1 addition & 1 deletion test/Connection/Http1ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function testUpgrade(): void
$socketData = "Data that should be sent after the upgrade response";

$invoked = false;
$callback = function (Socket\EncryptableSocket $socket, Request $request, Response $response) use (
$callback = function (Socket\Socket $socket, Request $request, Response $response) use (
&$invoked,
$socketData
) {
Expand Down
2 changes: 1 addition & 1 deletion test/ConnectionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ConnectionPoolTest extends AsyncTestCase
{
private Socket\SocketServer $socket;
private Socket\ServerSocket $socket;

private HttpClient $client;

Expand Down
4 changes: 2 additions & 2 deletions test/Interceptor/InterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use Amp\Http\Server\SocketHttpServer;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Socket\InternetAddress;
use Amp\Socket\ServerSocket;
use Amp\Socket\SocketAddress;
use Amp\Socket\SocketServer;
use Amp\Socket\StaticSocketConnector;
use Psr\Log\NullLogger;
use function Amp\Socket\socketConnector;
Expand All @@ -31,7 +31,7 @@ abstract class InterceptorTest extends AsyncTestCase

private HttpClient $client;

private SocketServer $serverSocket;
private ServerSocket $serverSocket;

private HttpServer $server;

Expand Down
4 changes: 2 additions & 2 deletions test/TimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testTimeoutDuringConnect(): void
->willReturnCallback(function (
string $uri,
?Socket\ConnectContext $connectContext = null
): Socket\EncryptableSocket {
): Socket\Socket {
$this->assertNotNull($connectContext);
$this->assertSame(0.001, $connectContext->getConnectTimeout());

Expand Down Expand Up @@ -195,7 +195,7 @@ public function testTimeoutDuringConnectInterceptor(): void
->willReturnCallback(function (
string $uri,
?Socket\ConnectContext $connectContext = null
): Socket\EncryptableSocket {
): Socket\Socket {
$this->assertNotNull($connectContext);
$this->assertSame(0.001, $connectContext->getConnectTimeout());

Expand Down

0 comments on commit 2043047

Please sign in to comment.