2026-02-26 14:39:04 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Coroutine\Server;
|
|
|
|
|
|
|
|
|
|
class Transport
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array<Struct>
|
|
|
|
|
*/
|
|
|
|
|
private array $clients = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-02-26 14:39:04 +08:00
|
|
|
* @param Struct $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function add(int $userId, Struct $data): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
if (isset($this->clients[$userId])) {
|
|
|
|
|
$this->clients[$userId]->ws->close();
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
2026-04-04 10:34:16 +08:00
|
|
|
$this->clients[$userId] = $data;
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-02-26 14:39:04 +08:00
|
|
|
* @param mixed $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function sendUserId(int $userId, mixed $data): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
if (isset($this->clients[$userId])) {
|
|
|
|
|
$this->clients[$userId]->ws->push($data);
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 10:34:16 +08:00
|
|
|
/**
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function sendFd(int $fd, mixed $data): void
|
|
|
|
|
{
|
|
|
|
|
$struct = $this->getClientId($fd);
|
|
|
|
|
$struct?->ws->push($data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:39:04 +08:00
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-02-26 14:39:04 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function close(int $userId): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
if (isset($this->clients[$userId])) {
|
|
|
|
|
$this->clients[$userId]->ws->close();
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-02-26 14:39:04 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function remove(int $userId): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
if ($this->has($userId)) {
|
|
|
|
|
$this->clients[$userId]?->ws->close();
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
2026-04-04 10:34:16 +08:00
|
|
|
unset($this->clients[$userId]);
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-04 10:29:39 +08:00
|
|
|
/**
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @return Struct|null
|
|
|
|
|
*/
|
|
|
|
|
public function getClientId(int $fd): ?Struct
|
|
|
|
|
{
|
|
|
|
|
return array_find($this->clients, fn($client) => $client->fd == $fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-04-04 10:29:39 +08:00
|
|
|
* @return Struct|null
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function getUserId(int $userId): ?Struct
|
2026-04-04 10:29:39 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
return $this->clients[$userId] ?? null;
|
2026-04-04 10:29:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:34:16 +08:00
|
|
|
* @param int $userId
|
2026-04-04 10:29:39 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2026-04-04 10:34:16 +08:00
|
|
|
public function has(int $userId): bool
|
2026-04-04 10:29:39 +08:00
|
|
|
{
|
2026-04-04 10:34:16 +08:00
|
|
|
return isset($this->clients[$userId]);
|
2026-04-04 10:29:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-02-26 14:39:04 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getLists(): array
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
2026-04-04 10:34:16 +08:00
|
|
|
foreach ($this->clients as $userId => $client) {
|
2026-02-26 14:39:04 +08:00
|
|
|
$array[] = [
|
2026-04-04 10:34:16 +08:00
|
|
|
'userId' => $userId,
|
2026-04-04 10:29:39 +08:00
|
|
|
'nickname' => $client->user->getNickname(),
|
2026-02-26 14:39:04 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function size(): int
|
|
|
|
|
{
|
|
|
|
|
return count($this->clients);
|
|
|
|
|
}
|
|
|
|
|
}
|