mirror of
https://github.com/MeowCraftMC/CrystalConnector
synced 2025-05-22 02:43:00 +00:00
@@ -7,6 +7,6 @@ public class S2CAuthenticatedPacket : PacketS2C
|
||||
{
|
||||
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 OperationRegisterChannel = "Register";
|
||||
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 ResponseAuthenticated = "Authenticated";
|
||||
|
||||
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 ResponseErrorUndefinedDirection = "UndefinedDirection";
|
||||
public static readonly string ResponseErrorUnregisteredDirection = "UnregisteredDirection";
|
||||
|
||||
@@ -68,11 +68,20 @@ public class WebSocketHandler
|
||||
|
||||
var direction = (MessageDirection)directionValue;
|
||||
|
||||
webSocket.GetConnectionInfo().RegisteredChannels.Add(channelId, new MessageChannel
|
||||
var channels = webSocket.GetConnectionInfo().RegisteredChannels;
|
||||
if (channelId.Contains(channelId))
|
||||
{
|
||||
Id = channelId,
|
||||
Direction = direction
|
||||
});
|
||||
channels[channelId].Direction |= direction;
|
||||
}
|
||||
else
|
||||
{
|
||||
webSocket.GetConnectionInfo().RegisteredChannels.Add(channelId, new MessageChannel
|
||||
{
|
||||
Id = channelId,
|
||||
Direction = direction
|
||||
});
|
||||
}
|
||||
|
||||
Logger.LogInformation("Client {Name}({Id}) registered channel {Channel}(Direction: {Direction})",
|
||||
webSocket.GetConnectionInfo().Name, webSocket.GetConnectionInfo().Id, channelId, direction.ToString());
|
||||
return new S2CSuccessfulPacket();
|
||||
|
||||
@@ -31,7 +31,7 @@ public static class WebSocketExtension
|
||||
|
||||
webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
|
||||
|
||||
WebSocketConnectionManager.Info.Remove(webSocket);
|
||||
Purge(webSocket);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static void Purge(this WebSocket webSocket)
|
||||
{
|
||||
WebSocketConnectionManager.Info.Remove(webSocket);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using CrystalConnector.Configs;
|
||||
using CrystalConnector.Connector.Packet.S2C;
|
||||
using CrystalConnector.Handlers;
|
||||
using CrystalConnector.Utilities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -49,26 +50,30 @@ public class WebSocketManager : IWebSocketManager
|
||||
{
|
||||
if (Config.IsDebug())
|
||||
{
|
||||
Logger.LogWarning(ex, "Malformed message!");
|
||||
Logger.LogWarning(ex, "Unknown message!");
|
||||
}
|
||||
Disconnect(webSocket);
|
||||
|
||||
await webSocket.Send(new S2CUnknownPacket());
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
if (Config.IsDebug())
|
||||
{
|
||||
Logger.LogWarning(ex, "Bad message!");
|
||||
Logger.LogWarning(ex, "Malformed message!");
|
||||
}
|
||||
Disconnect(webSocket);
|
||||
|
||||
await webSocket.Send(new S2CMalformedPacket());
|
||||
}
|
||||
catch (MessageException ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "Why do that?");
|
||||
Disconnect(webSocket);
|
||||
await webSocket.Send(new S2CMalformedPacket());
|
||||
}
|
||||
|
||||
receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
}
|
||||
|
||||
webSocket.Purge();
|
||||
}
|
||||
|
||||
public void Disconnect(WebSocket webSocket)
|
||||
|
||||
Reference in New Issue
Block a user