Skip to content

Sync shared code from runtime #19918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Shared/runtime/Http2/Hpack/HPackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -129,13 +130,13 @@ public void Decode(in ReadOnlySequence<byte> data, bool endHeaders, IHttpHeaders
CheckIncompleteHeaderBlock(endHeaders);
}

public void Decode(ReadOnlySpan<byte> data, bool endHeaders, IHttpHeadersHandler handler)
public void Decode(ReadOnlySpan<byte> data, bool endHeaders, IHttpHeadersHandler? handler)
{
DecodeInternal(data, endHeaders, handler);
CheckIncompleteHeaderBlock(endHeaders);
}

private void DecodeInternal(ReadOnlySpan<byte> data, bool endHeaders, IHttpHeadersHandler handler)
private void DecodeInternal(ReadOnlySpan<byte> data, bool endHeaders, IHttpHeadersHandler? handler)
{
int intResult;

Expand Down Expand Up @@ -370,7 +371,7 @@ private void CheckIncompleteHeaderBlock(bool endHeaders)
}
}

private void ProcessHeaderValue(IHttpHeadersHandler handler)
private void ProcessHeaderValue(IHttpHeadersHandler? handler)
{
OnString(nextState: State.Ready);

Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/Shared/runtime/Http2/Hpack/HPackEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -370,7 +372,7 @@ public static bool EncodeStringLiteral(string value, Span<byte> destination, out
return false;
}

public static bool EncodeStringLiterals(ReadOnlySpan<string> values, string separator, Span<byte> destination, out int bytesWritten)
public static bool EncodeStringLiterals(ReadOnlySpan<string> values, string? separator, Span<byte> destination, out int bytesWritten)
{
bytesWritten = 0;

Expand All @@ -393,6 +395,7 @@ public static bool EncodeStringLiterals(ReadOnlySpan<string> 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;
Expand Down
11 changes: 6 additions & 5 deletions src/Shared/runtime/Http3/QPack/QPackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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!;
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Shared/runtime/Http3/QPack/QPackEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,7 +11,7 @@ namespace System.Net.Http.QPack
{
internal class QPackEncoder
{
private IEnumerator<KeyValuePair<string, string>> _enumerator;
private IEnumerator<KeyValuePair<string, string>>? _enumerator;

// https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.2
// 0 1 2 3 4 5 6 7
Expand Down Expand Up @@ -194,7 +195,7 @@ private static bool EncodeValueString(string s, Span<byte> buffer, out int lengt
/// <summary>
/// Encodes a value by concatenating a collection of strings, separated by a separator string.
/// </summary>
public static bool EncodeValueString(ReadOnlySpan<string> values, string separator, Span<byte> buffer, out int length)
public static bool EncodeValueString(ReadOnlySpan<string> values, string? separator, Span<byte> buffer, out int length)
{
if (values.Length == 1)
{
Expand All @@ -209,6 +210,7 @@ public static bool EncodeValueString(ReadOnlySpan<string> 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)
{
Expand Down Expand Up @@ -386,7 +388,7 @@ private bool Encode(Span<byte> 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)
{
Expand Down
33 changes: 17 additions & 16 deletions src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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();

Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -141,7 +142,7 @@ internal override long GetRemoteAvailableBidirectionalStreamCount()
internal async Task<Socket> 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
Expand All @@ -156,7 +157,7 @@ internal override async ValueTask<QuicStreamProvider> 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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/Shared/runtime/Quic/Implementations/Mock/MockListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -49,7 +50,7 @@ internal override async ValueTask<QuicConnectionProvider> 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);
Expand Down Expand Up @@ -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.
Expand Down
Loading