Skip to content
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
94 changes: 94 additions & 0 deletions src/Server/Socket/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace CrazyGoat\Forklift\Server\Socket;

class Connection
{
private float $lastActivity;
private bool $closed = false;

public function __construct(private readonly \Socket $resource)
{
$this->lastActivity = \microtime(true);
}

public function read(int $length = 65536): string|false
{
if ($this->closed) {
return false;
}

$data = \socket_read($this->resource, $length);

if ($data !== false) {
$this->lastActivity = \microtime(true);
}

return $data;
}

public function write(string $data): int|false
{
if ($this->closed) {
return false;
}

$result = \socket_write($this->resource, $data, strlen($data));

if ($result !== false) {
$this->lastActivity = \microtime(true);
}

return $result;
}

public function close(): void
{
if (!$this->closed) {
\socket_close($this->resource);
$this->closed = true;
}
}

public function isClosed(): bool
{
return $this->closed;
}

/** @return array{host: string, port: int} */
public function getPeerName(): array
Comment thread
s2x marked this conversation as resolved.
{
if ($this->closed) {
throw new \RuntimeException('Connection is closed');
}

if (!\socket_getpeername($this->resource, $addr, $port)) {
throw new \RuntimeException('Failed to get peer name');
}

/** @var string $addr */
/** @var int $port */
return ['host' => $addr, 'port' => $port];
}

public function getLastActivity(): float
{
return $this->lastActivity;
}

/** @param array<int, mixed>|int|string $value */
public function setOption(int $level, int $option, array|int|string $value): void
{
if ($this->closed) {
return;
}

if (\socket_set_option($this->resource, $level, $option, $value) === false) {
throw new \RuntimeException(
\socket_strerror(\socket_last_error($this->resource)),
);
}
}
}
81 changes: 81 additions & 0 deletions src/Server/Socket/Socket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace CrazyGoat\Forklift\Server\Socket;

use CrazyGoat\Forklift\Server\Exception\SocketAcceptException;
use CrazyGoat\Forklift\Server\Exception\SocketCreationException;

class Socket
{
public function __construct(private ?\Socket $resource)
{
}

public function accept(): Connection
{
if (!$this->resource instanceof \Socket) {
throw new SocketAcceptException('Socket is closed');
}

$accepted = @\socket_accept($this->resource);

if ($accepted === false) {
throw new SocketAcceptException(
\socket_strerror(\socket_last_error($this->resource)),
);
}

return new Connection($accepted);
}

public function close(): void
{
if ($this->resource instanceof \Socket) {
\socket_close($this->resource);
}

$this->resource = null;
}

/** @param array<int, mixed>|int|string $value */
public function setOption(int $level, int $option, array|int|string $value): void
{
if (!$this->resource instanceof \Socket) {
throw new SocketCreationException('Socket is closed');
}

if (\socket_set_option($this->resource, $level, $option, $value) === false) {
throw new SocketCreationException(
\socket_strerror(\socket_last_error($this->resource)),
);
}
}

public function bind(string $address, int $port): void
{
if (!$this->resource instanceof \Socket) {
throw new SocketCreationException('Socket is closed');
}

if (!\socket_bind($this->resource, $address, $port)) {
throw new SocketCreationException(
\socket_strerror(\socket_last_error($this->resource)),
);
}
}

public function listen(int $backlog = SOMAXCONN): void
{
if (!$this->resource instanceof \Socket) {
throw new SocketCreationException('Socket is closed');
}

if (!\socket_listen($this->resource, $backlog)) {
throw new SocketCreationException(
\socket_strerror(\socket_last_error($this->resource)),
);
}
}
}
148 changes: 148 additions & 0 deletions tests/Server/Socket/SocketTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace CrazyGoat\Forklift\Tests\Server\Socket;

use CrazyGoat\Forklift\Server\Exception\SocketAcceptException;
use CrazyGoat\Forklift\Server\Exception\SocketCreationException;
use CrazyGoat\Forklift\Server\Socket\Connection;
use CrazyGoat\Forklift\Server\Socket\Socket;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

class SocketTest extends TestCase
{
public function testConstructorWrapsResource(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);

$this->assertInstanceOf(Socket::class, $socket);

\socket_close($resource);
}

public function testBindAndListen(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->setOption(SOL_SOCKET, SO_REUSEADDR, 1);
$socket->bind('127.0.0.1', 0);
$socket->listen(5);

$socket->close();
Comment thread
s2x marked this conversation as resolved.
}

public function testAcceptReturnsConnection(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->setOption(SOL_SOCKET, SO_REUSEADDR, 1);
$socket->bind('127.0.0.1', 0);
$socket->listen();

\socket_getsockname($resource, $addr, $port);
$this->assertIsString($addr);
$this->assertIsInt($port);

$client = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($client);

\socket_connect($client, $addr, $port);

$connection = $socket->accept();

$this->assertInstanceOf(Connection::class, $connection);

\socket_close($client);
$connection->close();
$socket->close();
}
Comment thread
s2x marked this conversation as resolved.

public function testCloseIsIdempotent(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);

$socket->close();
$socket->close();

$reflection = new ReflectionProperty(Socket::class, 'resource');

$this->assertNull($reflection->getValue($socket));
}

public function testCloseSetsResourceToNull(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->close();

$reflection = new ReflectionProperty(Socket::class, 'resource');

$this->assertNull($reflection->getValue($socket));
}

public function testAcceptThrowsAfterClose(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->close();

$this->expectException(SocketAcceptException::class);

$socket->accept();
}

public function testBindThrowsAfterClose(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->close();

$this->expectException(SocketCreationException::class);

$socket->bind('127.0.0.1', 8080);
}

public function testListenThrowsAfterClose(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->close();

$this->expectException(SocketCreationException::class);

$socket->listen();
}

public function testSetOptionThrowsAfterClose(): void
{
$resource = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->assertNotFalse($resource);

$socket = new Socket($resource);
$socket->close();

$this->expectException(SocketCreationException::class);

$socket->setOption(SOL_SOCKET, SO_REUSEADDR, 1);
}
}
Loading