* @author Kyrian Obikwelu */ final class Server { public function __construct( private readonly Handler $jsonRpcHandler, private readonly LoggerInterface $logger = new NullLogger(), ) { } public static function make(): ServerBuilder { return new ServerBuilder(); } public function connect(TransportInterface $transport): void { $transport->initialize(); $this->logger->info('Transport initialized.', [ 'transport' => $transport::class, ]); $transport->onMessage(function (string $message, ?Uuid $sessionId) use ($transport) { foreach ($this->jsonRpcHandler->process($message, $sessionId) as [$response, $context]) { if (null === $response) { continue; } $transport->send($response, $context); } }); $transport->onSessionEnd(function (Uuid $sessionId) { $this->jsonRpcHandler->destroySession($sessionId); }); } }