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
10 changes: 8 additions & 2 deletions src/FreeDSx/Socket/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FreeDSx\Socket;

use FreeDSx\Socket\Exception\ConnectionException;
use function fclose;
use function fread;
use function fwrite;
use function stream_context_create;
Expand Down Expand Up @@ -122,10 +123,15 @@ public function isEncrypted(): bool
return $this->isEncrypted;
}

public function close(): static
/**
* @param bool $shutdown False closes only this process's FD copy without affecting shared socket state.
*/
public function close(bool $shutdown = true): static
{
if ($this->socket !== null) {
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$shutdown
? stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR)
: fclose($this->socket);
}
$this->socket = null;
$this->isEncrypted = false;
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ public function test_it_should_tell_whether_it_is_connected_for_unix(): void
self::assertFalse($subject->isConnected());
}

public function test_close_without_shutdown_disconnects_tcp(): void
{
$subject = Socket::tcp(
'www.google.com',
(new SocketOptions())->setPort(80),
);

self::assertTrue($subject->isConnected());
$subject->close(shutdown: false);
self::assertFalse($subject->isConnected());
}

public function test_close_without_shutdown_disconnects_unix(): void
{
$path = $this->createUnixServer();
$subject = Socket::unix($path);

self::assertTrue($subject->isConnected());
$subject->close(shutdown: false);
self::assertFalse($subject->isConnected());
}

public function test_it_should_return_at_most_buffer_size_bytes_per_read(): void
{
[$local, $remote] = $this->createSocketPair();
Expand Down
Loading