From b97bf7b2df96f5191fe514748da531a326cbd542 Mon Sep 17 00:00:00 2001 From: Marco Bacis Date: Tue, 27 Jun 2023 13:30:45 +0200 Subject: [PATCH] fix: fix message parser performance --- WeArtClient.cs | 4 +- WeArtMessageCustomSerializer.cs | 65 +++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/WeArtClient.cs b/WeArtClient.cs index ba88bdc..84b0a91 100644 --- a/WeArtClient.cs +++ b/WeArtClient.cs @@ -257,8 +257,8 @@ private bool ReceiveMessages(out IWeArtMessage[] messages) messages = new IWeArtMessage[split.Length]; for (int i = 0; i < messages.Length; i++) { - _messageSerializer.Deserialize(split[i], out messages[i]); - ForwardMessage(split[i], messages[i]); + if(_messageSerializer.Deserialize(split[i], out messages[i])) + ForwardMessage(split[i], messages[i]); } return true; } diff --git a/WeArtMessageCustomSerializer.cs b/WeArtMessageCustomSerializer.cs index 5a5d60f..9244d2b 100644 --- a/WeArtMessageCustomSerializer.cs +++ b/WeArtMessageCustomSerializer.cs @@ -59,7 +59,7 @@ public override bool Serialize(IWeArtMessage message, out string data) if (message is WeArtJsonMessage jsonMessage) { - data = SerializeJsonMessage(reflectionData, jsonMessage); + data = SerializeJsonMessage(reflectionData, jsonMessage); return true; } @@ -105,35 +105,52 @@ public override bool Deserialize(string data, out IWeArtMessage message) { try { - string[] split = data.Split(separator); - var messageID = split[0]; + if (IsJsonMessage(data)) + message = DeserializeJsonMessage(data); + else + message = DeserializeCsvMessage(data); + } + catch (Exception) + { + message = null; + } + return message != null; + } - var reflectionData = ReflectionData.GetFrom(messageID); + private bool IsJsonMessage(string data) + { + // Heuristic to see fast (without regex or parsing) if the data we received could be a json message + // Old protocol messages does not start with "[" or "{" so it should be ok + if (string.IsNullOrWhiteSpace(data)) return false; + data = data.Trim(); + + bool isObject = data.StartsWith("{") && data.EndsWith("}"); + bool isArray = data.StartsWith("[") && data.EndsWith("]"); + return isObject || isArray; + } - message = (IWeArtMessage)Activator.CreateInstance(reflectionData.Type); + private IWeArtMessage DeserializeCsvMessage(string data) + { + string[] split = data.Split(separator); + var messageID = split[0]; - if (message is IWeArtMessageCustomSerialization deserializableMessage) - return deserializableMessage.Deserialize(split); + var reflectionData = ReflectionData.GetFrom(messageID); + var message = (IWeArtMessage)Activator.CreateInstance(reflectionData.Type); + if (message is IWeArtMessageCustomSerialization deserializableMessage) + { + deserializableMessage.Deserialize(split); + } + else + { for (int i = 1; i < split.Length; i++) { var field = reflectionData.Fields[i - 1]; var value = Deserialize(split[i], field.FieldType); field.SetValue(message, value); } - return true; - } - catch (MessageTypeNotFound) - { - // Try with json - message = DeserializeJsonMessage(data); - return message != null; - } - catch (Exception) - { - message = null; - return false; } + return message; } private IWeArtMessage DeserializeJsonMessage(string data) @@ -142,6 +159,16 @@ private IWeArtMessage DeserializeJsonMessage(string data) { JsonMessageTemplate json = JsonConvert.DeserializeObject(data); ReflectionData reflectionData = ReflectionData.GetFrom(json.Type); + + // Message without parameters + if (json.Data is null) + { + var message = (WeArtJsonMessage)Activator.CreateInstance(reflectionData.Type); + message.Timestamp = json.Timestamp; + return message; + } + + // Message with parameters return (IWeArtMessage)json.Data.ToObject(reflectionData.Type) ?? null; } catch (MessageTypeNotFound)