📝 Protocol doc.

Signed-off-by: 秋雨落 <i@rain.cx>
This commit is contained in:
秋雨落
2023-09-17 18:12:38 +08:00
parent 220cdf5538
commit 163d52967e
6 changed files with 49 additions and 15 deletions
@@ -7,6 +7,6 @@ public class S2CAuthenticatedPacket : PacketS2C
{ {
protected override void WriteData(CborWriter writer) protected override void WriteData(CborWriter writer)
{ {
writer.WriteTextString(HandlerConstants.ResponseAuthenticated); writer.WriteTextString(HandlerConstants.ResponseErrorAuthenticated);
} }
} }
@@ -0,0 +1,12 @@
using System.Formats.Cbor;
using CrystalConnector.Handlers;
namespace CrystalConnector.Connector.Packet.S2C;
public class S2CMalformedPacket : PacketS2C
{
protected override void WriteData(CborWriter writer)
{
writer.WriteTextString(HandlerConstants.ResponseErrorMalformed);
}
}
@@ -5,11 +5,14 @@ public class HandlerConstants
public static readonly string OperationAuthenticate = "Authenticate"; public static readonly string OperationAuthenticate = "Authenticate";
public static readonly string OperationRegisterChannel = "Register"; public static readonly string OperationRegisterChannel = "Register";
public static readonly string OperationPublish = "Publish"; public static readonly string OperationPublish = "Publish";
// Todo: qyl27: Does we need a exit message?
// public static readonly string OperationExit = "Bye";
public static readonly string ResponseSuccessful = "Successful"; public static readonly string ResponseSuccessful = "Successful";
public static readonly string ResponseAuthenticated = "Authenticated";
public static readonly string ResponseErrorUnknownPacket = "Unknown"; public static readonly string ResponseErrorUnknownPacket = "Unknown";
public static readonly string ResponseErrorMalformed = "Malformed";
public static readonly string ResponseErrorAuthenticated = "Authenticated";
public static readonly string ResponseErrorUnauthenticated = "Unauthenticated"; public static readonly string ResponseErrorUnauthenticated = "Unauthenticated";
public static readonly string ResponseErrorUndefinedDirection = "UndefinedDirection"; public static readonly string ResponseErrorUndefinedDirection = "UndefinedDirection";
public static readonly string ResponseErrorUnregisteredDirection = "UnregisteredDirection"; public static readonly string ResponseErrorUnregisteredDirection = "UnregisteredDirection";
+14 -5
View File
@@ -67,12 +67,21 @@ public class WebSocketHandler
} }
var direction = (MessageDirection)directionValue; var direction = (MessageDirection)directionValue;
webSocket.GetConnectionInfo().RegisteredChannels.Add(channelId, new MessageChannel var channels = webSocket.GetConnectionInfo().RegisteredChannels;
if (channelId.Contains(channelId))
{ {
Id = channelId, channels[channelId].Direction |= direction;
Direction = direction }
}); else
{
webSocket.GetConnectionInfo().RegisteredChannels.Add(channelId, new MessageChannel
{
Id = channelId,
Direction = direction
});
}
Logger.LogInformation("Client {Name}({Id}) registered channel {Channel}(Direction: {Direction})", Logger.LogInformation("Client {Name}({Id}) registered channel {Channel}(Direction: {Direction})",
webSocket.GetConnectionInfo().Name, webSocket.GetConnectionInfo().Id, channelId, direction.ToString()); webSocket.GetConnectionInfo().Name, webSocket.GetConnectionInfo().Id, channelId, direction.ToString());
return new S2CSuccessfulPacket(); return new S2CSuccessfulPacket();
@@ -30,8 +30,8 @@ public static class WebSocketExtension
info.CompletionSource.SetResult(WebSocketHandleResult.Successful); info.CompletionSource.SetResult(WebSocketHandleResult.Successful);
webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
WebSocketConnectionManager.Info.Remove(webSocket); Purge(webSocket);
} }
public static async Task Send(this WebSocket webSocket, PacketS2C packet) public static async Task Send(this WebSocket webSocket, PacketS2C packet)
@@ -50,4 +50,9 @@ public static class WebSocketExtension
{ {
await webSocket.SendAsync(new ReadOnlyMemory<byte>(data, startIndex, length), WebSocketMessageType.Binary, true, CancellationToken.None); await webSocket.SendAsync(new ReadOnlyMemory<byte>(data, startIndex, length), WebSocketMessageType.Binary, true, CancellationToken.None);
} }
public static void Purge(this WebSocket webSocket)
{
WebSocketConnectionManager.Info.Remove(webSocket);
}
} }
@@ -2,6 +2,7 @@
using System.Net; using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using CrystalConnector.Configs; using CrystalConnector.Configs;
using CrystalConnector.Connector.Packet.S2C;
using CrystalConnector.Handlers; using CrystalConnector.Handlers;
using CrystalConnector.Utilities; using CrystalConnector.Utilities;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@@ -49,26 +50,30 @@ public class WebSocketManager : IWebSocketManager
{ {
if (Config.IsDebug()) if (Config.IsDebug())
{ {
Logger.LogWarning(ex, "Malformed message!"); Logger.LogWarning(ex, "Unknown message!");
} }
Disconnect(webSocket);
await webSocket.Send(new S2CUnknownPacket());
} }
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
if (Config.IsDebug()) if (Config.IsDebug())
{ {
Logger.LogWarning(ex, "Bad message!"); Logger.LogWarning(ex, "Malformed message!");
} }
Disconnect(webSocket);
await webSocket.Send(new S2CMalformedPacket());
} }
catch (MessageException ex) catch (MessageException ex)
{ {
Logger.LogWarning(ex, "Why do that?"); Logger.LogWarning(ex, "Why do that?");
Disconnect(webSocket); await webSocket.Send(new S2CMalformedPacket());
} }
receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
} }
webSocket.Purge();
} }
public void Disconnect(WebSocket webSocket) public void Disconnect(WebSocket webSocket)