| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports.Quic.Internal; |
| | 4 | | using System.Net; |
| | 5 | | using System.Net.Quic; |
| | 6 | | using System.Net.Security; |
| | 7 | |
|
| | 8 | | namespace IceRpc.Transports.Quic; |
| | 9 | |
|
| | 10 | | /// <summary>Implements <see cref="IMultiplexedClientTransport"/> using QUIC.</summary> |
| | 11 | | public class QuicClientTransport : IMultiplexedClientTransport |
| | 12 | | { |
| | 13 | | /// <inheritdoc/> |
| 235 | 14 | | public string Name => "quic"; |
| | 15 | |
|
| | 16 | | private readonly QuicClientTransportOptions _quicTransportOptions; |
| | 17 | |
|
| | 18 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | 19 | | /// <param name="options">The options to configure the QUIC client transport.</param> |
| 470 | 20 | | public QuicClientTransport(QuicClientTransportOptions options) => _quicTransportOptions = options; |
| | 21 | |
|
| | 22 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | 23 | | public QuicClientTransport() |
| 0 | 24 | | : this(new QuicClientTransportOptions()) |
| 0 | 25 | | { |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <inheritdoc/> |
| | 29 | | public IMultiplexedConnection CreateConnection( |
| | 30 | | ServerAddress serverAddress, |
| | 31 | | MultiplexedConnectionOptions options, |
| | 32 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| 235 | 33 | | { |
| 235 | 34 | | if (!QuicConnection.IsSupported) |
| 0 | 35 | | { |
| 0 | 36 | | throw new NotSupportedException("The Quic client transport is not supported on this platform."); |
| | 37 | | } |
| | 38 | |
|
| 235 | 39 | | if ((serverAddress.Transport is string transport && transport != Name) || !CheckParams(serverAddress)) |
| 2 | 40 | | { |
| 2 | 41 | | throw new ArgumentException( |
| 2 | 42 | | $"The server address '{serverAddress}' contains parameters that are not valid for the Quic client transp |
| 2 | 43 | | nameof(serverAddress)); |
| | 44 | | } |
| | 45 | |
|
| 233 | 46 | | if (serverAddress.Transport is null) |
| 0 | 47 | | { |
| 0 | 48 | | serverAddress = serverAddress with { Transport = Name }; |
| 0 | 49 | | } |
| | 50 | |
|
| 233 | 51 | | clientAuthenticationOptions = clientAuthenticationOptions?.Clone() ?? new(); |
| 233 | 52 | | clientAuthenticationOptions.TargetHost ??= serverAddress.Host; |
| 233 | 53 | | clientAuthenticationOptions.ApplicationProtocols ??= new List<SslApplicationProtocol> // Mandatory with Quic |
| 233 | 54 | | { |
| 233 | 55 | | new SslApplicationProtocol(serverAddress.Protocol.Name) |
| 233 | 56 | | }; |
| | 57 | |
|
| 233 | 58 | | EndPoint endpoint = IPAddress.TryParse(serverAddress.Host, out IPAddress? ipAddress) ? |
| 233 | 59 | | new IPEndPoint(ipAddress, serverAddress.Port) : |
| 233 | 60 | | new DnsEndPoint(serverAddress.Host, serverAddress.Port); |
| | 61 | |
|
| 233 | 62 | | var quicClientOptions = new QuicClientConnectionOptions |
| 233 | 63 | | { |
| 233 | 64 | | ClientAuthenticationOptions = clientAuthenticationOptions, |
| 233 | 65 | | DefaultCloseErrorCode = (int)MultiplexedConnectionCloseError.Aborted, |
| 233 | 66 | | DefaultStreamErrorCode = 0, |
| 233 | 67 | | IdleTimeout = _quicTransportOptions.IdleTimeout, |
| 233 | 68 | | #if NET9_0_OR_GREATER |
| 233 | 69 | | KeepAliveInterval = _quicTransportOptions.KeepAliveInterval, |
| 233 | 70 | | #endif |
| 233 | 71 | | LocalEndPoint = _quicTransportOptions.LocalNetworkAddress, |
| 233 | 72 | | RemoteEndPoint = endpoint, |
| 233 | 73 | | MaxInboundBidirectionalStreams = options.MaxBidirectionalStreams, |
| 233 | 74 | | MaxInboundUnidirectionalStreams = options.MaxUnidirectionalStreams |
| 233 | 75 | | }; |
| | 76 | |
|
| 233 | 77 | | return new QuicMultiplexedClientConnection(options, quicClientOptions); |
| 233 | 78 | | } |
| | 79 | |
|
| 235 | 80 | | private static bool CheckParams(ServerAddress serverAddress) => serverAddress.Params.Count == 0; |
| | 81 | | } |