NotificationQueueManager.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Concurrent;
  3. using WingServerCommon.Log;
  4. using WingServerCommon.Utilities.Executors;
  5. namespace WingNotificationModule
  6. {
  7. internal partial class NotificationServer
  8. {
  9. internal class NotificationQueueManager
  10. {
  11. const string DefaultMessageQueueId = "DefaultMessageQueue";
  12. private readonly ConcurrentDictionary<string, SequenceExecutor<PostMessageParam>> _notifyExecutorCollection = new ConcurrentDictionary<string, SequenceExecutor<PostMessageParam>>();
  13. private readonly Func<PostMessageParam, bool> _sendMessage;
  14. private int _maxQueueCount;
  15. public NotificationQueueManager(int maxQueueCount, Func<PostMessageParam, bool> sendMessage)
  16. {
  17. _sendMessage = sendMessage;
  18. _maxQueueCount = maxQueueCount;
  19. _notifyExecutorCollection.TryAdd(DefaultMessageQueueId, new SequenceExecutor<PostMessageParam>(DefaultMessageQueueId));
  20. }
  21. /// <summary>
  22. /// Try add a msg pushing queue
  23. /// </summary>
  24. public bool TryAdd(string msgQueueId)
  25. {
  26. //TODO define some rules
  27. if (_maxQueueCount <= _notifyExecutorCollection.Count)
  28. {
  29. return false;
  30. }
  31. return _notifyExecutorCollection.TryAdd(msgQueueId, new SequenceExecutor<PostMessageParam>(msgQueueId));
  32. }
  33. /// <summary>
  34. /// Try remove a msg pushing queue
  35. /// </summary>
  36. public bool TryRemove(string msgQueueId)
  37. {
  38. //TODO define some rules
  39. return _notifyExecutorCollection.TryRemove(msgQueueId, out _);
  40. }
  41. /// <summary>
  42. /// Post
  43. /// </summary>
  44. /// <param name="msgQueueId"></param>
  45. /// <param name="messageParam"></param>
  46. public void Post(string msgQueueId, PostMessageParam messageParam)
  47. {
  48. Logger.WriteLineInfo($"Post,msgQueueId:{msgQueueId},Token:{messageParam.Token},WSConnectType:{messageParam.WSConnectType},Message:{messageParam.Message}");
  49. SequenceExecutor<PostMessageParam> postMsgExecutor = null;
  50. if (!string.IsNullOrWhiteSpace(msgQueueId) && _notifyExecutorCollection.TryGetValue(msgQueueId, out postMsgExecutor) && postMsgExecutor != null)
  51. {
  52. postMsgExecutor.Add(_sendMessage, messageParam);
  53. Logger.WriteLineInfo($"postMsgExecutor.Add,, WorkerCount:{postMsgExecutor.WorkerCount},GetExecutingStatus:{postMsgExecutor.GetExecutingStatus},IsClosed:{postMsgExecutor.IsClosed},msgQueueId:{msgQueueId},Token:{messageParam.Token},WSConnectType:{messageParam.WSConnectType},Message:{messageParam.Message}");
  54. }
  55. else if (_notifyExecutorCollection.TryGetValue(DefaultMessageQueueId, out postMsgExecutor) && postMsgExecutor != null)
  56. {
  57. postMsgExecutor.Add(_sendMessage, messageParam);
  58. Logger.WriteLineInfo($"postMsgExecutor.Add, WorkerCount:{postMsgExecutor.WorkerCount},GetExecutingStatus:{postMsgExecutor.GetExecutingStatus},IsClosed:{postMsgExecutor.IsClosed},msgQueueId:{msgQueueId},Token:{messageParam.Token},WSConnectType:{messageParam.WSConnectType},Message:{messageParam.Message}");
  59. }
  60. }
  61. }
  62. }
  63. }