Communication.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using EventBus;
  2. using ICommunication;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading.Tasks;
  8. namespace ActiveMQCommunication
  9. {
  10. [CommunicationType(false)]
  11. public class Communication : ICommunication.ICommunication
  12. {
  13. public string IP => bus == null ? "" : bus.Host;
  14. public int Port => bus == null ? 0 : bus.Port;
  15. public bool IsLan { get; } = false;
  16. private readonly Dictionary<string, List<IBaseEventData>> _list = new Dictionary<string, List<IBaseEventData>>();
  17. string GetHash()
  18. {
  19. return DateTime.Now.Ticks.ToString();
  20. }
  21. EasyMQ.IBus bus;
  22. public bool IsConnected { get; private set; }
  23. public bool IsService{ get; private set; }
  24. public event EventHandler Connected;
  25. public event EventHandler Disconnected;
  26. public void Close()
  27. {
  28. bus?.Dispose();
  29. IsConnected = false;
  30. }
  31. public void Connect(string ip, int port)
  32. {
  33. IsService = false;
  34. try
  35. {
  36. bus = EasyMQ.ActiveHutch.Default.CreateBus(ip, port, new EasyMQ.MessagePackSerializer());
  37. bus.ConnectionInterruptedListener += (_, _) =>
  38. {
  39. IsConnected = false;
  40. Disconnected?.Invoke(this, EventArgs.Empty);
  41. };
  42. IsConnected = true;
  43. }
  44. catch
  45. {
  46. IsConnected = false;
  47. }
  48. }
  49. public IEventData<TData>? GetEvent<TData>()
  50. {
  51. lock (_list)
  52. {
  53. string hash = GetHash();
  54. if (_list.TryGetValue(typeof(MQEventData<TData>).FullName!, out List<IBaseEventData>? baseevent))
  55. {
  56. baseevent ??= new List<IBaseEventData>();
  57. if (baseevent.Count == 0) baseevent.Add(new MQEventData<TData>(bus, hash, hash));
  58. return (IEventData<TData>)baseevent.First();
  59. }
  60. else
  61. {
  62. MQEventData<TData> eventData = new MQEventData<TData>(bus, hash, hash);
  63. _list.Add(typeof(MQEventData<TData>).FullName!, new List<IBaseEventData>() { eventData });
  64. return eventData;
  65. }
  66. }
  67. }
  68. public IEventData<TData, T>? GetEvent<TData, T>()
  69. {
  70. lock (_list)
  71. {
  72. string hash = GetHash();
  73. if (_list.TryGetValue(typeof(MQEventData<TData, T>).FullName!, out List<IBaseEventData>? baseevent))
  74. {
  75. baseevent ??= new List<IBaseEventData>();
  76. if (baseevent.Count == 0) baseevent.Add(new MQEventData<TData,T>(bus, hash, hash));
  77. return (IEventData<TData,T>)baseevent.First();
  78. }
  79. else
  80. {
  81. MQEventData<TData,T> eventData = new MQEventData<TData,T>(bus, hash, hash);
  82. _list.Add(typeof(MQEventData<TData, T>).FullName!, new List<IBaseEventData>() { eventData });
  83. return eventData;
  84. }
  85. }
  86. }
  87. public IAnonymousEventData? GetEvent(string eventName)
  88. {
  89. lock (_list)
  90. {
  91. string hash = GetHash();
  92. ArgumentNullException.ThrowIfNullOrEmpty(eventName);
  93. if (_list.TryGetValue(typeof(MQAnonymousEventData).FullName!, out List<IBaseEventData>? baseevent))
  94. {
  95. baseevent ??= new List<IBaseEventData>();
  96. if (!baseevent.Where(x => x is MQAnonymousEventData && x.EventName.Contains(eventName)).Any()) baseevent.Add(new MQAnonymousEventData(bus, eventName, GetHash()));
  97. return (IAnonymousEventData)baseevent.Where(x => x is MQAnonymousEventData && (x as MQAnonymousEventData)!.EventName.Contains(eventName)).First();
  98. }
  99. else
  100. {
  101. MQAnonymousEventData eventData = new MQAnonymousEventData(bus, eventName, GetHash());
  102. _list.Add(typeof(MQAnonymousEventData).FullName!, new List<IBaseEventData>() { eventData });
  103. return eventData;
  104. }
  105. }
  106. }
  107. public IAnonymousEventData<T>? GetEvent<T>(string eventName)
  108. {
  109. lock (_list)
  110. {
  111. string hash = GetHash();
  112. ArgumentNullException.ThrowIfNullOrEmpty(eventName);
  113. if (_list.TryGetValue(typeof(MQAnonymousEventData<T>).FullName!, out List<IBaseEventData>? baseevent))
  114. {
  115. baseevent ??= new List<IBaseEventData>();
  116. if (!baseevent.Where(x => x is MQAnonymousEventData<T> && x.EventName.Contains(eventName)).Any()) baseevent.Add(new MQAnonymousEventData<T>(bus, eventName, GetHash()));
  117. return (IAnonymousEventData<T>)baseevent.Where(x => x is MQAnonymousEventData<T> && (x as MQAnonymousEventData<T>)!.EventName.Contains(eventName)).First();
  118. }
  119. else
  120. {
  121. MQAnonymousEventData<T> eventData = new MQAnonymousEventData<T>(bus, eventName, GetHash());
  122. _list.Add(typeof(MQAnonymousEventData<T>).FullName!, new List<IBaseEventData>() { eventData });
  123. return eventData;
  124. }
  125. }
  126. }
  127. public void Start()
  128. {
  129. }
  130. public void StartService(string ip,int port)
  131. {
  132. IsService = true;
  133. try
  134. {
  135. bus = EasyMQ.ActiveHutch.Default.CreateBus(ip, port, new EasyMQ.MessagePackSerializer());
  136. bus.ConnectionInterruptedListener += (_, _) =>
  137. {
  138. IsConnected = false;
  139. Disconnected?.Invoke(this, EventArgs.Empty);
  140. };
  141. IsConnected = true;
  142. }
  143. catch
  144. {
  145. IsConnected = false;
  146. }
  147. }
  148. }
  149. }