MessageQueue.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. namespace Universal.IO;
  2. public class MessageQueue : BaseFilter, IDisposable
  3. {
  4. private EventQueue<Data>? _receiveQueue;
  5. private EventQueue<Data>? _sendQueue;
  6. private bool disposedValue;
  7. public bool Initialize()
  8. {
  9. if (this._receiveQueue is not null)
  10. return false;
  11. if (this._sendQueue is not null)
  12. return false;
  13. this._receiveQueue = new(ReceivedQueueHandler);
  14. this._sendQueue = new(SendQueueHandler);
  15. return true;
  16. }
  17. public override bool Send(Data data)
  18. {
  19. if (_sendQueue is null)
  20. return false;
  21. this._sendQueue?.Enqueue(data);
  22. return true;
  23. }
  24. public override bool Receive(Data data)
  25. {
  26. if (this._receiveQueue is null)
  27. return false;
  28. this._receiveQueue?.Enqueue(data);
  29. return true;
  30. }
  31. private void SendQueueHandler(Data data)
  32. {
  33. base.Send(data);
  34. }
  35. private void ReceivedQueueHandler(Data data)
  36. {
  37. base.Receive(data);
  38. }
  39. protected virtual void Dispose(bool disposing)
  40. {
  41. if (!disposedValue)
  42. {
  43. if (disposing)
  44. {
  45. this._receiveQueue?.Dispose();
  46. this._sendQueue?.Dispose();
  47. this._receiveQueue = null;
  48. this._sendQueue = null;
  49. }
  50. disposedValue = true;
  51. }
  52. }
  53. public void Dispose()
  54. {
  55. Dispose(disposing: true);
  56. GC.SuppressFinalize(this);
  57. }
  58. }
  59. public class MessageReceiveQueue : BaseFilter, IDisposable
  60. {
  61. private EventQueue<Data>? _receiveQueue;
  62. private bool disposedValue;
  63. public bool Initialize()
  64. {
  65. if (this._receiveQueue is not null)
  66. return false;
  67. this._receiveQueue = new(ReceivedQueueHandler);
  68. return true;
  69. }
  70. public override bool Receive(Data data)
  71. {
  72. if (this._receiveQueue is null)
  73. return false;
  74. this._receiveQueue?.Enqueue(data);
  75. return true;
  76. }
  77. private void ReceivedQueueHandler(Data data)
  78. {
  79. base.Receive(data);
  80. }
  81. protected virtual void Dispose(bool disposing)
  82. {
  83. if (!disposedValue)
  84. {
  85. if (disposing)
  86. {
  87. this._receiveQueue?.Dispose();
  88. this._receiveQueue = null;
  89. }
  90. disposedValue = true;
  91. }
  92. }
  93. public void Dispose()
  94. {
  95. Dispose(disposing: true);
  96. GC.SuppressFinalize(this);
  97. }
  98. }
  99. public class MessageSendQueue : BaseFilter, IDisposable
  100. {
  101. private EventQueue<Data>? _sendQueue;
  102. private bool disposedValue;
  103. public bool Initialize()
  104. {
  105. if (this._sendQueue is not null)
  106. return false;
  107. this._sendQueue = new(SendQueueHandler);
  108. return true;
  109. }
  110. //void IDisposable.Dispose()
  111. //{
  112. // this._sendQueue?.Dispose();
  113. // this._sendQueue = null;
  114. //}
  115. public override bool Send(Data data)
  116. {
  117. if (_sendQueue is null)
  118. return false;
  119. this._sendQueue?.Enqueue(data);
  120. return true;
  121. }
  122. private void SendQueueHandler(Data data)
  123. {
  124. base.Send(data);
  125. }
  126. protected virtual void Dispose(bool disposing)
  127. {
  128. if (!disposedValue)
  129. {
  130. if (disposing)
  131. {
  132. this._sendQueue?.Dispose();
  133. this._sendQueue = null;
  134. }
  135. disposedValue = true;
  136. }
  137. }
  138. public void Dispose()
  139. {
  140. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  141. Dispose(disposing: true);
  142. GC.SuppressFinalize(this);
  143. }
  144. }