|
| 1 | +// Copyright (c) ZeroC, Inc. |
| 2 | + |
| 3 | +using IceRpc; |
| 4 | +using IceRpc.Transports.Quic; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using System.Net.Security; |
| 7 | +using System.Security.Cryptography.X509Certificates; |
| 8 | +using VisitorCenter; |
| 9 | + |
| 10 | +using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => |
| 11 | + builder |
| 12 | + .AddSimpleConsole() |
| 13 | + .AddFilter("IceRpc", LogLevel.Information)); |
| 14 | + |
| 15 | +// SSL setup |
| 16 | +using var rootCA = new X509Certificate2("../../../../certs/cacert.der"); |
| 17 | +var clientAuthenticationOptions = new SslClientAuthenticationOptions |
| 18 | +{ |
| 19 | + RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => |
| 20 | + { |
| 21 | + using var customChain = new X509Chain(); |
| 22 | + customChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; |
| 23 | + customChain.ChainPolicy.DisableCertificateDownloads = true; |
| 24 | + customChain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; |
| 25 | + customChain.ChainPolicy.CustomTrustStore.Add(rootCA); |
| 26 | + return customChain.Build((X509Certificate2)certificate!); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +// Use our own factory method (see below) to create a client connection using QUIC with a fallback to TCP. |
| 31 | +await using ClientConnection connection = await CreateClientConnectionAsync( |
| 32 | + new Uri("icerpc://localhost"), |
| 33 | + clientAuthenticationOptions, |
| 34 | + loggerFactory.CreateLogger<ClientConnection>()); |
| 35 | + |
| 36 | +// Create an invocation pipeline and install the logger interceptor. |
| 37 | +Pipeline pipeline = new Pipeline() |
| 38 | + .UseLogger(loggerFactory) |
| 39 | + .Into(connection); |
| 40 | + |
| 41 | +// Create a greeter proxy with this invocation pipeline. |
| 42 | +var greeter = new GreeterProxy(pipeline); |
| 43 | + |
| 44 | +string greeting = await greeter.GreetAsync(Environment.UserName); |
| 45 | + |
| 46 | +Console.WriteLine(greeting); |
| 47 | + |
| 48 | +await connection.ShutdownAsync(); |
| 49 | + |
| 50 | +// Creates a client connection connected to the server over QUIC if possible. If the QUIC connection establishment |
| 51 | +// fails, fall back to a TCP connection. The caller must dispose the returned connection. |
| 52 | +static async Task<ClientConnection> CreateClientConnectionAsync( |
| 53 | + Uri serverAddressUri, |
| 54 | + SslClientAuthenticationOptions clientAuthenticationOptions, |
| 55 | + ILogger logger) |
| 56 | +{ |
| 57 | +#pragma warning disable CA2000 // Dispose objects before losing scope |
| 58 | + // This client connection is either returned to the caller after a successful ConnectAsync, or disposed if the |
| 59 | + // ConnectAsync fails. |
| 60 | + var quicConnection = new ClientConnection( |
| 61 | + serverAddressUri, |
| 62 | + clientAuthenticationOptions, |
| 63 | + multiplexedClientTransport: new QuicClientTransport(), |
| 64 | + logger: logger); |
| 65 | +#pragma warning restore CA2000 |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + await quicConnection.ConnectAsync(); |
| 70 | + return quicConnection; |
| 71 | + } |
| 72 | + catch (Exception exception) |
| 73 | + { |
| 74 | + logger.LogInformation(exception, "Failed to connect to server using QUIC, falling back to TCP"); |
| 75 | + await quicConnection.DisposeAsync(); |
| 76 | + } |
| 77 | + |
| 78 | +#pragma warning disable CA2000 // Dispose objects before losing scope |
| 79 | + return new ClientConnection(serverAddressUri, clientAuthenticationOptions, logger: logger); |
| 80 | +#pragma warning restore CA2000 |
| 81 | +} |
0 commit comments