using EventBus; using MessagePack; using MessagePack.Resolvers; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Text; namespace TcpEventBus { public static class MsgTool { private static MessagePackSerializerOptions options= TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true); public static DataMsg GetMsg(this TEvent evnet,TData data,Properties? properties = null,bool isresult=false) where TEvent:IBaseEventData { DataMsg msg = new DataMsg(); msg.EventType = typeof(TEvent).FullName!; msg.EventName = evnet.EventName; msg.IsResultMsg = isresult; if (properties != null) { msg.Properties = properties.ToString(); } msg.Data = MessagePack.MessagePackSerializer.Serialize(data, options); return msg; } public static DataMsg GetAnonyMsg(this TEvent evnet, object[] data, Properties? properties = null,bool isresult=false) where TEvent :IBaseEventData { DataMsg msg = new DataMsg(); msg.EventType = typeof(TEvent).FullName!; msg.EventName = evnet.EventName; msg.IsResultMsg = isresult; if (properties != null) { msg.Properties = properties.ToString(); } if (data == null || data.Length ==0) { msg.Data = new byte[0]; } else { List bytes = new List(); bytes.AddRange(BitConverter.GetBytes(data.Length)); for(int i=0;i(ref data[0])]; int start = 4; for(int i=0;i(ref data[start])]; start += 4; Unsafe.CopyBlock(ref temp[0], ref data[start], (uint)temp.Length); start += temp.Length; d[i] = MessagePack.MessagePackSerializer.Deserialize(temp,options); } return d; } public static TData GetValue(byte[] data) { TData d = default!; try { d = MessagePack.MessagePackSerializer.Deserialize(data, options); } catch { } return d; } public static byte[] GetBytes(this DataMsg msg) { if (msg == null) return new byte[0]; return MessagePack.MessagePackSerializer.Serialize(msg,options); } } }