TcpEventData{TData_T}.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using EventBus;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace TcpEventBus
  11. {
  12. public class TcpEventData<TData, T> :BaseResultSubscripData, IEventData<TData, T>
  13. {
  14. private AutoResetEvent _WaitResult = new AutoResetEvent(false);
  15. [AllowNull]
  16. private T _Result = default;
  17. public override void SetResultValue(byte[] result)
  18. {
  19. _Result = MsgTool.GetValue<T>(result);
  20. _WaitResult.Set();
  21. }
  22. public override void Subscrip(byte[] data, Properties properties)
  23. {
  24. throw new NotSupportedException();
  25. }
  26. public override void SubscripData(byte[] data, Properties properties)
  27. {
  28. lock (_list)
  29. {
  30. TData t = MsgTool.GetValue<TData>(data);
  31. var temp = _list.Where(x => x.Value.Properties.FilterRule(properties) && x.Value.HasDelegate).Select(x => x.Value).ToList();
  32. if (temp.Count == 0) return;
  33. var tempevent = temp.First();
  34. T result = tempevent.Invoke(this, t, this);
  35. var bytes = MsgTool.GetMsg(this, result, properties,true).GetBytes() ;
  36. _Socket.Send(bytes);
  37. }
  38. }
  39. public override bool IsAnonymous => false;
  40. ConcurrentDictionary<Guid, FuncValue<TData,T>> _list = new ConcurrentDictionary<Guid, FuncValue<TData,T>>();
  41. private ISocket _Socket;
  42. internal TcpEventData(ISocket socket,string eventName,string hash)
  43. {
  44. _Socket =socket;
  45. EventName = eventName;
  46. Hash = hash;
  47. }
  48. public override string EventName { get; }
  49. public string Hash { get; }
  50. public void Clear()
  51. {
  52. lock (_list)
  53. {
  54. _list.Clear();
  55. }
  56. }
  57. public T Publish(object sender, TData data, Properties? properties = null)
  58. {
  59. if (data == null || _Socket == null) return default;
  60. _Socket.Send(this.GetMsg(data, properties).GetBytes());
  61. _WaitResult.Reset();
  62. if (!_WaitResult.WaitOne(_Socket.ReceiveTimeout))
  63. {
  64. return default;
  65. }
  66. else return _Result;
  67. }
  68. public Task<T> PublishAsync(object sender, TData data, Properties? properties = null)
  69. {
  70. return Task<T>.Run(()=>
  71. {
  72. return Publish(sender, data, properties);
  73. });
  74. }
  75. public List<T> PublishList(object sender, TData data, Properties? properties = null)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. public Task<List<T>> PublishListAsync(object sender, TData data, Properties? properties = null)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. public Guid Subscrip(Func<object, EventArgs<TData>, T> func, Properties? properties = null)
  84. {
  85. lock (_list)
  86. {
  87. ArgumentNullException.ThrowIfNull(func);
  88. properties ??= Properties.Default;
  89. var temp = new FuncValue<TData, T>(func, properties);
  90. _list[temp.IntPtr] = temp;
  91. return temp.IntPtr;
  92. }
  93. }
  94. public Guid SubscripList(Func<object, EventArgs<TData>, List<T>> func, Properties? properties = null)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. public void UnSubscrip(Guid guid)
  99. {
  100. lock (_list)
  101. {
  102. if (guid == Guid.Empty) return;
  103. _list.TryRemove(guid, out _);
  104. }
  105. }
  106. }
  107. }