Skip to content

Commit

Permalink
v1.2.9
Browse files Browse the repository at this point in the history
- Fixes message packing in WebSocketGateway, now working also for inbound messages.
  • Loading branch information
genemars committed Jan 13, 2023
1 parent 427c682 commit c8d06cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
51 changes: 39 additions & 12 deletions MIG/Gateways/WebSocketGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<WsRequest>(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));
Expand All @@ -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
Expand Down Expand Up @@ -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()
{
}
}
}
4 changes: 2 additions & 2 deletions MIG/MigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ public static byte[] Pack(MigEvent e)
return MessagePackSerializer.Serialize(e);
}

public static MigEvent Unpack(byte[] data)
public static T Unpack<T>(byte[] data)
{
return MessagePackSerializer.Deserialize<MigEvent>(data, MessagePackSerializerOptions.Standard);
return MessagePackSerializer.Deserialize<T>(data, MessagePackSerializerOptions.Standard);
}

public static void ShellCommand(string command, string args)
Expand Down

0 comments on commit c8d06cc

Please sign in to comment.