/// <exception cref="WrxTcpAuthorizationFailedException">Если авторизация не удалась.</exception> public static async ValueTask <T> AuthorizeAsync <T>(IPacketWriter <WrxPacket> requestWriter, IPacketReader <WrxPacket> responseReader, IAsyncWrxTcpHubAuthorizer <T> authorizer, ArrayPool <byte> byteArrayPool, CancellationToken cancellationToken = default) { using var requestPacket = new WrxPacket(0, WrxPacketAction.TcpAuthorizationRequest, 0, _zeroes.AsMemory()); await requestWriter.WriteAsync(requestPacket, cancellationToken).ConfigureAwait(false); using var responsePacket = await responseReader.ReadAsync(cancellationToken).ConfigureAwait(false); var(_, responseAction, responseCommand, responseData) = responsePacket; if (responseAction != WrxPacketAction.TcpAuthorizationResponse) { throw new Exception("Invalid action."); } if (responseCommand != 0) { throw new Exception("Invalid command."); } var authorizationData = WrxTcpAuthorizationData.Read(responseData.Span); var info = await authorizer.ValidateDataAsync(authorizationData, cancellationToken).ConfigureAwait(false); using var acknowledgementPacket = new WrxPacket(1, WrxPacketAction.TcpAuthorizationRequest, 1, _zeroes.AsMemory()); await requestWriter.WriteAsync(acknowledgementPacket, cancellationToken).ConfigureAwait(false); return(info); }