Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wj8400684 committed May 3, 2024
1 parent 564e88f commit e7356eb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
37 changes: 18 additions & 19 deletions src/SuperSocket.Quic/QuicConnectionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SuperSocket.Quic
internal sealed class QuicConnectionListener : IConnectionListener
{
private readonly ILogger _logger;
private readonly QuicTransportOptions _quicTransportOptions;

private QuicListener _listenQuic;
private CancellationTokenSource _cancellationTokenSource;
Expand All @@ -23,11 +24,14 @@ internal sealed class QuicConnectionListener : IConnectionListener
public ListenOptions Options { get; }
public bool IsRunning { get; private set; }

public QuicConnectionListener(ListenOptions options, IConnectionFactory connectionFactory, ILogger logger)
public QuicConnectionListener(ListenOptions options,
QuicTransportOptions quicTransportOptions,
IConnectionFactory connectionFactory, ILogger logger)
{
Options = options;
ConnectionFactory = connectionFactory;
_logger = logger;
_quicTransportOptions = quicTransportOptions;
}

public bool Start()
Expand All @@ -38,24 +42,27 @@ public bool Start()
{
var listenEndpoint = options.ToEndPoint();

ArgumentNullException.ThrowIfNull(options.CertificateOptions);
if (options.CertificateOptions == null)
throw new ArgumentNullException(nameof(options.CertificateOptions),"Quic requires an ssl certificate");

if (options.CertificateOptions.Certificate == null)
options.CertificateOptions.EnsureCertificate();

var quicListenerOptions = new QuicListenerOptions
{
ListenBacklog = options.BackLog,
ListenEndPoint = listenEndpoint,
ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 },
ConnectionOptionsCallback = (connection, ssl, token) => ValueTask.FromResult(
new QuicServerConnectionOptions()
new QuicServerConnectionOptions
{
DefaultStreamErrorCode = 0,
DefaultCloseErrorCode = 0,
IdleTimeout = TimeSpan.FromMicroseconds(10),
//MaxInboundBidirectionalStreams = 0,
//MaxInboundUnidirectionalStreams = 0,
DefaultStreamErrorCode = _quicTransportOptions.DefaultStreamErrorCode,
DefaultCloseErrorCode = _quicTransportOptions.DefaultCloseErrorCode,
IdleTimeout = _quicTransportOptions.IdleTimeout.HasValue
? TimeSpan.FromMicroseconds(_quicTransportOptions.IdleTimeout.Value)
: Timeout.InfiniteTimeSpan,
MaxInboundBidirectionalStreams = _quicTransportOptions.MaxBidirectionalStreamCount,
MaxInboundUnidirectionalStreams = _quicTransportOptions.MaxUnidirectionalStreamCount,
ServerAuthenticationOptions = new SslServerAuthenticationOptions()
{
ApplicationProtocols =
Expand All @@ -67,14 +74,7 @@ public bool Start()
})
};

var result = QuicListener.ListenAsync(quicListenerOptions);

if (result.IsCompleted)
_listenQuic = result.Result;
else
_listenQuic = result.GetAwaiter().GetResult();

var listenSocket = _listenQuic;
var listenSocket = QuicListener.ListenAsync(quicListenerOptions).GetAwaiter().GetResult();

IsRunning = true;

Expand All @@ -92,7 +92,6 @@ public bool Start()


private async Task KeepAcceptAsync(QuicListener listenSocket, CancellationToken cancellationToken)

{
while (!cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -156,4 +155,4 @@ public override string ToString()
return Options?.ToString();
}
}
}
}
14 changes: 10 additions & 4 deletions src/SuperSocket.Quic/QuicConnectionListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SuperSocket.Connection;
using SuperSocket.Server.Abstractions;
using SuperSocket.Server.Abstractions.Connections;
Expand All @@ -7,21 +8,26 @@ namespace SuperSocket.Quic
{
internal class QuicConnectionListenerFactory : IConnectionListenerFactory
{
private readonly QuicTransportOptions _quicTransportOptions;
private readonly IConnectionFactoryBuilder _connectionFactoryBuilder;

public QuicConnectionListenerFactory(IConnectionFactoryBuilder connectionFactoryBuilder)
public QuicConnectionListenerFactory(IConnectionFactoryBuilder connectionFactoryBuilder,
IOptions<QuicTransportOptions> options)
{
_connectionFactoryBuilder = connectionFactoryBuilder;
_quicTransportOptions = options.Value;
}

public IConnectionListener CreateConnectionListener(ListenOptions options, ConnectionOptions connectionOptions, ILoggerFactory loggerFactory)

public IConnectionListener CreateConnectionListener(ListenOptions options, ConnectionOptions connectionOptions,
ILoggerFactory loggerFactory)
{
connectionOptions.Logger = loggerFactory.CreateLogger(nameof(IConnection));
var connectionFactoryLogger = loggerFactory.CreateLogger(nameof(QuicConnectionListener));

var connectionFactory = _connectionFactoryBuilder.Build(options, connectionOptions);

return new QuicConnectionListener(options, connectionFactory, connectionFactoryLogger);
return new QuicConnectionListener(options, _quicTransportOptions, connectionFactory,
connectionFactoryLogger);
}
}
}
19 changes: 15 additions & 4 deletions src/SuperSocket.Quic/QuicServerHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
using SuperSocket.Server.Abstractions.Connections;
using SuperSocket.Server.Abstractions.Host;
using SuperSocket.Quic;

#pragma warning disable CA2252

namespace SuperSocket.Server
{
public static class QuicServerHostBuilderExtensions
{
public static ISuperSocketHostBuilder UseQuic(this ISuperSocketHostBuilder hostBuilder)
{
return (hostBuilder as ISuperSocketHostBuilder).UseQuic(o => { }) as
ISuperSocketHostBuilder;
}

public static ISuperSocketHostBuilder UseQuic(this ISuperSocketHostBuilder hostBuilder,
Action<QuicTransportOptions> globalConfigure)
{
if (!QuicListener.IsSupported)
throw new PlatformNotSupportedException("System.Net.Quic is not supported on this platform.");

return hostBuilder.ConfigureServices((_, services) =>
{
services.Configure(globalConfigure);
services.AddSingleton<IConnectionListenerFactory, QuicConnectionListenerFactory>();
services.AddSingleton<IConnectionFactoryBuilder, QuicConnectionFactoryBuilder>();
}) as ISuperSocketHostBuilder;
}

public static ISuperSocketHostBuilder<TReceivePackage> UseQuic<TReceivePackage>(this ISuperSocketHostBuilder<TReceivePackage> hostBuilder)
public static ISuperSocketHostBuilder<TReceivePackage> UseQuic<TReceivePackage>(
this ISuperSocketHostBuilder<TReceivePackage> hostBuilder)
{
return (hostBuilder as ISuperSocketHostBuilder).UseQuic() as ISuperSocketHostBuilder<TReceivePackage>;
return (hostBuilder as ISuperSocketHostBuilder).UseQuic(o => { }) as
ISuperSocketHostBuilder<TReceivePackage>;
}
}
}
}
21 changes: 10 additions & 11 deletions src/SuperSocket.Quic/QuicTransportOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ public sealed class QuicTransportOptions
/// The maximum number of concurrent inbound uni-directional streams per connection.
/// </summary>
public int MaxUnidirectionalStreamCount { get; set; } = 10;

/// <summary>The maximum read size.</summary>
public long MaxReadBufferSize { get; set; } = 1048576L;

/// <summary>The maximum write size.</summary>
public long MaxWriteBufferSize { get; set; } = 65536L;

/// <summary>The maximum length of the pending connection queue.</summary>
public int Backlog { get; set; } = 512;


/// <summary>
/// Error code used when the stream needs to abort the read or write side of the stream internally.
/// </summary>
public long DefaultStreamErrorCode { get; set; }

/// <summary>Error code used when an open connection is disposed.</summary>
/// <summary>
/// Error code used when an open connection is disposed
/// </summary>
public long DefaultCloseErrorCode { get; set; }

/// <summary>Gets or sets the idle timeout for connections. The idle timeout is the time after which the connection will be closed.
/// Default <see cref="F:System.TimeSpan.Zero" /> means underlying implementation default idle timeout.</summary>
/// <returns>The idle timeout for connections. The default is <see cref="F:System.TimeSpan.Zero" />, which means that the default idle timeout of the underlying implementation is used.</returns>
public int? IdleTimeout { get; set; }

}
10 changes: 4 additions & 6 deletions test/SuperSocket.Tests/QuicHostConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
using SuperSocket.Quic;
using SuperSocket.Quic.Connection;

#pragma warning disable CA2252
#if NET7_0_OR_GREATER
namespace SuperSocket.Tests
{
#if NET7_0_OR_GREATER

public class QuicHostConfigurator : IHostConfigurator
{
private static readonly ArrayPool<byte> _bufferPool = ArrayPool<byte>.Shared;
Expand Down Expand Up @@ -100,7 +100,6 @@ public QuicClient(IPipelineFilter<TReceivePackage> pipelineFilter, ConnectionOpt
protected override async ValueTask<bool> ConnectAsync(EndPoint remoteEndPoint,
CancellationToken cancellationToken)
{
#pragma warning disable CA2252
var quicConnection = await QuicConnection.ConnectAsync(
cancellationToken: cancellationToken,
options: new QuicClientConnectionOptions
Expand Down Expand Up @@ -147,6 +146,5 @@ public Socket CreateClient()
throw new NotImplementedException();
}
}

#endif
}
}
#endif

0 comments on commit e7356eb

Please sign in to comment.