MessageQueue.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. namespace UniversalNetFrame451.IO;
  2. public class MessageQueue : BaseFilter, IDisposable
  3. {
  4. private EventQueue<Data> _sendQueue;
  5. private EventQueue<Data> _receiveQueue;
  6. public bool Initialize()
  7. {
  8. if (this._sendQueue is not null || this._receiveQueue is not null)
  9. return false;
  10. this._sendQueue = new(SendQueueHandler);
  11. this._receiveQueue = new(ReceivedQueueHandler);
  12. return true;
  13. }
  14. void IDisposable.Dispose()
  15. {
  16. this._receiveQueue.Cancel();
  17. this._receiveQueue = null;
  18. this._sendQueue.Cancel();
  19. this._sendQueue = null;
  20. }
  21. public override bool Send(Data data)
  22. {
  23. if (_sendQueue is null)
  24. return false;
  25. this._sendQueue?.Enqueue(data);
  26. return true;
  27. }
  28. public override bool Receive(Data data)
  29. {
  30. if (this._receiveQueue is null)
  31. return false;
  32. this._receiveQueue?.Enqueue(data);
  33. return true;
  34. }
  35. private void SendQueueHandler(Data data)
  36. {
  37. base.Send(data);
  38. }
  39. private void ReceivedQueueHandler(Data data)
  40. {
  41. base.Receive(data);
  42. }
  43. }
  44. public class MessageReceiveQueue : BaseFilter, IDisposable
  45. {
  46. private EventQueue<Data> _receiveQueue;
  47. public bool Initialize()
  48. {
  49. if ( this._receiveQueue is not null)
  50. return false;
  51. this._receiveQueue = new(ReceivedQueueHandler);
  52. return true;
  53. }
  54. void IDisposable.Dispose()
  55. {
  56. this._receiveQueue.Cancel();
  57. this._receiveQueue = null;
  58. }
  59. public override bool Receive(Data data)
  60. {
  61. if (this._receiveQueue is null)
  62. return false;
  63. this._receiveQueue?.Enqueue(data);
  64. return true;
  65. }
  66. private void ReceivedQueueHandler(Data data)
  67. {
  68. base.Receive(data);
  69. }
  70. }
  71. public class MessageSendQueue : BaseFilter, IDisposable
  72. {
  73. private EventQueue<Data> _sendQueue;
  74. public bool Initialize()
  75. {
  76. if (this._sendQueue is not null )
  77. return false;
  78. this._sendQueue = new(SendQueueHandler);
  79. return true;
  80. }
  81. void IDisposable.Dispose()
  82. {
  83. this._sendQueue.Cancel();
  84. this._sendQueue = null;
  85. }
  86. public override bool Send(Data data)
  87. {
  88. if (_sendQueue is null)
  89. return false;
  90. this._sendQueue?.Enqueue(data);
  91. return true;
  92. }
  93. private void SendQueueHandler(Data data)
  94. {
  95. base.Send(data);
  96. }
  97. }