Skip to content

Commit d5bf3fe

Browse files
committed
Updates
1 parent b09352b commit d5bf3fe

File tree

5 files changed

+23
-28
lines changed

5 files changed

+23
-28
lines changed

src/Hardware/Remote/RemoteBlockDevice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ abstract class RemoteBlockDevice extends RemoteDevice implements Seek
1010
{
1111
public function lseek(int $offset, int $whence): int
1212
{
13-
return (int) $this->socket->request('lseek', ['offset' => $offset, 'whence' => $whence])->await();
13+
return (int) $this->peer->request('lseek', ['offset' => $offset, 'whence' => $whence])->await();
1414
}
1515
}

src/Hardware/Remote/RemoteCharacterDevice.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Flat3\RevPi\Interfaces\Hardware\Stream;
88
use Flat3\RevPi\JsonRpc\Event;
9+
use Flat3\RevPi\JsonRpc\Peer;
910
use Revolt\EventLoop;
1011

1112
abstract class RemoteCharacterDevice extends RemoteDevice implements Stream
@@ -16,8 +17,10 @@ abstract class RemoteCharacterDevice extends RemoteDevice implements Stream
1617
/** @var resource */
1718
protected mixed $remote;
1819

19-
public function __construct()
20+
public function __construct(Peer $peer)
2021
{
22+
parent::__construct($peer);
23+
2124
$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
2225
assert($sockets !== false);
2326
[$this->local, $this->remote] = $sockets;
@@ -27,9 +30,9 @@ public function __construct()
2730

2831
public function fdopen(): mixed
2932
{
30-
$this->socket->request('fdopen')->await();
33+
$this->peer->request('fdopen')->await();
3134

32-
$this->socket->on(function (Event $event) {
35+
$this->peer->on(function (Event $event) {
3336
if ($event->type === 'readable') {
3437
fwrite($this->remote, $event->payload);
3538
}

src/Hardware/Remote/RemoteDevice.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@
1111

1212
abstract class RemoteDevice implements Device, Ioctl
1313
{
14-
protected Peer $socket;
14+
public function __construct(protected Peer $peer) {}
1515

1616
public function socket(WebsocketClient $websocket): void
1717
{
18-
$this->socket = Peer::initiate($websocket);
18+
$this->peer->setSocket($websocket);
1919
}
2020

2121
public function open(string $pathname, int $flags): int
2222
{
23-
return (int) $this->socket->request('open', ['pathname' => $pathname, 'flags' => $flags])->await();
23+
return (int) $this->peer->request('open', ['pathname' => $pathname, 'flags' => $flags])->await();
2424
}
2525

2626
public function close(): int
2727
{
28-
return (int) $this->socket->request('close')->await();
28+
return (int) $this->peer->request('close')->await();
2929
}
3030

3131
public function read(string &$buffer, int $count): int
3232
{
3333
/** @var array{buffer: string, return: int} $response */
34-
$response = $this->socket->request('read', ['buffer' => $buffer, 'count' => $count])->await();
34+
$response = $this->peer->request('read', ['buffer' => $buffer, 'count' => $count])->await();
3535
$buffer = $response['buffer'];
3636

3737
return $response['return'];
3838
}
3939

4040
public function write(string $buffer, int $count): int
4141
{
42-
return (int) $this->socket->request('write', ['buffer' => $buffer, 'count' => $count])->await();
42+
return (int) $this->peer->request('write', ['buffer' => $buffer, 'count' => $count])->await();
4343
}
4444

4545
public function ioctl(int $request, ?string &$argp = null): int
4646
{
4747
/** @var array{argp: ?string, return: int} $response */
48-
$response = $this->socket->request('ioctl', ['request' => $request, 'argp' => $argp])->await();
48+
$response = $this->peer->request('ioctl', ['request' => $request, 'argp' => $argp])->await();
4949

5050
$argp = $response['argp'];
5151

src/Hardware/Remote/RemoteTerminalDevice.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RemoteTerminalDevice extends RemoteCharacterDevice implements Terminal
1111
public function cfgetispeed(string &$buffer): int
1212
{
1313
/** @var array{buffer: string, return: int} $response */
14-
$response = $this->socket->request('cfgetispeed', ['buffer' => $buffer])->await();
14+
$response = $this->peer->request('cfgetispeed', ['buffer' => $buffer])->await();
1515
$buffer = $response['buffer'];
1616

1717
return $response['return'];
@@ -20,7 +20,7 @@ public function cfgetispeed(string &$buffer): int
2020
public function cfgetospeed(string &$buffer): int
2121
{
2222
/** @var array{buffer: string, return: int} $response */
23-
$response = $this->socket->request('cfgetospeed', ['buffer' => $buffer])->await();
23+
$response = $this->peer->request('cfgetospeed', ['buffer' => $buffer])->await();
2424
$buffer = $response['buffer'];
2525

2626
return $response['return'];
@@ -29,7 +29,7 @@ public function cfgetospeed(string &$buffer): int
2929
public function cfsetispeed(string &$buffer, int $speed): int
3030
{
3131
/** @var array{buffer: string, return: int} $response */
32-
$response = $this->socket->request('cfsetispeed', ['buffer' => $buffer, 'speed' => $speed])->await();
32+
$response = $this->peer->request('cfsetispeed', ['buffer' => $buffer, 'speed' => $speed])->await();
3333
$buffer = $response['buffer'];
3434

3535
return $response['return'];
@@ -38,24 +38,24 @@ public function cfsetispeed(string &$buffer, int $speed): int
3838
public function cfsetospeed(string &$buffer, int $speed): int
3939
{
4040
/** @var array{buffer: string, return: int} $response */
41-
$response = $this->socket->request('cfsetospeed', ['buffer' => $buffer, 'speed' => $speed])->await();
41+
$response = $this->peer->request('cfsetospeed', ['buffer' => $buffer, 'speed' => $speed])->await();
4242
$buffer = $response['buffer'];
4343

4444
return $response['return'];
4545
}
4646

4747
public function tcflush(int $queue_selector): int
4848
{
49-
return (int) $this->socket->request('tcflush', ['queue_selector' => $queue_selector])->await();
49+
return (int) $this->peer->request('tcflush', ['queue_selector' => $queue_selector])->await();
5050
}
5151

5252
public function tcdrain(): int
5353
{
54-
return (int) $this->socket->request('tcdrain')->await();
54+
return (int) $this->peer->request('tcdrain')->await();
5555
}
5656

5757
public function tcsendbreak(int $duration = 0): int
5858
{
59-
return (int) $this->socket->request('tcsendbreak', ['duration' => $duration])->await();
59+
return (int) $this->peer->request('tcsendbreak', ['duration' => $duration])->await();
6060
}
6161
}

src/JsonRpc/Peer.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,10 @@ class Peer implements WebsocketClientHandler
5050
*/
5151
protected array $pending = [];
5252

53-
public static function initiate(WebsocketClient $socket): self
54-
{
55-
$endpoint = new self;
56-
$endpoint->setSocket($socket);
57-
async(fn () => $endpoint->receiveLoop());
58-
59-
return $endpoint;
60-
}
61-
6253
public function setSocket(WebsocketClient $socket): self
6354
{
6455
$this->socket = $socket;
56+
async(fn () => $this->receiveLoop());
6557

6658
return $this;
6759
}
@@ -287,7 +279,7 @@ public function handleClient(
287279
Request $request,
288280
Response $response
289281
): void {
290-
$this->setSocket($client);
282+
$this->socket = $client;
291283

292284
while ($message = $client->receive()) {
293285
$request = $message->read();

0 commit comments

Comments
 (0)