-
-
Notifications
You must be signed in to change notification settings - Fork 0
M1I03: Socket wrapper #58
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| 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)), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)), | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
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(); | ||
| } | ||
|
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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.