123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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<TEvent,TData>(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<TEvent>(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<byte> bytes = new List<byte>();
- bytes.AddRange(BitConverter.GetBytes(data.Length));
- for(int i=0;i<data.Length;i++)
- {
- bytes.AddRange(GetObjectBytes(data[i]));
- }
- msg.Data = bytes.ToArray();
- }
- return msg;
- }
- private static byte[] GetObjectBytes(object data)
- {
- if(data ==null) return new byte[4] { 0, 0, 0, 0 };
- using (BinaryWriter writer = new BinaryWriter(new MemoryStream()))
- {
- byte[] bytes = MessagePack.MessagePackSerializer.Serialize(data, options);
- writer.Write(bytes.Length);
- writer.Write(bytes);
- return (writer.BaseStream as MemoryStream)!.ToArray();
- }
- }
- public static object[] GetAnonyDatas(byte[] data)
- {
- if (data == null || data.Length ==0) return new object[0];
- object[] d = new object[Unsafe.As<byte, int>(ref data[0])];
- int start = 4;
- for(int i=0;i<d.Length;i++)
- {
- byte[] temp = new byte[Unsafe.As<byte, int>(ref data[start])];
- start += 4;
- Unsafe.CopyBlock(ref temp[0], ref data[start], (uint)temp.Length);
- start += temp.Length;
- d[i] = MessagePack.MessagePackSerializer.Deserialize<object>(temp,options);
- }
- return d;
- }
- public static TData GetValue<TData>(byte[] data)
- {
- TData d = default!;
- try
- {
- d = MessagePack.MessagePackSerializer.Deserialize<TData>(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);
- }
- }
- }
|