|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Micilini\PhpSockets\Connection; |
| 6 | + |
| 7 | +use Micilini\PhpSockets\Protocol\CloseCode; |
| 8 | +use Micilini\PhpSockets\Protocol\Frame; |
| 9 | +use Micilini\PhpSockets\Protocol\FrameCodec; |
| 10 | +use RuntimeException; |
| 11 | +use Socket; |
| 12 | + |
| 13 | +final class Connection |
| 14 | +{ |
| 15 | + private ConnectionState $state; |
| 16 | + private ?string $userId = null; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly string $id, |
| 20 | + private readonly Socket $socket, |
| 21 | + private readonly FrameCodec $codec, |
| 22 | + private readonly ?string $remoteAddress = null, |
| 23 | + ConnectionState $state = ConnectionState::OPEN, |
| 24 | + ) { |
| 25 | + $this->state = $state; |
| 26 | + } |
| 27 | + |
| 28 | + public function id(): string |
| 29 | + { |
| 30 | + return $this->id; |
| 31 | + } |
| 32 | + |
| 33 | + public function socket(): Socket |
| 34 | + { |
| 35 | + return $this->socket; |
| 36 | + } |
| 37 | + |
| 38 | + public function socketId(): int |
| 39 | + { |
| 40 | + return spl_object_id($this->socket); |
| 41 | + } |
| 42 | + |
| 43 | + public function state(): ConnectionState |
| 44 | + { |
| 45 | + return $this->state; |
| 46 | + } |
| 47 | + |
| 48 | + public function remoteAddress(): ?string |
| 49 | + { |
| 50 | + return $this->remoteAddress; |
| 51 | + } |
| 52 | + |
| 53 | + public function userId(): ?string |
| 54 | + { |
| 55 | + return $this->userId; |
| 56 | + } |
| 57 | + |
| 58 | + public function setUserId(?string $userId): void |
| 59 | + { |
| 60 | + $this->userId = $userId; |
| 61 | + } |
| 62 | + |
| 63 | + public function markOpen(): void |
| 64 | + { |
| 65 | + $this->state = ConnectionState::OPEN; |
| 66 | + } |
| 67 | + |
| 68 | + public function markClosing(): void |
| 69 | + { |
| 70 | + $this->state = ConnectionState::CLOSING; |
| 71 | + } |
| 72 | + |
| 73 | + public function markClosed(): void |
| 74 | + { |
| 75 | + $this->state = ConnectionState::CLOSED; |
| 76 | + } |
| 77 | + |
| 78 | + public function send(string|Frame $message): void |
| 79 | + { |
| 80 | + if ($this->state === ConnectionState::CLOSED) { |
| 81 | + throw new RuntimeException('Cannot send data to a closed WebSocket connection.'); |
| 82 | + } |
| 83 | + |
| 84 | + $frame = is_string($message) ? Frame::text($message) : $message; |
| 85 | + $this->writeAll($this->codec->encode($frame)); |
| 86 | + } |
| 87 | + |
| 88 | + public function close(int $code = CloseCode::NORMAL_CLOSURE->value, string $reason = ''): void |
| 89 | + { |
| 90 | + if ($this->state === ConnectionState::CLOSED) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + $this->state = ConnectionState::CLOSING; |
| 95 | + $payload = pack('n', $code) . $reason; |
| 96 | + |
| 97 | + try { |
| 98 | + $this->send(Frame::close($payload)); |
| 99 | + } catch (RuntimeException) { |
| 100 | + } |
| 101 | + |
| 102 | + socket_close($this->socket); |
| 103 | + $this->state = ConnectionState::CLOSED; |
| 104 | + } |
| 105 | + |
| 106 | + private function writeAll(string $bytes): void |
| 107 | + { |
| 108 | + $length = strlen($bytes); |
| 109 | + $written = 0; |
| 110 | + |
| 111 | + while ($written < $length) { |
| 112 | + $result = socket_write($this->socket, substr($bytes, $written)); |
| 113 | + |
| 114 | + if ($result === false || $result === 0) { |
| 115 | + throw new RuntimeException('Failed to write data to the WebSocket connection.'); |
| 116 | + } |
| 117 | + |
| 118 | + $written += $result; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments