Skip to content

Commit

Permalink
Make protocol project nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
vicancy committed Nov 20, 2024
1 parent f317977 commit aaa0582
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Azure.SignalR.Protocols/CloseWithAckMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;
using System.Collections.Generic;
Expand All @@ -22,7 +23,7 @@ public abstract class CloseWithAckMessage : ExtensibleServiceMessage, IAckableMe
/// <summary>
/// Gets or sets the reason for the close.
/// </summary>
public string Reason { get; set; }
public string? Reason { get; set; }

public CloseWithAckMessage(int ackId)
{
Expand All @@ -35,7 +36,7 @@ public abstract class CloseMultiConnectionsWithAckMessage : CloseWithAckMessage
/// <summary>
/// Gets or sets the list of excluded connection Ids.
/// </summary>
public IReadOnlyList<string> ExcludedList { get; set; }
public IReadOnlyList<string>? ExcludedList { get; set; }

public CloseMultiConnectionsWithAckMessage(int ackId) : base(ackId) { }
}
Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.Azure.SignalR.Protocols/ConnectionMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;
using System.Buffers;
Expand Down Expand Up @@ -79,7 +80,7 @@ public OpenConnectionMessage(string connectionId, Claim[] claims, IDictionary<st
/// <summary>
/// Gets or sets the protocol for new connection.
/// </summary>
public string Protocol { get; set; }
public string? Protocol { get; set; }
}

/// <summary>
Expand All @@ -98,7 +99,7 @@ public class CloseConnectionMessage : ConnectionMessage, IMessageWithTracingId
/// <param name="connectionId">The connection Id.</param>
/// <param name="errorMessage">Optional error message.</param>
/// <param name="headers">A <see cref="IDictionary{TKey,TValue}"/> associated with the connection.</param>
public CloseConnectionMessage(string connectionId, string errorMessage, IDictionary<string, StringValues> headers = null) : base(connectionId)
public CloseConnectionMessage(string connectionId, string errorMessage, IDictionary<string, StringValues>? headers = null) : base(connectionId)
{
ErrorMessage = errorMessage ?? "";
Headers = headers ?? new Dictionary<string, StringValues>();
Expand Down Expand Up @@ -239,7 +240,7 @@ public ClientCompletionMessage(string invocationId, string connectionId, string
/// <summary>
/// Gets or sets the connection protocol.
/// </summary>
public string Protocol { get; set; }
public string? Protocol { get; set; }

/// <summary>
/// Gets or sets the binary payload.
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Azure.SignalR.Protocols/GroupMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Azure.SignalR.Protocols/IHasProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
public interface IHasProtocol
{
string Protocol { get; set; }
string? Protocol { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
public interface IHasSubscriberFilter
{
string Filter { get; set; }
string? Filter { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Azure.SignalR.Protocols/IHasTtl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Azure.SignalR.Protocols/IPartializable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol
{
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Azure.SignalR.Protocols/IServiceProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;
using System.Buffers;
Expand All @@ -22,7 +23,7 @@ public interface IServiceProtocol
/// <param name="input">The serialized representation of the message.</param>
/// <param name="message">When this method returns <c>true</c>, contains the parsed message.</param>
/// <returns>A value that is <c>true</c> if the <see cref="ServiceMessage"/> was successfully parsed; otherwise, <c>false</c>.</returns>
bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage message);
bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage? message);

/// <summary>
/// Writes the specified <see cref="ServiceMessage"/> to a writer.
Expand Down
15 changes: 8 additions & 7 deletions src/Microsoft.Azure.SignalR.Protocols/MulticastDataMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -41,7 +42,7 @@ protected MulticastDataMessage(IDictionary<string, ReadOnlyMemory<byte>> payload
/// <summary>
/// Filter out the subscribers to send messages to
/// </summary>
public string Filter { get; set; }
public string? Filter { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -128,7 +129,7 @@ public class BroadcastDataMessage : MulticastDataMessage, IPartitionableMessage
/// <summary>
/// Gets or sets the list of excluded connection Ids.
/// </summary>
public IReadOnlyList<string> ExcludedList { get; set; }
public IReadOnlyList<string>? ExcludedList { get; set; }

public byte PartitionKey => Key;

Expand All @@ -147,7 +148,7 @@ public BroadcastDataMessage(IDictionary<string, ReadOnlyMemory<byte>> payloads,
/// <param name="excludedList">The list of excluded connection Ids.</param>
/// <param name="payloads">The payload dictionary which contains binary payload of multiple protocols.</param>
/// <param name="tracingId">The tracing Id of the message.</param>
public BroadcastDataMessage(IReadOnlyList<string> excludedList, IDictionary<string, ReadOnlyMemory<byte>> payloads, ulong? tracingId = null) : base(payloads, tracingId)
public BroadcastDataMessage(IReadOnlyList<string>? excludedList, IDictionary<string, ReadOnlyMemory<byte>> payloads, ulong? tracingId = null) : base(payloads, tracingId)
{
ExcludedList = excludedList;
}
Expand All @@ -168,17 +169,17 @@ public class GroupBroadcastDataMessage : MulticastDataMessage, IPartitionableMes
/// <summary>
/// Gets or sets the list of excluded connection Ids.
/// </summary>
public IReadOnlyList<string> ExcludedList { get; set; }
public IReadOnlyList<string>? ExcludedList { get; set; }

/// <summary>
/// Gets or sets the list of excluded user Ids.
/// </summary>
public IReadOnlyList<string> ExcludedUserList { get; set; }
public IReadOnlyList<string>? ExcludedUserList { get; set; }

/// <summary>
/// Gets or sets the user ID of the message caller
/// </summary>
public string CallerUserId { get; set; }
public string? CallerUserId { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="GroupBroadcastDataMessage"/> class.
Expand All @@ -198,7 +199,7 @@ public GroupBroadcastDataMessage(string groupName, IDictionary<string, ReadOnlyM
/// <param name="excludedList">The list of excluded connection Ids.</param>
/// <param name="payloads">The payload dictionary which contains binary payload of multiple protocols.</param>
/// <param name="tracingId">The tracing Id of the message.</param>
public GroupBroadcastDataMessage(string groupName, IReadOnlyList<string> excludedList, IDictionary<string, ReadOnlyMemory<byte>> payloads, ulong? tracingId = null)
public GroupBroadcastDataMessage(string groupName, IReadOnlyList<string>? excludedList, IDictionary<string, ReadOnlyMemory<byte>> payloads, ulong? tracingId = null)
: base(payloads, tracingId)
{
GroupName = groupName;
Expand Down
19 changes: 10 additions & 9 deletions src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;

Expand Down Expand Up @@ -68,7 +69,7 @@ public abstract class ServiceMessage
/// Clone should make a copy of everything that may get modified throughout the lifetime of the message
/// The default implementation is a shallow copy as it fits the current needs.
/// </summary>
public virtual ServiceMessage Clone() => MemberwiseClone() as ServiceMessage;
public virtual ServiceMessage Clone() => (MemberwiseClone() as ServiceMessage)!;

public static byte GeneratePartitionKey(string input)
{
Expand Down Expand Up @@ -238,13 +239,13 @@ public class AccessKeyRequestMessage : ExtensibleServiceMessage
/// <summary>
/// Gets or sets the Azure Active Directory token.
/// </summary>
public string Token { get; set; }
public string? Token { get; set; }

/// <summary>
/// Gets or sets the key Id.
/// <c>null</c>
/// </summary>
public string Kid { get; set; }
public string? Kid { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="AccessKeyRequestMessage"/> class.
Expand All @@ -271,22 +272,22 @@ public class AccessKeyResponseMessage : ExtensibleServiceMessage
/// <summary>
/// Gets or sets the key Id.
/// </summary>
public string Kid { get; set; }
public string? Kid { get; set; }

/// <summary>
/// Gets or sets the access key.
/// </summary>
public string AccessKey { get; set; }
public string? AccessKey { get; set; }

/// <summary>
/// Gets or sets error type.
/// </summary>
public string ErrorType { get; set; }
public string? ErrorType { get; set; }

/// <summary>
/// Gets or sets error message.
/// </summary>
public string ErrorMessage { get; set; }
public string? ErrorMessage { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="AccessKeyResponseMessage"/> class.
Expand Down Expand Up @@ -342,7 +343,7 @@ public class HandshakeRequestMessage : ExtensibleServiceMessage
/// <summary>
/// Gets or sets the target of service connection, only work for OnDemand connections.
/// </summary>
public string Target { get; set; }
public string? Target { get; set; }

/// <summary>
/// Gets or sets the migratable flag.
Expand Down Expand Up @@ -397,7 +398,7 @@ public class HandshakeResponseMessage : ExtensibleServiceMessage
/// <summary>
/// Gets or sets the id of this connection.
/// </summary>
public string ConnectionId { get; set; }
public string? ConnectionId { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="HandshakeResponseMessage"/> class.
Expand Down
22 changes: 14 additions & 8 deletions src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Security.Claims;
Expand All @@ -17,13 +19,17 @@ namespace Microsoft.Azure.SignalR.Protocol
/// </summary>
public class ServiceProtocol : IServiceProtocol
{
private static readonly IDictionary<string, ReadOnlyMemory<byte>> EmptyReadOnlyMemoryDictionary = new ReadOnlyDictionary<string, ReadOnlyMemory<byte>>(new Dictionary<string, ReadOnlyMemory<byte>>());

private static readonly IDictionary<string, StringValues> EmptyStringValuesDictionary = new ReadOnlyDictionary<string, StringValues>(new Dictionary<string, StringValues>());

private static readonly int ProtocolVersion = 1;

/// <inheritdoc />
public int Version => ProtocolVersion;

/// <inheritdoc />
public bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage message)
public bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage? message)
{
if (!BinaryMessageParser.TryParseMessage(ref input, out var payload))
{
Expand All @@ -37,7 +43,7 @@ public bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage
return true;
}

private static ServiceMessage ParseMessage(ref MessagePackReader reader)
private static ServiceMessage? ParseMessage(ref MessagePackReader reader)
{
var arrayLength = reader.ReadArrayHeader();

Expand Down Expand Up @@ -715,7 +721,7 @@ private static void WriteConnectionFlowControlMessage(ref MessagePackWriter writ
message.WriteExtensionMembers(ref writer);
}

private static void WriteStringArray(ref MessagePackWriter writer, IReadOnlyList<string> array)
private static void WriteStringArray(ref MessagePackWriter writer, IReadOnlyList<string>? array)
{
if (array?.Count > 0)
{
Expand Down Expand Up @@ -1351,7 +1357,7 @@ private static Claim[] ReadClaims(ref MessagePackReader reader)
return claims;
}

return null;
return [];
}

private static IDictionary<string, ReadOnlyMemory<byte>> ReadPayloads(ref MessagePackReader reader)
Expand All @@ -1370,10 +1376,10 @@ private static IDictionary<string, ReadOnlyMemory<byte>> ReadPayloads(ref Messag
return payloads;
}

return null;
return EmptyReadOnlyMemoryDictionary;
}

private static Dictionary<string, StringValues> ReadHeaders(ref MessagePackReader reader)
private static IDictionary<string, StringValues> ReadHeaders(ref MessagePackReader reader)
{
var headerCount = ReadMapLength(ref reader, "headers");
if (headerCount > 0)
Expand All @@ -1394,7 +1400,7 @@ private static Dictionary<string, StringValues> ReadHeaders(ref MessagePackReade
return headers;
}

return new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
return EmptyStringValuesDictionary;
}

private static bool ReadBoolean(ref MessagePackReader reader, string field)
Expand Down Expand Up @@ -1472,7 +1478,7 @@ private static string[] ReadStringArray(ref MessagePackReader reader, string fie
return array;
}

return null;
return [];
}

private static byte[] ReadBytes(ref MessagePackReader reader, string field)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable

namespace Microsoft.Azure.SignalR.Protocol;

Expand Down

0 comments on commit aaa0582

Please sign in to comment.