From c8d06cc1e75a4a9d78d391a8dd22da2ff0650f0d Mon Sep 17 00:00:00 2001 From: Gene Date: Fri, 13 Jan 2023 23:11:45 +0100 Subject: [PATCH] v1.2.9 - Fixes message packing in WebSocketGateway, now working also for inbound messages. --- MIG/Gateways/WebSocketGateway.cs | 51 ++++++++++++++++++++++++-------- MIG/MigService.cs | 4 +-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/MIG/Gateways/WebSocketGateway.cs b/MIG/Gateways/WebSocketGateway.cs index db4e5ab..a823d86 100644 --- a/MIG/Gateways/WebSocketGateway.cs +++ b/MIG/Gateways/WebSocketGateway.cs @@ -23,6 +23,7 @@ limitations under the License. using System; using System.Collections.Generic; +using MessagePack; using MIG.Config; using MIG.Gateways.Authentication; using Newtonsoft.Json; @@ -76,8 +77,8 @@ public class WebSocketGateway : IMigGateway private int servicePort = 8181; private string authenticationSchema = WebAuthenticationSchema.None; private string authenticationRealm = "MIG Secure Zone"; - private bool ignoreExtensions = false; - private bool messagePack = false; + private bool ignoreExtensions; + private bool messagePack; public WebSocketGateway() { @@ -164,19 +165,29 @@ public void ProcessRequest(MessageEventArgs message, WebSocketContext context) bool isJsonPayload = false; string requestId = ""; string messageData = message.Data; - try + if (messagePack) { - dynamic jsonPayload = JsonConvert.DeserializeObject(messageData); - if (jsonPayload != null && jsonPayload.id != null) - { - isJsonPayload = true; - requestId = jsonPayload.id; - messageData = jsonPayload.data; - } + var request = MigService.Unpack(message.RawData); + requestId = request.id; + messageData = request.data; + isJsonPayload = true; } - catch (Exception e) + else { - // Not valid JSON, process as text message + try + { + dynamic jsonPayload = JsonConvert.DeserializeObject(messageData); + if (jsonPayload != null && jsonPayload.id != null) + { + requestId = jsonPayload.id; + messageData = jsonPayload.data; + isJsonPayload = true; + } + } + catch (Exception e) + { + // Not valid JSON, process as text message + } } var migContext = new MigContext(ContextSource.WebSocketGateway, message); var migRequest = new MigClientRequest(migContext, new MigInterfaceCommand(messageData, message)); @@ -189,6 +200,10 @@ public void ProcessRequest(MessageEventArgs message, WebSocketContext context) var responseEvent = new MigEvent("#", requestId, "", "Response.Data", migRequest.ResponseData); if (messagePack) { + if (responseEvent.Value is string == false) + { + responseEvent.Value = JsonConvert.SerializeObject(responseEvent.Value); + } context.WebSocket.Send(MigService.Pack(responseEvent)); } else @@ -260,4 +275,16 @@ private void SendMessage(MigEvent message) } } } + + [Serializable, MessagePackObject] + public class WsRequest + { + [Key(0)] + public string id { get; set; } + [Key(1)] + public string data { get; set; } + public WsRequest() + { + } + } } diff --git a/MIG/MigService.cs b/MIG/MigService.cs index 9329509..fddd23e 100644 --- a/MIG/MigService.cs +++ b/MIG/MigService.cs @@ -452,9 +452,9 @@ public static byte[] Pack(MigEvent e) return MessagePackSerializer.Serialize(e); } - public static MigEvent Unpack(byte[] data) + public static T Unpack(byte[] data) { - return MessagePackSerializer.Deserialize(data, MessagePackSerializerOptions.Standard); + return MessagePackSerializer.Deserialize(data, MessagePackSerializerOptions.Standard); } public static void ShellCommand(string command, string args)