| | | 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 | | using System.Runtime.Versioning; |
| | | 8 | | |
| | | 9 | | namespace IceRpc.Transports.Quic; |
| | | 10 | | |
| | | 11 | | /// <summary>Implements <see cref="IMultiplexedClientTransport"/> using QUIC.</summary> |
| | | 12 | | [SupportedOSPlatform("linux")] |
| | | 13 | | [SupportedOSPlatform("macos")] |
| | | 14 | | [SupportedOSPlatform("windows")] |
| | | 15 | | public class QuicClientTransport : IMultiplexedClientTransport |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc/> |
| | 127 | 18 | | public string Name => "quic"; |
| | | 19 | | |
| | | 20 | | private readonly QuicClientTransportOptions _quicTransportOptions; |
| | | 21 | | |
| | | 22 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | | 23 | | /// <param name="options">The options to configure the QUIC client transport.</param> |
| | 244 | 24 | | public QuicClientTransport(QuicClientTransportOptions options) => _quicTransportOptions = options; |
| | | 25 | | |
| | | 26 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | | 27 | | public QuicClientTransport() |
| | 4 | 28 | | : this(new QuicClientTransportOptions()) |
| | 4 | 29 | | { |
| | 4 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | public IMultiplexedConnection CreateConnection( |
| | | 34 | | ServerAddress serverAddress, |
| | | 35 | | MultiplexedConnectionOptions options, |
| | | 36 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| | 118 | 37 | | { |
| | 118 | 38 | | if (!QuicConnection.IsSupported) |
| | 0 | 39 | | { |
| | 0 | 40 | | throw new NotSupportedException( |
| | 0 | 41 | | "The QUIC client transport is not available on this system. Please review the Platform Dependencies for |
| | | 42 | | } |
| | | 43 | | |
| | 118 | 44 | | if ((serverAddress.Transport is string transport && transport != Name) || !CheckParams(serverAddress)) |
| | 1 | 45 | | { |
| | 1 | 46 | | throw new ArgumentException( |
| | 1 | 47 | | $"The server address '{serverAddress}' contains parameters that are not valid for the QUIC client transp |
| | 1 | 48 | | nameof(serverAddress)); |
| | | 49 | | } |
| | | 50 | | |
| | 117 | 51 | | if (serverAddress.Transport is null) |
| | 0 | 52 | | { |
| | 0 | 53 | | serverAddress = serverAddress with { Transport = Name }; |
| | 0 | 54 | | } |
| | | 55 | | |
| | 117 | 56 | | clientAuthenticationOptions = clientAuthenticationOptions?.Clone() ?? new(); |
| | 117 | 57 | | clientAuthenticationOptions.TargetHost ??= serverAddress.Host; |
| | 117 | 58 | | clientAuthenticationOptions.ApplicationProtocols ??= |
| | 117 | 59 | | [ |
| | 117 | 60 | | // Mandatory with Quic |
| | 117 | 61 | | new(serverAddress.Protocol.Name) |
| | 117 | 62 | | ]; |
| | | 63 | | |
| | 117 | 64 | | EndPoint endpoint = IPAddress.TryParse(serverAddress.Host, out IPAddress? ipAddress) ? |
| | 117 | 65 | | new IPEndPoint(ipAddress, serverAddress.Port) : |
| | 117 | 66 | | new DnsEndPoint(serverAddress.Host, serverAddress.Port); |
| | | 67 | | |
| | 117 | 68 | | var quicClientOptions = new QuicClientConnectionOptions |
| | 117 | 69 | | { |
| | 117 | 70 | | ClientAuthenticationOptions = clientAuthenticationOptions, |
| | 117 | 71 | | DefaultCloseErrorCode = (int)MultiplexedConnectionCloseError.Aborted, |
| | 117 | 72 | | DefaultStreamErrorCode = 0, |
| | 117 | 73 | | HandshakeTimeout = options.HandshakeTimeout, |
| | 117 | 74 | | IdleTimeout = _quicTransportOptions.IdleTimeout, |
| | 117 | 75 | | InitialReceiveWindowSizes = _quicTransportOptions.InitialReceiveWindowSizes, |
| | 117 | 76 | | KeepAliveInterval = _quicTransportOptions.KeepAliveInterval, |
| | 117 | 77 | | LocalEndPoint = _quicTransportOptions.LocalNetworkAddress, |
| | 117 | 78 | | RemoteEndPoint = endpoint, |
| | 117 | 79 | | MaxInboundBidirectionalStreams = options.MaxBidirectionalStreams, |
| | 117 | 80 | | MaxInboundUnidirectionalStreams = options.MaxUnidirectionalStreams |
| | 117 | 81 | | }; |
| | | 82 | | |
| | 117 | 83 | | return new QuicMultiplexedClientConnection(options, quicClientOptions); |
| | 117 | 84 | | } |
| | | 85 | | |
| | 118 | 86 | | private static bool CheckParams(ServerAddress serverAddress) => serverAddress.Params.Count == 0; |
| | | 87 | | } |