ClientMessageQueue.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using CommonNetwork_ICE.Util;
  7. using CommonNetwork_ICE.Common;
  8. using Slice;
  9. using CommonLang.Log;
  10. namespace CommonNetwork_ICE.Msg
  11. {
  12. /// <summary>
  13. /// 客户端消息包循环发送处理类
  14. /// </summary>
  15. public class ClientMessageQueue
  16. {
  17. private static Logger log = LoggerFactory.GetLogger("ClientMessageQueue");
  18. // 待发送消息队列
  19. private static Queue<TransMessage> queue = new Queue<TransMessage>();
  20. // 消息队列绑定线程
  21. private Thread thread;
  22. // 是否运行线程
  23. private bool runFlag;
  24. // 发送的数据包序列号
  25. private long sentSerial;
  26. // 超时时间
  27. private int timeout = Env.SEND_WAIT_ACK_TIME_OUT;
  28. public ClientMessageQueue()
  29. {
  30. this.sentSerial = 0;
  31. this.LastSerial = 0;
  32. }
  33. // 发送接口
  34. public IceConnector Connector { get; set; }
  35. // 服务器返回的数据包序列号
  36. public long LastSerial { set; get; }
  37. public void Start()
  38. {
  39. thread = new Thread(Run);
  40. runFlag = true;
  41. thread.Start();
  42. }
  43. public void End()
  44. {
  45. runFlag = false;
  46. if (thread != null)
  47. {
  48. thread.Join();
  49. thread.Abort();
  50. }
  51. }
  52. /// <summary>
  53. /// 加入新消息
  54. /// </summary>
  55. /// <param name="message"></param>
  56. public void AddMessage(TransMessage message)
  57. {
  58. queue.Enqueue(message);
  59. }
  60. /// <summary>
  61. /// 消息发送
  62. /// </summary>
  63. private void Run()
  64. {
  65. TransMessage preMessage = null;
  66. DateTime preDateTime = DateTime.Now;
  67. while (runFlag || queue.Count > 0)
  68. {
  69. try
  70. {
  71. // 当前发送数据包的应答序号号未收到
  72. if (sentSerial != LastSerial)
  73. {
  74. // 是否在发送超时时间内
  75. if (preDateTime.AddSeconds(timeout) < DateTime.Now)
  76. {
  77. if (preMessage != null)
  78. {
  79. try
  80. {
  81. Connector.Sender.SendData(Connector.GetConnectorConfig().LocalPort, preMessage);
  82. }
  83. catch (Exception e)
  84. {
  85. log.Error("客户端重发数据错误:" + e.Message);
  86. continue;
  87. }
  88. log.Trace("没收到关键包应答,客户端重发数据:" + preMessage.serial);
  89. preDateTime = DateTime.Now;
  90. }
  91. continue;
  92. }
  93. else
  94. {
  95. log.Trace("没收到关键包应答,等待当前序列号数据包:" + sentSerial);
  96. Thread.Sleep(Env.SEND_WAIT_ACT_THREAD_SLEEP_TIME);
  97. continue;
  98. }
  99. }
  100. if (queue.Count > 0)
  101. {
  102. // 发送数据包
  103. TransMessage outMessage = queue.Dequeue();
  104. try
  105. {
  106. Connector.Sender.SendData(Connector.GetConnectorConfig().LocalPort, outMessage);
  107. }
  108. catch (Exception e)
  109. {
  110. log.Error("消息循环发送数据错误:" + e.Message);
  111. continue;
  112. }
  113. preDateTime = DateTime.Now;
  114. // 如果是关键数据包,记录序列号,进入下一个发送周期
  115. if (outMessage.type == Constants.PACKET_HINGE)
  116. {
  117. sentSerial = outMessage.serial;
  118. preMessage = outMessage;
  119. }
  120. }
  121. }
  122. catch (Exception err)
  123. {
  124. log.Error(err.Message, err);
  125. }
  126. }
  127. }
  128. }
  129. }