之前在文章在.Net 中使用Quic通信尝鲜 中介绍过如何使用quic协议, 在.net 7中,Quic相关API已经正式可用了, 不过目前还是预览状态,基本示例如下:

服务端代码:

using System;
using System.Net;
using System.Net.Quic;
using System.Net.Security;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
#pragma warning disable CA1416 

namespace QuicExample
{
    [RequiresPreviewFeatures]
    class Program
    {
        static async Task Main(string[] args)
        {
            var isRunning = true;
            
            if (!QuicListener.IsSupported)
            {
                Console.WriteLine("QUIC is not supported.");
                return;
            }
            
            var serverConnectionOptions = new QuicServerConnectionOptions()
            {                
                DefaultStreamErrorCode = 0x0A,                
                DefaultCloseErrorCode = 0x0B, 
                ServerAuthenticationOptions = new SslServerAuthenticationOptions
                {                    
                    ApplicationProtocols = new List<SslApplicationProtocol>() { new SslApplicationProtocol("protocol-name") },                    
                    ServerCertificate = X509Certificate2.CreateFromPemFile("certiciate-file-path")
                }
            };
            
            var listener = await QuicListener.ListenAsync(new QuicListenerOptions()
            {                
                ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),               
                ApplicationProtocols = new List<SslApplicationProtocol>() { new SslApplicationProtocol("protocol-name") },
                ConnectionOptionsCallback = (_, _, _) => ValueTask.FromResult(serverConnectionOptions)
            });

           
            while (isRunning)
            {                
                var connection = await listener.AcceptConnectionAsync();
                var stream = await connection.AcceptInboundStreamAsync();
                // Work with the incoming stream ...

            }


            await listener.DisposeAsync();
        }
    }
}

客户端代码:

using System;
using System.Net;
using System.Net.Quic;
using System.Net.Security;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
#pragma warning disable CA1416 

namespace QuicExample
{
    [RequiresPreviewFeatures]
    class Program
    {
        static async Task Main(string[] args)
        {
            var isRunning = true;
            
            if (!QuicListener.IsSupported)
            {
                Console.WriteLine("QUIC is not supported.");
                return;
            }
            
            var clientConnectionOptions = new QuicClientConnectionOptions()
            {                
                RemoteEndPoint = listener.LocalEndPoint,                
                DefaultStreamErrorCode = 0x0A, 
                DefaultCloseErrorCode = 0x0B, 
                MaxInboundUnidirectionalStreams = 10,
                MaxInboundBidirectionalStreams = 100,
                ClientAuthenticationOptions = new SslClientAuthenticationOptions() { 
                    ApplicationProtocols = new List<SslApplicationProtocol>() { new SslApplicationProtocol("protocol-name") }
                }
            };            
            var connection = await QuicConnection.ConnectAsync(clientConnectionOptions);
            var outgoingStream = await connection.OpenOutboundStreamAsync(QuicStreamType.Bidirectional);
            // Work with the outgoing stream ...

            await connection.CloseAsync(0x0C);
            await connection.DisposeAsync();           
        }
    }
}

使用的时候需要参考前面的文章安装msquic,以及准备证书。目前还是预览阶段,相关接口功能也比较少,并且后续接口也可能会发生变化(和.net 6非公开的相比已经变化不少了)。等正式版再详细使用好了。

参考文章:How to use QUIC (Quick UDP Internet Connections) in .NET 7

posted on 2023-02-04 14:13  天方  阅读(199)  评论(0编辑  收藏  举报