Skip to content

Commit

Permalink
add connection id in handshake response and add echo in ping. (#1821)
Browse files Browse the repository at this point in the history
  • Loading branch information
vwxyzh authored Aug 24, 2023
1 parent 06c5071 commit d050dd2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ protected Task OnServiceErrorAsync(ServiceErrorMessage serviceErrorMessage)

protected virtual Task OnPingMessageAsync(PingMessage pingMessage)
{
if (RuntimeServicePingMessage.IsEchoMessage(pingMessage))
{
return WriteAsync(pingMessage);
}
if (RuntimeServicePingMessage.TryGetOffline(pingMessage, out var instanceId))
{
Log.ReceivedInstanceOfflinePing(Logger, instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Azure.SignalR
{
internal static class RuntimeServicePingMessage
{
private const string EchoKey = "echo";
private const string OfflineKey = "offline";
private const string TargetKey = "target";
private const string StatusKey = "status";
Expand Down Expand Up @@ -52,6 +53,11 @@ internal static class RuntimeServicePingMessage
private static readonly ServicePingMessage ServersTag =
new ServicePingMessage { Messages = new[] { ServersKey, string.Empty } };

public static bool IsEchoMessage(this ServicePingMessage ping)
{
return TryGetValue(ping, EchoKey, out _);
}

public static bool TryGetMessageLogEnableFlag(this ServicePingMessage ping, out bool enableMessageLog)
{
if (TryGetValue(ping, DiagnosticLogsMessagingTypeKey, out var value))
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ public class HandshakeResponseMessage : ExtensibleServiceMessage
/// </summary>
public string ErrorMessage { get; set; }

/// <summary>
/// Gets or sets the id of this connection.
/// </summary>
public string ConnectionId { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="HandshakeResponseMessage"/> class.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,11 @@ private static void WriteHandshakeRequestMessage(ref MessagePackWriter writer, H

private static void WriteHandshakeResponseMessage(ref MessagePackWriter writer, HandshakeResponseMessage message)
{
writer.WriteArrayHeader(3);
writer.WriteArrayHeader(4);
writer.Write(ServiceProtocolConstants.HandshakeResponseType);
writer.Write(message.ErrorMessage);
message.WriteExtensionMembers(ref writer);
writer.Write(message.ConnectionId);
}

private static void WriteAccessKeyRequestMessage(ref MessagePackWriter writer, AccessKeyRequestMessage message)
Expand Down Expand Up @@ -800,6 +801,10 @@ private static HandshakeResponseMessage CreateHandshakeResponseMessage(ref Messa
{
result.ReadExtensionMembers(ref reader);
}
if (arrayLength >= 4)
{
result.ConnectionId = ReadString(ref reader, "connectionId");
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ private bool HandshakeRequestMessagesEqual(HandshakeRequestMessage x, HandshakeR

private bool HandshakeResponseMessagesEqual(HandshakeResponseMessage x, HandshakeResponseMessage y)
{
return StringEqual(x.ErrorMessage, y.ErrorMessage);
return StringEqual(x.ErrorMessage, y.ErrorMessage) &&
StringEqual(x.ConnectionId, y.ConnectionId);
}

private bool AccessKeyRequestMessageEqual(AccessKeyRequestMessage x, AccessKeyRequestMessage y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ public static IEnumerable<object[]> TestParseOldData
name: "HandshakeResponse_NoOptionalField",
message: new HandshakeResponseMessage(),
binary: "kgKg"),
new ProtocolTestData(
name: "HandshakeResponse_NoConnectionId",
message: new HandshakeResponseMessage(),
binary: "kwKggA=="),
new ProtocolTestData(
name: "HandshakeResponseWithError_NoOptionalField",
message: new HandshakeResponseMessage("Version mismatch."),
binary: "kgKxVmVyc2lvbiBtaXNtYXRjaC4="),
new ProtocolTestData(
name: "HandshakeResponseWithError_NoConnectionId",
message: new HandshakeResponseMessage("Version mismatch."),
binary: "kwKxVmVyc2lvbiBtaXNtYXRjaC6A"),
new ProtocolTestData(
name: "OpenConnection_NoOptionalField",
message: new OpenConnectionMessage("conn1", null),
Expand Down Expand Up @@ -280,11 +288,15 @@ public static IEnumerable<object[]> TestParseOldData
new ProtocolTestData(
name: "HandshakeResponse",
message: new HandshakeResponseMessage(),
binary: "kwKggA=="),
binary: "lAKggMA="),
new ProtocolTestData(
name: "HandshakeResponseWithError",
message: new HandshakeResponseMessage("Version mismatch."),
binary: "kwKxVmVyc2lvbiBtaXNtYXRjaC6A"),
binary: "lAKxVmVyc2lvbiBtaXNtYXRjaC6AwA=="),
new ProtocolTestData(
name: "HandshakeResponseWithConnectionId",
message: new HandshakeResponseMessage() { ConnectionId = "abc" },
binary: "lAKggKNhYmM="),
new ProtocolTestData(
name: "AccessKeyRequestMessage",
message: new AccessKeyRequestMessage("token"),
Expand Down

0 comments on commit d050dd2

Please sign in to comment.