MQAnonymousEventData{T}.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using EventBus;
  2. using TcpEventBus;
  3. namespace ActiveMQCommunication
  4. {
  5. public class MQAnonymousEventData<T> : IAnonymousEventData<T>
  6. {
  7. EasyMQ.IBus _Bus;
  8. private Dictionary<string, string> dic = new Dictionary<string, string>();
  9. public MQAnonymousEventData(EasyMQ.IBus socket, string eventName, string hash)
  10. {
  11. _Bus = socket;
  12. EventName = eventName;
  13. Hash = hash;
  14. dic[nameof(EventName)] = eventName + ";" + typeof(T).FullName;
  15. }
  16. public string EventName { get; private set; }
  17. public string Hash { get; private set; }
  18. public void Clear()
  19. {
  20. }
  21. public T Publish(object sender, Properties? properties = null, params object[] data)
  22. {
  23. bool timeout = false;
  24. var msg =this.GetAnonyMsg(data, properties);
  25. var val = _Bus.RPC.Request<DataMsg, T>(msg, ref timeout,dic);
  26. if (timeout || val == null) return default;
  27. return val;
  28. }
  29. public Task<T> PublishAsync(object sender, Properties? properties = null, params object[] data)
  30. {
  31. return Task.Run(() => Publish(sender, properties, data));
  32. }
  33. public List<T> PublishList(object sender, Properties? properties = null, params object[] data)
  34. {
  35. bool timeout = false;
  36. var msg = this.GetAnonyMsg(data, properties);
  37. var val = _Bus.RPC.Request<DataMsg, List<T>>(msg, ref timeout, dic);
  38. if (timeout || val == null) return new List<T>();
  39. return val;
  40. }
  41. public Task<List<T>> PublishListAsync(object sender, Properties? properties = null, params object[] data)
  42. {
  43. return Task.Run(() => PublishList(sender, properties, data));
  44. }
  45. public Guid Subscrip(Func<object, EventArgs<object[]>, T> func, Properties? properties = null)
  46. {
  47. _Bus.RPC.Respond<DataMsg,T>((data,pro)=>
  48. {
  49. return func.Invoke(this, new EventArgs<object[]>(MsgTool.GetAnonyDatas(data.GetBytes()), Guid.Empty, this));
  50. }, properties == null ? $"{nameof(EventName)}={EventName}" : properties.PropertiesToselector() + $" and {nameof(EventName)}={EventName}");
  51. return Guid.Empty;
  52. }
  53. public Guid SubscripList(Func<object, EventArgs<object[]>, List<T>> func, Properties? properties = null)
  54. {
  55. _Bus.RPC.Respond<DataMsg, List<T>>((data, pro) =>
  56. {
  57. return func.Invoke(this, new EventArgs<object[]>(MsgTool.GetAnonyDatas(data.GetBytes()), Guid.Empty, this));
  58. }, properties == null ? $"{nameof(EventName)}={EventName}" : properties.PropertiesToselector() + $" and {nameof(EventName)}={EventName}");
  59. return Guid.Empty;
  60. }
  61. public void UnSubscrip(Guid guid)
  62. {
  63. }
  64. }
  65. }