diff --git a/src/Shared/runtime/Http2/Hpack/HPackDecoder.cs b/src/Shared/runtime/Http2/Hpack/HPackDecoder.cs index 41eb1c6938a5..3fe3c86243d3 100644 --- a/src/Shared/runtime/Http2/Hpack/HPackDecoder.cs +++ b/src/Shared/runtime/Http2/Hpack/HPackDecoder.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; #if KESTREL @@ -93,7 +94,7 @@ private enum State : byte private byte[] _headerValueOctets; private State _state = State.Ready; - private byte[] _headerName; + private byte[]? _headerName; private int _stringIndex; private int _stringLength; private int _headerNameLength; @@ -129,13 +130,13 @@ public void Decode(in ReadOnlySequence data, bool endHeaders, IHttpHeaders CheckIncompleteHeaderBlock(endHeaders); } - public void Decode(ReadOnlySpan data, bool endHeaders, IHttpHeadersHandler handler) + public void Decode(ReadOnlySpan data, bool endHeaders, IHttpHeadersHandler? handler) { DecodeInternal(data, endHeaders, handler); CheckIncompleteHeaderBlock(endHeaders); } - private void DecodeInternal(ReadOnlySpan data, bool endHeaders, IHttpHeadersHandler handler) + private void DecodeInternal(ReadOnlySpan data, bool endHeaders, IHttpHeadersHandler? handler) { int intResult; @@ -370,7 +371,7 @@ private void CheckIncompleteHeaderBlock(bool endHeaders) } } - private void ProcessHeaderValue(IHttpHeadersHandler handler) + private void ProcessHeaderValue(IHttpHeadersHandler? handler) { OnString(nextState: State.Ready); @@ -394,7 +395,7 @@ public void CompleteDecode() } } - private void OnIndexedHeaderField(int index, IHttpHeadersHandler handler) + private void OnIndexedHeaderField(int index, IHttpHeadersHandler? handler) { HeaderField header = GetHeader(index); handler?.OnHeader(header.Name, header.Value); diff --git a/src/Shared/runtime/Http2/Hpack/HPackEncoder.cs b/src/Shared/runtime/Http2/Hpack/HPackEncoder.cs index 15aec6cfa659..d09f1841349d 100644 --- a/src/Shared/runtime/Http2/Hpack/HPackEncoder.cs +++ b/src/Shared/runtime/Http2/Hpack/HPackEncoder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable +using System.Collections.Generic; using System.Diagnostics; namespace System.Net.Http.HPack @@ -370,7 +372,7 @@ public static bool EncodeStringLiteral(string value, Span destination, out return false; } - public static bool EncodeStringLiterals(ReadOnlySpan values, string separator, Span destination, out int bytesWritten) + public static bool EncodeStringLiterals(ReadOnlySpan values, string? separator, Span destination, out int bytesWritten) { bytesWritten = 0; @@ -393,6 +395,7 @@ public static bool EncodeStringLiterals(ReadOnlySpan values, string sepa valueLength = checked((int)(valueLength + part.Length)); } + Debug.Assert(separator != null); valueLength = checked((int)(valueLength + (values.Length - 1) * separator.Length)); destination[0] = 0; diff --git a/src/Shared/runtime/Http3/QPack/QPackDecoder.cs b/src/Shared/runtime/Http3/QPack/QPackDecoder.cs index 958dfac303e8..e3b85872ed75 100644 --- a/src/Shared/runtime/Http3/QPack/QPackDecoder.cs +++ b/src/Shared/runtime/Http3/QPack/QPackDecoder.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.Net.Http.HPack; @@ -118,7 +119,7 @@ private enum State private bool _huffman; private int? _index; - private byte[] _headerName; + private byte[]? _headerName; private int _headerNameLength; private int _headerValueLength; private int _stringLength; @@ -130,7 +131,7 @@ private enum State private static void ReturnAndGetNewPooledArray(ref byte[] buffer, int newSize) { byte[] old = buffer; - buffer = null; + buffer = null!; Pool.Return(old, clearArray: true); buffer = Pool.Rent(newSize); @@ -151,19 +152,19 @@ public void Dispose() if (_stringOctets != null) { Pool.Return(_stringOctets, true); - _stringOctets = null; + _stringOctets = null!; } if (_headerNameOctets != null) { Pool.Return(_headerNameOctets, true); - _headerNameOctets = null; + _headerNameOctets = null!; } if (_headerValueOctets != null) { Pool.Return(_headerValueOctets, true); - _headerValueOctets = null; + _headerValueOctets = null!; } } diff --git a/src/Shared/runtime/Http3/QPack/QPackEncoder.cs b/src/Shared/runtime/Http3/QPack/QPackEncoder.cs index ecf0f1e183ab..c95609591062 100644 --- a/src/Shared/runtime/Http3/QPack/QPackEncoder.cs +++ b/src/Shared/runtime/Http3/QPack/QPackEncoder.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Collections.Generic; using System.Diagnostics; using System.Net.Http.HPack; @@ -10,7 +11,7 @@ namespace System.Net.Http.QPack { internal class QPackEncoder { - private IEnumerator> _enumerator; + private IEnumerator>? _enumerator; // https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.2 // 0 1 2 3 4 5 6 7 @@ -194,7 +195,7 @@ private static bool EncodeValueString(string s, Span buffer, out int lengt /// /// Encodes a value by concatenating a collection of strings, separated by a separator string. /// - public static bool EncodeValueString(ReadOnlySpan values, string separator, Span buffer, out int length) + public static bool EncodeValueString(ReadOnlySpan values, string? separator, Span buffer, out int length) { if (values.Length == 1) { @@ -209,6 +210,7 @@ public static bool EncodeValueString(ReadOnlySpan values, string separat if (buffer.Length > 0) { + Debug.Assert(separator != null); int valueLength = separator.Length * (values.Length - 1); for (int i = 0; i < values.Length; ++i) { @@ -386,7 +388,7 @@ private bool Encode(Span buffer, bool throwIfNoneEncoded, out int length) do { - if (!EncodeLiteralHeaderFieldWithoutNameReference(_enumerator.Current.Key, _enumerator.Current.Value, buffer.Slice(length), out int headerLength)) + if (!EncodeLiteralHeaderFieldWithoutNameReference(_enumerator!.Current.Key, _enumerator.Current.Value, buffer.Slice(length), out int headerLength)) { if (length == 0 && throwIfNoneEncoded) { diff --git a/src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs b/src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs index d9ab07022e72..ae4040621033 100644 --- a/src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs +++ b/src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers.Binary; using System.Net.Security; using System.Net.Sockets; @@ -14,17 +15,17 @@ internal sealed class MockConnection : QuicConnectionProvider { private readonly bool _isClient; private bool _disposed = false; - private IPEndPoint _remoteEndPoint; - private IPEndPoint _localEndPoint; + private IPEndPoint? _remoteEndPoint; + private IPEndPoint? _localEndPoint; private object _syncObject = new object(); - private Socket _socket = null; - private IPEndPoint _peerListenEndPoint = null; - private TcpListener _inboundListener = null; + private Socket? _socket = null; + private IPEndPoint? _peerListenEndPoint = null; + private TcpListener? _inboundListener = null; private long _nextOutboundBidirectionalStream; private long _nextOutboundUnidirectionalStream; // Constructor for outbound connections - internal MockConnection(IPEndPoint remoteEndPoint, SslClientAuthenticationOptions sslClientAuthenticationOptions, IPEndPoint localEndPoint = null) + internal MockConnection(IPEndPoint? remoteEndPoint, SslClientAuthenticationOptions? sslClientAuthenticationOptions, IPEndPoint? localEndPoint = null) { _remoteEndPoint = remoteEndPoint; _localEndPoint = localEndPoint; @@ -43,8 +44,8 @@ internal MockConnection(Socket socket, IPEndPoint peerListenEndPoint, TcpListene _socket = socket; _peerListenEndPoint = peerListenEndPoint; _inboundListener = inboundListener; - _localEndPoint = (IPEndPoint)socket.LocalEndPoint; - _remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint; + _localEndPoint = (IPEndPoint?)socket.LocalEndPoint; + _remoteEndPoint = (IPEndPoint?)socket.RemoteEndPoint; } internal override bool Connected @@ -57,9 +58,9 @@ internal override bool Connected } } - internal override IPEndPoint LocalEndPoint => new IPEndPoint(_localEndPoint.Address, _localEndPoint.Port); + internal override IPEndPoint LocalEndPoint => new IPEndPoint(_localEndPoint!.Address, _localEndPoint.Port); - internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint.Address, _remoteEndPoint.Port); + internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint!.Address, _remoteEndPoint.Port); internal override SslApplicationProtocol NegotiatedApplicationProtocol => throw new NotImplementedException(); @@ -73,14 +74,14 @@ internal override async ValueTask ConnectAsync(CancellationToken cancellationTok throw new InvalidOperationException("Already connected"); } - Socket socket = new Socket(_remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); + Socket socket = new Socket(_remoteEndPoint!.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await socket.ConnectAsync(_remoteEndPoint).ConfigureAwait(false); socket.NoDelay = true; - _localEndPoint = (IPEndPoint)socket.LocalEndPoint; + _localEndPoint = (IPEndPoint?)socket.LocalEndPoint; // Listen on a new local endpoint for inbound streams - TcpListener inboundListener = new TcpListener(_localEndPoint.Address, 0); + TcpListener inboundListener = new TcpListener(_localEndPoint!.Address, 0); inboundListener.Start(); int inboundListenPort = ((IPEndPoint)inboundListener.LocalEndpoint).Port; @@ -97,7 +98,7 @@ internal override async ValueTask ConnectAsync(CancellationToken cancellationTok } while (bytesRead != buffer.Length); int peerListenPort = BinaryPrimitives.ReadInt32LittleEndian(buffer); - IPEndPoint peerListenEndPoint = new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint).Address, peerListenPort); + IPEndPoint peerListenEndPoint = new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint!).Address, peerListenPort); _socket = socket; _peerListenEndPoint = peerListenEndPoint; @@ -141,7 +142,7 @@ internal override long GetRemoteAvailableBidirectionalStreamCount() internal async Task CreateOutboundMockStreamAsync(long streamId) { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - await socket.ConnectAsync(_peerListenEndPoint).ConfigureAwait(false); + await socket.ConnectAsync(_peerListenEndPoint!).ConfigureAwait(false); socket.NoDelay = true; // Write stream ID to socket so server can read it @@ -156,7 +157,7 @@ internal override async ValueTask AcceptStreamAsync(Cancella { CheckDisposed(); - Socket socket = await _inboundListener.AcceptSocketAsync().ConfigureAwait(false); + Socket socket = await _inboundListener!.AcceptSocketAsync().ConfigureAwait(false); // Read first bytes to get stream ID byte[] buffer = new byte[8]; diff --git a/src/Shared/runtime/Quic/Implementations/Mock/MockImplementationProvider.cs b/src/Shared/runtime/Quic/Implementations/Mock/MockImplementationProvider.cs index b70a11328483..56a5a1167963 100644 --- a/src/Shared/runtime/Quic/Implementations/Mock/MockImplementationProvider.cs +++ b/src/Shared/runtime/Quic/Implementations/Mock/MockImplementationProvider.cs @@ -10,7 +10,7 @@ internal sealed class MockImplementationProvider : QuicImplementationProvider { internal override QuicListenerProvider CreateListener(QuicListenerOptions options) { - return new MockListener(options.ListenEndPoint, options.ServerAuthenticationOptions); + return new MockListener(options.ListenEndPoint!, options.ServerAuthenticationOptions); } internal override QuicConnectionProvider CreateConnection(QuicClientConnectionOptions options) diff --git a/src/Shared/runtime/Quic/Implementations/Mock/MockListener.cs b/src/Shared/runtime/Quic/Implementations/Mock/MockListener.cs index 911f1896b169..88297bdfdd51 100644 --- a/src/Shared/runtime/Quic/Implementations/Mock/MockListener.cs +++ b/src/Shared/runtime/Quic/Implementations/Mock/MockListener.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Sockets; using System.Net.Security; using System.Threading.Tasks; @@ -13,11 +14,11 @@ namespace System.Net.Quic.Implementations.Mock internal sealed class MockListener : QuicListenerProvider { private bool _disposed = false; - private SslServerAuthenticationOptions _sslOptions; + private SslServerAuthenticationOptions? _sslOptions; private IPEndPoint _listenEndPoint; - private TcpListener _tcpListener = null; + private TcpListener _tcpListener; - internal MockListener(IPEndPoint listenEndPoint, SslServerAuthenticationOptions sslServerAuthenticationOptions) + internal MockListener(IPEndPoint listenEndPoint, SslServerAuthenticationOptions? sslServerAuthenticationOptions) { if (listenEndPoint == null) { @@ -49,7 +50,7 @@ internal override async ValueTask AcceptConnectionAsync( } while (bytesRead != buffer.Length); int peerListenPort = BinaryPrimitives.ReadInt32LittleEndian(buffer); - IPEndPoint peerListenEndPoint = new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint).Address, peerListenPort); + IPEndPoint peerListenEndPoint = new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint!).Address, peerListenPort); // Listen on a new local endpoint for inbound streams TcpListener inboundListener = new TcpListener(_listenEndPoint.Address, 0); @@ -96,7 +97,7 @@ private void Dispose(bool disposing) if (disposing) { _tcpListener?.Stop(); - _tcpListener = null; + _tcpListener = null!; } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. diff --git a/src/Shared/runtime/Quic/Implementations/Mock/MockStream.cs b/src/Shared/runtime/Quic/Implementations/Mock/MockStream.cs index 187ba680e17d..f367d981bd10 100644 --- a/src/Shared/runtime/Quic/Implementations/Mock/MockStream.cs +++ b/src/Shared/runtime/Quic/Implementations/Mock/MockStream.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Diagnostics; using System.Net.Sockets; @@ -17,9 +18,9 @@ internal sealed class MockStream : QuicStreamProvider private bool _canRead; private bool _canWrite; - private MockConnection _connection; + private MockConnection? _connection; - private Socket _socket = null; + private Socket? _socket = null; // Constructor for outbound streams internal MockStream(MockConnection connection, long streamId, bool bidirectional) @@ -69,7 +70,7 @@ internal override int Read(Span buffer) throw new NotSupportedException(); } - return _socket.Receive(buffer); + return _socket!.Receive(buffer); } internal override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) @@ -86,7 +87,7 @@ internal override async ValueTask ReadAsync(Memory buffer, Cancellati await ConnectAsync(cancellationToken).ConfigureAwait(false); } - return await _socket.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); + return await _socket!.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); } internal override bool CanWrite => _canWrite; @@ -100,7 +101,7 @@ internal override void Write(ReadOnlySpan buffer) throw new NotSupportedException(); } - _socket.Send(buffer); + _socket!.Send(buffer); } internal override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) @@ -121,11 +122,11 @@ internal override async ValueTask WriteAsync(ReadOnlyMemory buffer, bool e { await ConnectAsync(cancellationToken).ConfigureAwait(false); } - await _socket.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); + await _socket!.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); if (endStream) { - _socket.Shutdown(SocketShutdown.Send); + _socket!.Shutdown(SocketShutdown.Send); } } @@ -149,12 +150,12 @@ internal override async ValueTask WriteAsync(ReadOnlySequence buffers, boo foreach (ReadOnlyMemory buffer in buffers) { - await _socket.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); + await _socket!.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); } if (endStream) { - _socket.Shutdown(SocketShutdown.Send); + _socket!.Shutdown(SocketShutdown.Send); } } @@ -178,12 +179,12 @@ internal override async ValueTask WriteAsync(ReadOnlyMemory foreach (ReadOnlyMemory buffer in buffers.ToArray()) { - await _socket.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); + await _socket!.SendAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false); } if (endStream) { - _socket.Shutdown(SocketShutdown.Send); + _socket!.Shutdown(SocketShutdown.Send); } } @@ -221,7 +222,7 @@ internal override void Shutdown() { CheckDisposed(); - _socket.Shutdown(SocketShutdown.Send); + _socket!.Shutdown(SocketShutdown.Send); } private void CheckDisposed() diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs index 977af1da9387..0b9893333d71 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.IO; using System.Net.Security; using System.Runtime.InteropServices; @@ -127,7 +128,7 @@ private unsafe MsQuicApi() _registrationContext = ctx; } - internal static MsQuicApi Api { get; } + internal static MsQuicApi Api { get; } = null!; internal static bool IsQuicSupported { get; } @@ -221,10 +222,10 @@ internal unsafe uint UnsafeGetParam( buf); } - public async ValueTask CreateSecurityConfig(X509Certificate certificate, string certFilePath, string privateKeyFilePath) + public async ValueTask CreateSecurityConfig(X509Certificate certificate, string? certFilePath, string? privateKeyFilePath) { - MsQuicSecurityConfig secConfig = null; - var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + MsQuicSecurityConfig? secConfig = null; + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); uint secConfigCreateStatus = MsQuicStatusCodes.InternalError; uint createConfigStatus; IntPtr unmanagedAddr = IntPtr.Zero; diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicSession.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicSession.cs index 89dd99f73c3e..0f19506b81a2 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicSession.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicSession.cs @@ -22,7 +22,7 @@ public IntPtr ConnectionOpen(QuicClientConnectionOptions options) { if (!_opened) { - OpenSession(options.ClientAuthenticationOptions.ApplicationProtocols[0].Protocol.ToArray(), + OpenSession(options.ClientAuthenticationOptions!.ApplicationProtocols![0].Protocol.ToArray(), (ushort)options.MaxBidirectionalStreams, (ushort)options.MaxUnidirectionalStreams); } @@ -50,7 +50,7 @@ public IntPtr ListenerOpen(QuicListenerOptions options) { if (!_opened) { - OpenSession(options.ServerAuthenticationOptions.ApplicationProtocols[0].Protocol.ToArray(), + OpenSession(options.ServerAuthenticationOptions!.ApplicationProtocols![0].Protocol.ToArray(), (ushort)options.MaxBidirectionalStreams, (ushort)options.MaxUnidirectionalStreams); } diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/QuicExceptionHelpers.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/QuicExceptionHelpers.cs index 1b8ab8ef2606..bca44c017d70 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/QuicExceptionHelpers.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/QuicExceptionHelpers.cs @@ -2,11 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable namespace System.Net.Quic.Implementations.MsQuic.Internal { internal static class QuicExceptionHelpers { - internal static void ThrowIfFailed(uint status, string message = null, Exception innerException = null) + internal static void ThrowIfFailed(uint status, string? message = null, Exception? innerException = null) { if (!MsQuicStatusHelper.SuccessfulStatusCode(status)) { diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/ResettableCompletionSource.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/ResettableCompletionSource.cs index 4bcf0a917a45..4164663df942 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/ResettableCompletionSource.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/Internal/ResettableCompletionSource.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Threading.Tasks; using System.Threading.Tasks.Sources; @@ -35,7 +36,7 @@ public ValueTaskSourceStatus GetStatus(short token) return _valueTaskSource.GetStatus(token); } - public void OnCompleted(Action continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) + public void OnCompleted(Action continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) { _valueTaskSource.OnCompleted(continuation, state, token, flags); } diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs index 1d914c2668e7..0fdecc68af8d 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.IO; using System.Net.Quic.Implementations.MsQuic.Internal; using System.Net.Security; @@ -16,7 +17,7 @@ namespace System.Net.Quic.Implementations.MsQuic { internal sealed class MsQuicConnection : QuicConnectionProvider { - private MsQuicSession _session; + private MsQuicSession? _session; // Pointer to the underlying connection // TODO replace all IntPtr with SafeHandles @@ -27,10 +28,10 @@ internal sealed class MsQuicConnection : QuicConnectionProvider // Delegate that wraps the static function that will be called when receiving an event. // TODO investigate if the delegate can be static instead. - private ConnectionCallbackDelegate _connectionDelegate; + private ConnectionCallbackDelegate? _connectionDelegate; // Endpoint to either connect to or the endpoint already accepted. - private IPEndPoint _localEndPoint; + private IPEndPoint? _localEndPoint; private readonly IPEndPoint _remoteEndPoint; private readonly ResettableCompletionSource _connectTcs = new ResettableCompletionSource(); @@ -38,7 +39,7 @@ internal sealed class MsQuicConnection : QuicConnectionProvider private bool _disposed; private bool _connected; - private MsQuicSecurityConfig _securityConfig; + private MsQuicSecurityConfig? _securityConfig; private long _abortErrorCode = -1; // Queue for accepted streams @@ -70,7 +71,7 @@ public MsQuicConnection(QuicClientConnectionOptions options) // Creating a session per connection isn't ideal. _session = new MsQuicSession(); _ptr = _session.ConnectionOpen(options); - _remoteEndPoint = options.RemoteEndPoint; + _remoteEndPoint = options.RemoteEndPoint!; SetCallbackHandler(); SetIdleTimeout(options.IdleTimeout); @@ -82,15 +83,15 @@ internal override IPEndPoint LocalEndPoint { get { - return new IPEndPoint(_localEndPoint.Address, _localEndPoint.Port); + return new IPEndPoint(_localEndPoint!.Address, _localEndPoint.Port); } } - internal async ValueTask SetSecurityConfigForConnection(X509Certificate cert, string certFilePath, string privateKeyFilePath) + internal async ValueTask SetSecurityConfigForConnection(X509Certificate cert, string? certFilePath, string? privateKeyFilePath) { _securityConfig = await MsQuicApi.Api.CreateSecurityConfig(cert, certFilePath, privateKeyFilePath); // TODO this isn't being set correctly - MsQuicParameterHelpers.SetSecurityConfig(MsQuicApi.Api, _ptr, (uint)QUIC_PARAM_LEVEL.CONNECTION, (uint)QUIC_PARAM_CONN.SEC_CONFIG, _securityConfig.NativeObjPtr); + MsQuicParameterHelpers.SetSecurityConfig(MsQuicApi.Api, _ptr, (uint)QUIC_PARAM_LEVEL.CONNECTION, (uint)QUIC_PARAM_CONN.SEC_CONFIG, _securityConfig!.NativeObjPtr); } internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint.Address, _remoteEndPoint.Port); @@ -355,7 +356,7 @@ internal static uint NativeCallbackHandler( ref ConnectionEvent connectionEventStruct) { GCHandle handle = GCHandle.FromIntPtr(context); - MsQuicConnection quicConnection = (MsQuicConnection)handle.Target; + MsQuicConnection quicConnection = (MsQuicConnection)handle.Target!; return quicConnection.HandleEvent(ref connectionEventStruct); } diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs index 14323d963f3e..e87126b87f0e 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Quic.Implementations.MsQuic.Internal; using System.Net.Security; using System.Runtime.InteropServices; @@ -25,7 +26,7 @@ internal sealed class MsQuicListener : QuicListenerProvider, IDisposable private GCHandle _handle; // Delegate that wraps the static function that will be called when receiving an event. - private ListenerCallbackDelegate _listenerDelegate; + private ListenerCallbackDelegate? _listenerDelegate; // Ssl listening options (ALPN, cert, etc) private SslServerAuthenticationOptions _sslOptions; @@ -46,8 +47,8 @@ internal MsQuicListener(QuicListenerOptions options) }); _options = options; - _sslOptions = options.ServerAuthenticationOptions; - _listenEndPoint = options.ListenEndPoint; + _sslOptions = options.ServerAuthenticationOptions!; + _listenEndPoint = options.ListenEndPoint!; _ptr = _session.ListenerOpen(options); } @@ -77,7 +78,7 @@ internal override async ValueTask AcceptConnectionAsync( throw new QuicOperationAbortedException(); } - await connection.SetSecurityConfigForConnection(_sslOptions.ServerCertificate, + await connection.SetSecurityConfigForConnection(_sslOptions.ServerCertificate!, _options.CertificateFilePath, _options.PrivateKeyFilePath); @@ -187,7 +188,7 @@ internal static uint NativeCallbackHandler( ref ListenerEvent connectionEventStruct) { GCHandle handle = GCHandle.FromIntPtr(context); - MsQuicListener quicListener = (MsQuicListener)handle.Target; + MsQuicListener quicListener = (MsQuicListener)handle.Target!; return quicListener.ListenerCallbackHandler(ref connectionEventStruct); } diff --git a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicStream.cs b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicStream.cs index 00ca779e3ce7..b8b6282a25c1 100644 --- a/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicStream.cs +++ b/src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicStream.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Collections.Generic; using System.Diagnostics; @@ -23,7 +24,7 @@ internal sealed class MsQuicStream : QuicStreamProvider private GCHandle _handle; // Delegate that wraps the static function that will be called when receiving an event. - private StreamCallbackDelegate _callback; + private StreamCallbackDelegate? _callback; // Backing for StreamId private long _streamId = -1; @@ -424,7 +425,7 @@ internal override Task FlushAsync(CancellationToken cancellationToken = default) { ThrowIfDisposed(); - return default; + return default!; } public override ValueTask DisposeAsync() @@ -500,7 +501,7 @@ internal static uint NativeCallbackHandler( ref StreamEvent streamEvent) { var handle = GCHandle.FromIntPtr(context); - var quicStream = (MsQuicStream)handle.Target; + var quicStream = (MsQuicStream)handle.Target!; return quicStream.HandleEvent(ref streamEvent); } diff --git a/src/Shared/runtime/Quic/Interop/MsQuicNativeMethods.cs b/src/Shared/runtime/Quic/Interop/MsQuicNativeMethods.cs index 6f7edba17314..31ab416fbd39 100644 --- a/src/Shared/runtime/Quic/Interop/MsQuicNativeMethods.cs +++ b/src/Shared/runtime/Quic/Interop/MsQuicNativeMethods.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; @@ -90,7 +91,7 @@ internal delegate uint SecConfigCreateDelegate( IntPtr registrationContext, uint flags, IntPtr certificate, - [MarshalAs(UnmanagedType.LPStr)]string principal, + [MarshalAs(UnmanagedType.LPStr)]string? principal, IntPtr context, SecConfigCreateCompleteDelegate completionHandler); diff --git a/src/Shared/runtime/Quic/QuicClientConnectionOptions.cs b/src/Shared/runtime/Quic/QuicClientConnectionOptions.cs index a9a9b0ec40c4..3e7d10a199a5 100644 --- a/src/Shared/runtime/Quic/QuicClientConnectionOptions.cs +++ b/src/Shared/runtime/Quic/QuicClientConnectionOptions.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Security; namespace System.Net.Quic @@ -14,17 +15,17 @@ internal class QuicClientConnectionOptions /// /// Client authentication options to use when establishing a . /// - public SslClientAuthenticationOptions ClientAuthenticationOptions { get; set; } + public SslClientAuthenticationOptions? ClientAuthenticationOptions { get; set; } /// /// The local endpoint that will be bound to. /// - public IPEndPoint LocalEndPoint { get; set; } + public IPEndPoint? LocalEndPoint { get; set; } /// /// The endpoint to connect to. /// - public IPEndPoint RemoteEndPoint { get; set; } + public IPEndPoint? RemoteEndPoint { get; set; } /// /// Limit on the number of bidirectional streams the peer connection can create diff --git a/src/Shared/runtime/Quic/QuicConnection.cs b/src/Shared/runtime/Quic/QuicConnection.cs index 877421a94f01..c2bbceb43cdd 100644 --- a/src/Shared/runtime/Quic/QuicConnection.cs +++ b/src/Shared/runtime/Quic/QuicConnection.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Quic.Implementations; using System.Net.Quic.Implementations.MsQuic.Internal; using System.Net.Security; @@ -22,13 +23,13 @@ internal sealed class QuicConnection : IDisposable /// The remote endpoint to connect to. /// TLS options /// The local endpoint to connect from. - public QuicConnection(IPEndPoint remoteEndPoint, SslClientAuthenticationOptions sslClientAuthenticationOptions, IPEndPoint localEndPoint = null) + public QuicConnection(IPEndPoint remoteEndPoint, SslClientAuthenticationOptions? sslClientAuthenticationOptions, IPEndPoint? localEndPoint = null) : this(QuicImplementationProviders.Default, remoteEndPoint, sslClientAuthenticationOptions, localEndPoint) { } // !!! TEMPORARY: Remove "implementationProvider" before shipping - public QuicConnection(QuicImplementationProvider implementationProvider, IPEndPoint remoteEndPoint, SslClientAuthenticationOptions sslClientAuthenticationOptions, IPEndPoint localEndPoint = null) + public QuicConnection(QuicImplementationProvider implementationProvider, IPEndPoint remoteEndPoint, SslClientAuthenticationOptions? sslClientAuthenticationOptions, IPEndPoint? localEndPoint = null) : this(implementationProvider, new QuicClientConnectionOptions() { RemoteEndPoint = remoteEndPoint, ClientAuthenticationOptions = sslClientAuthenticationOptions, LocalEndPoint = localEndPoint }) { } diff --git a/src/Shared/runtime/Quic/QuicListenerOptions.cs b/src/Shared/runtime/Quic/QuicListenerOptions.cs index f9eae30c3d5a..934c7a8e244e 100644 --- a/src/Shared/runtime/Quic/QuicListenerOptions.cs +++ b/src/Shared/runtime/Quic/QuicListenerOptions.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Net.Security; namespace System.Net.Quic @@ -14,22 +15,22 @@ internal class QuicListenerOptions /// /// Server Ssl options to use for ALPN, SNI, etc. /// - public SslServerAuthenticationOptions ServerAuthenticationOptions { get; set; } + public SslServerAuthenticationOptions? ServerAuthenticationOptions { get; set; } /// /// Optional path to certificate file to configure the security configuration. /// - public string CertificateFilePath { get; set; } + public string? CertificateFilePath { get; set; } /// /// Optional path to private key file to configure the security configuration. /// - public string PrivateKeyFilePath { get; set; } + public string? PrivateKeyFilePath { get; set; } /// /// The endpoint to listen on. /// - public IPEndPoint ListenEndPoint { get; set; } + public IPEndPoint? ListenEndPoint { get; set; } /// /// Number of connections to be held without accepting the connection. diff --git a/src/Shared/runtime/Quic/QuicStream.cs b/src/Shared/runtime/Quic/QuicStream.cs index 6e52bb07530d..4e1af6dff60b 100644 --- a/src/Shared/runtime/Quic/QuicStream.cs +++ b/src/Shared/runtime/Quic/QuicStream.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.IO; using System.Net.Quic.Implementations; @@ -29,13 +30,13 @@ internal QuicStream(QuicStreamProvider provider) public override void SetLength(long value) => throw new NotSupportedException(); public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } - public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => + public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => TaskToApm.Begin(ReadAsync(buffer, offset, count, default), callback, state); public override int EndRead(IAsyncResult asyncResult) => TaskToApm.End(asyncResult); - public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) => + public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => TaskToApm.Begin(WriteAsync(buffer, offset, count, default), callback, state); public override void EndWrite(IAsyncResult asyncResult) =>