MessageHandler.cs 4.0 KB

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