MsgTool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using EventBus;
  2. using MessagePack;
  3. using MessagePack.Resolvers;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Serialization;
  8. using System.Text;
  9. namespace TcpEventBus
  10. {
  11. public static class MsgTool
  12. {
  13. private static MessagePackSerializerOptions options= TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true);
  14. public static DataMsg GetMsg<TEvent,TData>(this TEvent evnet,TData data,Properties? properties = null,bool isresult=false) where TEvent:IBaseEventData
  15. {
  16. DataMsg msg = new DataMsg();
  17. msg.EventType = typeof(TEvent).FullName!;
  18. msg.EventName = evnet.EventName;
  19. msg.IsResultMsg = isresult;
  20. if (properties != null)
  21. {
  22. msg.Properties = properties.ToString();
  23. }
  24. msg.Data = MessagePack.MessagePackSerializer.Serialize(data, options);
  25. return msg;
  26. }
  27. public static DataMsg GetAnonyMsg<TEvent>(this TEvent evnet, object[] data, Properties? properties = null,bool isresult=false) where TEvent :IBaseEventData
  28. {
  29. DataMsg msg = new DataMsg();
  30. msg.EventType = typeof(TEvent).FullName!;
  31. msg.EventName = evnet.EventName;
  32. msg.IsResultMsg = isresult;
  33. if (properties != null)
  34. {
  35. msg.Properties = properties.ToString();
  36. }
  37. if (data == null || data.Length ==0)
  38. {
  39. msg.Data = new byte[0];
  40. }
  41. else
  42. {
  43. List<byte> bytes = new List<byte>();
  44. bytes.AddRange(BitConverter.GetBytes(data.Length));
  45. for(int i=0;i<data.Length;i++)
  46. {
  47. bytes.AddRange(GetObjectBytes(data[i]));
  48. }
  49. msg.Data = bytes.ToArray();
  50. }
  51. return msg;
  52. }
  53. private static byte[] GetObjectBytes(object data)
  54. {
  55. if(data ==null) return new byte[4] { 0, 0, 0, 0 };
  56. using (BinaryWriter writer = new BinaryWriter(new MemoryStream()))
  57. {
  58. byte[] bytes = MessagePack.MessagePackSerializer.Serialize(data, options);
  59. writer.Write(bytes.Length);
  60. writer.Write(bytes);
  61. return (writer.BaseStream as MemoryStream)!.ToArray();
  62. }
  63. }
  64. public static object[] GetAnonyDatas(byte[] data)
  65. {
  66. if (data == null || data.Length ==0) return new object[0];
  67. object[] d = new object[Unsafe.As<byte, int>(ref data[0])];
  68. int start = 4;
  69. for(int i=0;i<d.Length;i++)
  70. {
  71. byte[] temp = new byte[Unsafe.As<byte, int>(ref data[start])];
  72. start += 4;
  73. Unsafe.CopyBlock(ref temp[0], ref data[start], (uint)temp.Length);
  74. start += temp.Length;
  75. d[i] = MessagePack.MessagePackSerializer.Deserialize<object>(temp,options);
  76. }
  77. return d;
  78. }
  79. public static TData GetValue<TData>(byte[] data)
  80. {
  81. TData d = default!;
  82. try
  83. {
  84. d = MessagePack.MessagePackSerializer.Deserialize<TData>(data, options);
  85. }
  86. catch
  87. {
  88. }
  89. return d;
  90. }
  91. public static byte[] GetBytes(this DataMsg msg)
  92. {
  93. if (msg == null) return new byte[0];
  94. return MessagePack.MessagePackSerializer.Serialize(msg,options);
  95. }
  96. }
  97. }