MessageHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using NettyClient;
  2. using Message = Cn.Sagacloud.Proto.Message;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using NettyClient.proto;
  11. namespace Client
  12. {
  13. class MessageHandler
  14. {
  15. private SimpleMessageHandler simpleHandler;
  16. private TaskHandler taskHandler;
  17. private bool isPauseTask = false;
  18. private int maxTaskCount;
  19. public MessageHandler(SimpleMessageHandler simpleHandler, int maxTaskCount, TaskHandler taskHandler) {
  20. this.simpleHandler = simpleHandler;
  21. this.maxTaskCount = maxTaskCount;
  22. this.taskHandler = taskHandler;
  23. }
  24. public void setSimpleMessageHandler(SimpleMessageHandler simpleHandler) {
  25. this.simpleHandler = simpleHandler;
  26. }
  27. public void HandleMessage(object obj) {
  28. while (true) {
  29. Message message;
  30. bool hasMsg = SimpleMessageHandler.messageQueue.TryDequeue(out message);
  31. if (!hasMsg) {
  32. Thread.Sleep(500);
  33. continue;
  34. }
  35. Command command = Command.Useless;
  36. try
  37. {
  38. command = (Command)Enum.Parse(typeof(Command), message.Cmd);
  39. }
  40. catch { command = Command.Useless; }
  41. Message retMsg;
  42. switch (command) {
  43. case Command.SendTask:
  44. // 1. 检测是否接受该任务
  45. if (checkIsAcceptTask(message.TaskId))
  46. {
  47. // 2. 如果接受, 保存任务到数据库, 并同步该任务到内存, 并交由任务执行线程开始执行
  48. bool isSuccess = taskHandler.addOneTask(message.TaskId, message.Content);
  49. if (isSuccess)
  50. {
  51. retMsg = MessageUtil.generateMessageStr(Command.AcceptTask.ToString(), message.TaskId, "");
  52. simpleHandler.WriteMessage(retMsg);
  53. }
  54. }
  55. else {
  56. // 3. 如果拒绝, 直接返回拒绝消息
  57. retMsg = MessageUtil.generateMessageStr(Command.RefuseTask.ToString(), message.TaskId, "");
  58. simpleHandler.WriteMessage(retMsg);
  59. }
  60. break;
  61. case Command.ClientInfo: // 服务端要求客户端返回客户端信息
  62. ClientInfo info = new ClientInfo();
  63. info.Ipv4 = ClientInfoUtil.GetClientLocalIPv4Address();
  64. info.MacAddr = ClientInfoUtil.GetMacAddress();
  65. info.Name = ClientInfoUtil.GetUserName();
  66. retMsg = MessageUtil.generateMessageStr(Command.ClientInfo.ToString(), "", JsonConvert.SerializeObject(info, Formatting.None));
  67. simpleHandler.WriteMessage(retMsg);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. }
  74. private bool checkIsAcceptTask(string taskId)
  75. {
  76. //一. isPauseTask == true的时候拒绝任务, 二. 如果已存在该任务, 则拒绝, 三. 如果当前任务数达到maxTaskCount, 拒绝
  77. if (isPauseTask)
  78. return false;
  79. if (taskHandler.isContainTask(taskId)) {
  80. return false;
  81. }
  82. if (taskHandler.taskModels.Count >= maxTaskCount)
  83. return false;
  84. return true;
  85. }
  86. internal void PauseAllTasks()
  87. {
  88. isPauseTask = true;
  89. }
  90. internal void ResumeAllTasks()
  91. {
  92. isPauseTask = false;
  93. }
  94. }
  95. enum Command
  96. {
  97. SendTask,
  98. RefuseTask,
  99. AcceptTask,
  100. DownloadError,
  101. CommandError,
  102. TaskSuccess,
  103. ClientInfo,
  104. Useless
  105. }
  106. }