MessageQueue.cs 2.5 KB

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