Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -144,7 +142,7 @@ public static AutomaticProgressReporter Create(
cancellationToken);
}

private void OnTimer(object state)
private void OnTimer(object? state)
{
try
{
Expand Down
14 changes: 6 additions & 8 deletions src/NuGet.Core/NuGet.Protocol/Plugins/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,12 +28,12 @@ public sealed class Connection : IConnection
/// <summary>
/// Occurs when an unrecoverable fault has been caught.
/// </summary>
public event EventHandler<ProtocolErrorEventArgs> Faulted;
public event EventHandler<ProtocolErrorEventArgs>? Faulted;

/// <summary>
/// Occurs when a message has been received.
/// </summary>
public event EventHandler<MessageEventArgs> MessageReceived;
public event EventHandler<MessageEventArgs>? MessageReceived;

/// <summary>
/// Gets the message dispatcher.
Expand All @@ -50,7 +48,7 @@ public sealed class Connection : IConnection
/// <summary>
/// Gets the negotiated protocol version, or <see langword="null" /> if not yet connected.
/// </summary>
public SemanticVersion ProtocolVersion { get; private set; }
public SemanticVersion? ProtocolVersion { get; private set; }

/// <summary>
/// Instantiates a new instance of the <see cref="Connection" /> class.
Expand Down Expand Up @@ -271,7 +269,7 @@ public async Task SendAsync(Message message, CancellationToken cancellationToken
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
/// <exception cref="InvalidOperationException">Thrown if not connected.</exception>
public Task<TInbound> SendRequestAndReceiveResponseAsync<TOutbound, TInbound>(
public Task<TInbound?> SendRequestAndReceiveResponseAsync<TOutbound, TInbound>(
MessageMethod method,
TOutbound payload,
CancellationToken cancellationToken)
Expand All @@ -294,7 +292,7 @@ public Task<TInbound> SendRequestAndReceiveResponseAsync<TOutbound, TInbound>(
return MessageDispatcher.DispatchRequestAsync<TOutbound, TInbound>(method, payload, cancellationToken);
}

private void OnMessageReceived(object sender, MessageEventArgs e)
private void OnMessageReceived(object? sender, MessageEventArgs e)
{
if (_logger.IsEnabled)
{
Expand All @@ -304,7 +302,7 @@ private void OnMessageReceived(object sender, MessageEventArgs e)
MessageReceived?.Invoke(this, e);
}

private void OnFaulted(object sender, ProtocolErrorEventArgs e)
private void OnFaulted(object? sender, ProtocolErrorEventArgs e)
{
Faulted?.Invoke(this, e);
}
Expand Down
6 changes: 3 additions & 3 deletions src/NuGet.Core/NuGet.Protocol/Plugins/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public interface IConnection : IDisposable
/// <summary>
/// Occurs when an unrecoverable fault has been caught.
/// </summary>
event EventHandler<ProtocolErrorEventArgs> Faulted;
event EventHandler<ProtocolErrorEventArgs>? Faulted;

/// <summary>
/// Occurs when a message has been received.
/// </summary>
event EventHandler<MessageEventArgs> MessageReceived;
event EventHandler<MessageEventArgs>? MessageReceived;

/// <summary>
/// Gets the message dispatcher.
Expand Down Expand Up @@ -70,7 +70,7 @@ public interface IConnection : IDisposable
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
/// <exception cref="InvalidOperationException">Thrown if not connected.</exception>
Task<TInbound> SendRequestAndReceiveResponseAsync<TOutbound, TInbound>(
Task<TInbound?> SendRequestAndReceiveResponseAsync<TOutbound, TInbound>(
MessageMethod method,
TOutbound payload,
CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Message CreateMessage<TPayload>(MessageType type, MessageMethod method, TPayload
/// <returns>A task that represents the asynchronous operation.
/// The task result (<see cref="Task{TResult}.Result" />) returns a <typeparamref name="TInbound" />
/// from the target.</returns>
Task<TInbound> DispatchRequestAsync<TOutbound, TInbound>(
Task<TInbound?> DispatchRequestAsync<TOutbound, TInbound>(
MessageMethod method,
TOutbound payload,
CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/Plugins/IPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public interface IPlugin : IDisposable
/// <summary>
/// Occurs before the plugin closes.
/// </summary>
event EventHandler BeforeClose;
event EventHandler? BeforeClose;

/// <summary>
/// Occurs when the plugin has closed.
/// </summary>
event EventHandler Closed;
event EventHandler? Closed;

/// <summary>
/// Gets the connection for the plugin.
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Protocol/Plugins/IPluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ Task<IEnumerable<PluginCreationResult>> CreatePluginsAsync(
/// <param name="pluginDiscoveryResult"></param>
/// <param name="cancellationToken"></param>
/// <returns>A PluginCreationResult</returns>
Task<Tuple<bool, PluginCreationResult>> TryGetSourceAgnosticPluginAsync(PluginDiscoveryResult pluginDiscoveryResult, OperationClaim requestedOperationClaim, CancellationToken cancellationToken);
Task<Tuple<bool, PluginCreationResult?>> TryGetSourceAgnosticPluginAsync(PluginDiscoveryResult pluginDiscoveryResult, OperationClaim requestedOperationClaim, CancellationToken cancellationToken);
}
}
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/Plugins/IReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public interface IReceiver : IDisposable
/// <summary>
/// Occurs when an unrecoverable fault has been caught.
/// </summary>
event EventHandler<ProtocolErrorEventArgs> Faulted;
event EventHandler<ProtocolErrorEventArgs>? Faulted;

/// <summary>
/// Occurs when a message has been received.
/// </summary>
event EventHandler<MessageEventArgs> MessageReceived;
event EventHandler<MessageEventArgs>? MessageReceived;

/// <summary>
/// Closes the connection.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -48,14 +46,14 @@ static JsonSerializationUtilities()
/// </summary>
/// <typeparam name="T">The deserialization type.</typeparam>
/// <param name="json">JSON to deserialize.</param>
/// <returns>An instance of <typeparamref name="T" />.</returns>
/// <returns>An instance of <typeparamref name="T" />, or <see langword="null" /> if the JSON represents a null value.</returns>
/// <exception cref="ArgumentException">Thrown if <paramref name="json" />
/// is either <see langword="null" /> or an empty string.</exception>
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("Uses Newtonsoft.Json reflection-based deserialization.")]
[RequiresDynamicCode("Uses Newtonsoft.Json reflection-based deserialization.")]
#endif
public static T Deserialize<T>(string json)
public static T? Deserialize<T>(string json)
where T : class
{
if (string.IsNullOrEmpty(json))
Expand Down Expand Up @@ -115,13 +113,13 @@ public static void Serialize(JsonWriter writer, object value)
/// </summary>
/// <typeparam name="T">The deserialization type.</typeparam>
/// <param name="jObject">A JSON object.</param>
/// <returns>An instance of <typeparamref name="T" />.</returns>
/// <returns>An instance of <typeparamref name="T" />, or <see langword="null" /> if the JSON represents a null value.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="jObject" /> is <see langword="null" />.</exception>
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("Uses Newtonsoft.Json reflection-based deserialization.")]
[RequiresDynamicCode("Uses Newtonsoft.Json reflection-based deserialization.")]
#endif
public static T ToObject<T>(JObject jObject)
public static T? ToObject<T>(JObject jObject)
{
if (jObject == null)
{
Expand Down
52 changes: 18 additions & 34 deletions src/NuGet.Core/NuGet.Protocol/Plugins/MessageDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Collections.Concurrent;
#if NET5_0_OR_GREATER
Expand All @@ -20,7 +18,7 @@ namespace NuGet.Protocol.Plugins
/// </summary>
public sealed class MessageDispatcher : IMessageDispatcher, IResponseHandler
{
private IConnection _connection;
private IConnection? _connection;
private readonly IIdGenerator _idGenerator;
private bool _isClosed;
private bool _isDisposed;
Expand Down Expand Up @@ -279,7 +277,7 @@ public Task DispatchProgressAsync(Message request, Progress progress, Cancellati
/// from the target.</returns>
/// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
/// is cancelled.</exception>
public Task<TInbound> DispatchRequestAsync<TOutbound, TInbound>(
public Task<TInbound?> DispatchRequestAsync<TOutbound, TInbound>(
MessageMethod method,
TOutbound payload,
CancellationToken cancellationToken)
Expand Down Expand Up @@ -349,7 +347,7 @@ public Task DispatchResponseAsync<TOutbound>(
/// Sets the connection to be used for dispatching messages.
/// </summary>
/// <param name="connection">A connection instance. Can be <see langword="null" />.</param>
public void SetConnection(IConnection connection)
public void SetConnection(IConnection? connection)
{
if (_connection == connection)
{
Expand Down Expand Up @@ -385,9 +383,7 @@ private async Task DispatchAsync<TOutgoing>(
CancellationToken cancellationToken)
where TOutgoing : class
{
InboundRequestContext requestContext;

if (!_inboundRequestContexts.TryGetValue(request.RequestId, out requestContext))
if (!_inboundRequestContexts.TryGetValue(request.RequestId, out _))
{
return;
}
Expand Down Expand Up @@ -459,7 +455,7 @@ private async Task DispatchWithExistingContextAsync(
await connection.SendAsync(response, cancellationToken);
}

private async Task<TIncoming> DispatchWithNewContextAsync<TOutgoing, TIncoming>(
private async Task<TIncoming?> DispatchWithNewContextAsync<TOutgoing, TIncoming>(
IConnection connection,
MessageType type,
MessageMethod method,
Expand Down Expand Up @@ -525,7 +521,7 @@ private async Task<TIncoming> DispatchWithNewContextAsync<TOutgoing, TIncoming>(
return null;
}

private void OnMessageReceived(object sender, MessageEventArgs e)
private void OnMessageReceived(object? sender, MessageEventArgs e)
{
// Capture _connection as SetConnection(...) could null it out later.
var connection = _connection;
Expand All @@ -535,9 +531,7 @@ private void OnMessageReceived(object sender, MessageEventArgs e)
return;
}

OutboundRequestContext requestContext;

if (_outboundRequestContexts.TryGetValue(e.Message.RequestId, out requestContext))
if (_outboundRequestContexts.TryGetValue(e.Message.RequestId, out var requestContext))
{
switch (e.Message.Type)
{
Expand Down Expand Up @@ -593,9 +587,7 @@ private void OnMessageReceived(object sender, MessageEventArgs e)

private void HandleInboundCancel(Message message)
{
InboundRequestContext requestContext;

if (_inboundRequestContexts.TryGetValue(message.RequestId, out requestContext))
if (_inboundRequestContexts.TryGetValue(message.RequestId, out var requestContext))
{
requestContext.Cancel();
}
Expand All @@ -614,14 +606,14 @@ private void HandleInboundFault(Message fault)

var payload = MessageUtilities.DeserializePayload<Fault>(fault);

throw new ProtocolException(payload.Message);
throw new ProtocolException(payload?.Message);
}

private void HandleInboundRequest(Message message)
{
var cancellationToken = CancellationToken.None;
IRequestHandler requestHandler = null;
ProtocolException exception = null;
IRequestHandler? requestHandler = null;
ProtocolException? exception = null;

try
{
Expand All @@ -643,15 +635,13 @@ private void HandleInboundRequest(Message message)
}
else
{
requestContext.BeginFaultAsync(message, exception);
requestContext.BeginFaultAsync(message, exception!);
}
}

private IRequestHandler GetInboundRequestHandler(MessageMethod method)
{
IRequestHandler handler;

if (!RequestHandlers.TryGet(method, out handler))
if (!RequestHandlers.TryGet(method, out var handler))
{
throw new ProtocolException(
string.Format(CultureInfo.CurrentCulture, Strings.Plugin_RequestHandlerDoesNotExist, method));
Expand All @@ -662,9 +652,7 @@ private IRequestHandler GetInboundRequestHandler(MessageMethod method)

private OutboundRequestContext GetOutboundRequestContext(string requestId)
{
OutboundRequestContext requestContext;

if (!_outboundRequestContexts.TryGetValue(requestId, out requestContext))
if (!_outboundRequestContexts.TryGetValue(requestId, out var requestContext))
{
throw new ProtocolException(
string.Format(CultureInfo.CurrentCulture, Strings.Plugin_RequestContextDoesNotExist, requestId));
Expand All @@ -675,19 +663,15 @@ private OutboundRequestContext GetOutboundRequestContext(string requestId)

private void RemoveInboundRequestContext(string requestId)
{
InboundRequestContext requestContext;

if (_inboundRequestContexts.TryRemove(requestId, out requestContext))
if (_inboundRequestContexts.TryRemove(requestId, out var requestContext))
{
requestContext.Dispose();
}
}

private void RemoveOutboundRequestContext(string requestId)
{
OutboundRequestContext requestContext;

if (_outboundRequestContexts.TryRemove(requestId, out requestContext))
if (_outboundRequestContexts.TryRemove(requestId, out var requestContext))
{
requestContext.Dispose();
}
Expand All @@ -698,7 +682,7 @@ private InboundRequestContext CreateInboundRequestContext(
CancellationToken cancellationToken)
{
return new InboundRequestContext(
_connection,
_connection!,
Comment thread
nkolev92 marked this conversation as resolved.
message.RequestId,
cancellationToken,
_inboundRequestProcessingContext,
Expand All @@ -713,7 +697,7 @@ private OutboundRequestContext<TIncoming> CreateOutboundRequestContext<TIncoming
where TIncoming : class
{
return new OutboundRequestContext<TIncoming>(
_connection,
_connection!,
Comment thread
nkolev92 marked this conversation as resolved.
message,
timeout,
isKeepAlive,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

using System;
using System.Globalization;
using Newtonsoft.Json;
Expand Down
Loading