From 783e850de5ebcb7cd02dc84ae31388ab1e5bdce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=9B=A8=E8=90=BD?= Date: Wed, 10 Jan 2024 10:46:56 +0800 Subject: [PATCH] :sparkles: Fix entity. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋雨落 --- .../CrystalConnector.Protocol.csproj | 1 + .../Packets/C2S/PublishPacket.cs | 12 +++++----- .../Packets/S2C/ForwardPacket.cs | 22 ++++++++++--------- CrystalConnector.sln | 6 +++++ CrystalConnector/Connector/MessageChannel.cs | 5 +++-- .../Connector/WebSocketConnectionManager.cs | 5 +++-- CrystalConnector/Handlers/WebSocketHandler.cs | 16 +++++++------- .../Utilities/WebSocketConnectionInfo.cs | 5 +++-- 8 files changed, 43 insertions(+), 29 deletions(-) diff --git a/CrystalConnector.Protocol/CrystalConnector.Protocol.csproj b/CrystalConnector.Protocol/CrystalConnector.Protocol.csproj index 7809f00..228d81b 100644 --- a/CrystalConnector.Protocol/CrystalConnector.Protocol.csproj +++ b/CrystalConnector.Protocol/CrystalConnector.Protocol.csproj @@ -10,6 +10,7 @@ True True https://github.com/MeowCraftMC/CrystalConnector + 1.0.1 diff --git a/CrystalConnector.Protocol/Packets/C2S/PublishPacket.cs b/CrystalConnector.Protocol/Packets/C2S/PublishPacket.cs index 107110f..c816ada 100644 --- a/CrystalConnector.Protocol/Packets/C2S/PublishPacket.cs +++ b/CrystalConnector.Protocol/Packets/C2S/PublishPacket.cs @@ -1,14 +1,16 @@ -using CrystalConnector.Protocol.Messages; +using CrystalConnector.Protocol.Entities; +using CrystalConnector.Protocol.Messages; +using CrystalConnector.Protocol.Utilities; using Google.Protobuf; namespace CrystalConnector.Protocol.Packets.C2S; public class PublishPacket : IPacket { - public NamespacedName Channel { get; set; } + public NamespacedId Channel { get; set; } public string Payload { get; set; } - public PublishPacket(NamespacedName channel, string payload) + public PublishPacket(NamespacedId channel, string payload) { Channel = channel; Payload = payload; @@ -26,7 +28,7 @@ public class PublishPacket : IPacket public void Read(byte[] bytes) { var message = Publish.Parser.ParseFrom(bytes); - Channel = message.Channel; + Channel = message.Channel.ToId(); Payload = message.Payload; } @@ -34,7 +36,7 @@ public class PublishPacket : IPacket { var message = new Publish { - Channel = Channel, + Channel = Channel.ToName(), Payload = Payload }; diff --git a/CrystalConnector.Protocol/Packets/S2C/ForwardPacket.cs b/CrystalConnector.Protocol/Packets/S2C/ForwardPacket.cs index 42e8989..82db0c8 100644 --- a/CrystalConnector.Protocol/Packets/S2C/ForwardPacket.cs +++ b/CrystalConnector.Protocol/Packets/S2C/ForwardPacket.cs @@ -1,4 +1,6 @@ -using CrystalConnector.Protocol.Messages; +using CrystalConnector.Protocol.Entities; +using CrystalConnector.Protocol.Messages; +using CrystalConnector.Protocol.Utilities; using Google.Protobuf; namespace CrystalConnector.Protocol.Packets.S2C; @@ -6,17 +8,13 @@ namespace CrystalConnector.Protocol.Packets.S2C; public class ForwardPacket : IPacket { public string Publisher { get; set; } - public NamespacedName Channel { get; set; } + public NamespacedId Channel { get; set; } public string Payload { get; set; } - public ForwardPacket(string publisher, (string Namespace, string Name) channel, string payload) + public ForwardPacket(string publisher, NamespacedId channel, string payload) { Publisher = publisher; - Channel = new NamespacedName - { - Namespace = channel.Namespace, - Name = channel.Name - }; + Channel = channel; Payload = payload; } @@ -33,7 +31,7 @@ public class ForwardPacket : IPacket { var message = Forward.Parser.ParseFrom(bytes); Publisher = message.Publisher; - Channel = message.Channel; + Channel = message.Channel.ToId(); Payload = message.Payload; } @@ -42,7 +40,11 @@ public class ForwardPacket : IPacket var message = new Forward { Publisher = Publisher, - Channel = Channel, + Channel = new NamespacedName + { + Namespace = Channel.Namespace, + Name = Channel.Name + }, Payload = Payload }; diff --git a/CrystalConnector.sln b/CrystalConnector.sln index 1d1c5ab..aa4a05d 100644 --- a/CrystalConnector.sln +++ b/CrystalConnector.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorTest", "ConnectorT EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrystalConnector.Protocol", "CrystalConnector.Protocol\CrystalConnector.Protocol.csproj", "{1769D402-129D-46D2-9347-70F3E875FE7B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrystalConnector.Client", "CrystalConnector.Client\CrystalConnector.Client.csproj", "{DC40A2D9-1D24-4561-8C9C-DA7124BDD965}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {1769D402-129D-46D2-9347-70F3E875FE7B}.Debug|Any CPU.Build.0 = Debug|Any CPU {1769D402-129D-46D2-9347-70F3E875FE7B}.Release|Any CPU.ActiveCfg = Release|Any CPU {1769D402-129D-46D2-9347-70F3E875FE7B}.Release|Any CPU.Build.0 = Release|Any CPU + {DC40A2D9-1D24-4561-8C9C-DA7124BDD965}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC40A2D9-1D24-4561-8C9C-DA7124BDD965}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC40A2D9-1D24-4561-8C9C-DA7124BDD965}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC40A2D9-1D24-4561-8C9C-DA7124BDD965}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/CrystalConnector/Connector/MessageChannel.cs b/CrystalConnector/Connector/MessageChannel.cs index f0aa82f..0c5bc85 100644 --- a/CrystalConnector/Connector/MessageChannel.cs +++ b/CrystalConnector/Connector/MessageChannel.cs @@ -1,10 +1,11 @@ -using CrystalConnector.Protocol.Messages; +using CrystalConnector.Protocol.Entities; +using CrystalConnector.Protocol.Messages; namespace CrystalConnector.Connector; public class MessageChannel { - public required (string Namespace, string Name) Id { get; set; } + public required NamespacedId Id { get; set; } public required MessageDirection Direction { get; set; } } \ No newline at end of file diff --git a/CrystalConnector/Connector/WebSocketConnectionManager.cs b/CrystalConnector/Connector/WebSocketConnectionManager.cs index fe074bd..30f67fd 100644 --- a/CrystalConnector/Connector/WebSocketConnectionManager.cs +++ b/CrystalConnector/Connector/WebSocketConnectionManager.cs @@ -1,4 +1,5 @@ using System.Net.WebSockets; +using CrystalConnector.Protocol.Entities; using CrystalConnector.Protocol.Messages; using CrystalConnector.Protocol.Packets.S2C; using CrystalConnector.Utilities; @@ -9,7 +10,7 @@ public class WebSocketConnectionManager { internal static Dictionary Info { get; } = new(); - public static bool HaveClientRegistered((string Namespace, string Name) client) + public static bool HaveClientRegistered(NamespacedId client) { foreach (var (_, info) in Info) { @@ -30,7 +31,7 @@ public class WebSocketConnectionManager } } - public static async Task Broadcast(string origin, (string Namespace, string Name) channel, string payload, bool allowUnauthenticated = false) + public static async Task Broadcast(string origin, NamespacedId channel, string payload, bool allowUnauthenticated = false) { var packet = new ForwardPacket(origin, channel, payload); diff --git a/CrystalConnector/Handlers/WebSocketHandler.cs b/CrystalConnector/Handlers/WebSocketHandler.cs index bc0f700..f392c21 100644 --- a/CrystalConnector/Handlers/WebSocketHandler.cs +++ b/CrystalConnector/Handlers/WebSocketHandler.cs @@ -30,17 +30,17 @@ public class WebSocketHandler { case AuthenticatePacket authenticatePacket: { - var clientId = (authenticatePacket.ClientId.Namespace, authenticatePacket.ClientId.Name); + var clientId = authenticatePacket.ClientId.ToId(); if (authenticatePacket.Key == Config.GetSecretKey()) { if (WebSocketConnectionManager.HaveClientRegistered(clientId)) { - Logger.LogInformation("Client {Id} tried authorize with name {Name}, but it is already authorized!", clientId.ToIdString(), authenticatePacket.ClientName); + Logger.LogInformation("Client {Id} tried authorize with name {Name}, but it is already authorized!", clientId, authenticatePacket.ClientName); return new ResultPacket(Error.NameExists); } - Logger.LogInformation("Client {Name}({Id}) authorized", authenticatePacket.ClientName, clientId.ToIdString()); + Logger.LogInformation("Client {Name}({Id}) authorized", authenticatePacket.ClientName, clientId); var connectionInfo = webSocket.GetConnectionInfo(); @@ -52,7 +52,7 @@ public class WebSocketHandler foreach (var channel in authenticatePacket.Channels) { - var id = channel.Id.ToTuple(); + var id = channel.Id.ToId(); if (channels.TryGetValue(id, out var chan)) { chan.Direction = channel.Direction; @@ -70,13 +70,13 @@ public class WebSocketHandler return new ResultPacket(); } - Logger.LogWarning("Client {Name}({Id}) auth failed with wrong key!", authenticatePacket.ClientName, clientId.ToIdString()); + Logger.LogWarning("Client {Name}({Id}) auth failed with wrong key!", authenticatePacket.ClientName, clientId); return new ResultPacket(Error.Unauthenticated); } case PublishPacket publishPacket: { var connectionInfo = webSocket.GetConnectionInfo(); - var id = publishPacket.Channel.ToTuple(); + var id = publishPacket.Channel; if (!connectionInfo.Authenticated) { @@ -88,7 +88,7 @@ public class WebSocketHandler || registered.Direction == MessageDirection.All) { Logger.LogWarning("Channel {Channel}(Direction: {Direction}) have not been registered by Client {Name}({Id})", - id.ToIdString(), MessageDirection.Outgoing, webSocket.GetConnectionInfo().Name, webSocket.GetConnectionInfo().Id); + id, MessageDirection.Outgoing, webSocket.GetConnectionInfo().Name, webSocket.GetConnectionInfo().Id); return new ResultPacket(Error.UnregisteredChannel); } @@ -98,7 +98,7 @@ public class WebSocketHandler if (Config.IsDebug()) { Logger.LogDebug("Client {Name}({Id}) send data in {Channel}: {Data}", - name, webSocket.GetConnectionInfo().ClientId!.Value.ToIdString(), id.ToIdString(), publishPacket.Payload); + name, webSocket.GetConnectionInfo().ClientId, id, publishPacket.Payload); } return new ResultPacket(); diff --git a/CrystalConnector/Utilities/WebSocketConnectionInfo.cs b/CrystalConnector/Utilities/WebSocketConnectionInfo.cs index 2e210d2..62a0b74 100644 --- a/CrystalConnector/Utilities/WebSocketConnectionInfo.cs +++ b/CrystalConnector/Utilities/WebSocketConnectionInfo.cs @@ -1,5 +1,6 @@ using System.Net; using CrystalConnector.Connector; +using CrystalConnector.Protocol.Entities; namespace CrystalConnector.Utilities; @@ -13,9 +14,9 @@ public class WebSocketConnectionInfo public string? Name { get; set; } - public (string Namespace, string Name)? ClientId { get; set; } + public NamespacedId? ClientId { get; set; } public bool Authenticated { get; set; } = false; - public Dictionary<(string Namespace, string Name), MessageChannel> RegisteredChannels { get; } = new(); + public Dictionary RegisteredChannels { get; } = new(); } \ No newline at end of file