ClientMsgHandlerUdpDelegate.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommonNetwork_ICE.Common;
  6. using CommonLang.Protocol;
  7. using CommonNetwork_ICE.Util;
  8. using CommonNetwork_ICE.Session;
  9. using Slice;
  10. using CommonLang.Log;
  11. using CommonNetwork_ICE.Client;
  12. namespace CommonNetwork_ICE.Msg
  13. {
  14. /// <summary>
  15. /// ICE消息委托处理类:根据客户端消息接收类的委托,处理服务器发来的ICE网络消息
  16. /// </summary>
  17. internal class ClientMsgHandlerUdpDelegate : ClientMsgHandler
  18. {
  19. private static Logger log = LoggerFactory.GetLogger("ClientMsgHandlerUdpDelegate");
  20. public ClientMsgHandlerUdpDelegate(IceClientConnector iceClient, IceClientNetSession session)
  21. : base(iceClient, session)
  22. {
  23. }
  24. /// <summary>
  25. /// 收到数据包
  26. /// </summary>
  27. /// <param name="message">数据包</param>
  28. /// <param name="ip">发送方IP</param>
  29. /// <param name="port">发送方端口</param>
  30. public override void RecvData(int recvPort, TransMessage message, String ip, int port)
  31. {
  32. log.Trace("收到服务器发来数据,地址IP【" + ip + "】,端口【" + port + "】,编号:" + message.serial);
  33. //session.TotalRecvBytes += message.length;
  34. if (message.type == Constants.PACKET_HINGE && message.serial == iceClient.RecvLastSerial)
  35. {
  36. // 如果是之前已经收到的关键响应数据包,只发送响应确认
  37. try
  38. {
  39. iceClient.Connector.Sender.SendAck(recvPort, message.serial);
  40. }
  41. catch (Exception e)
  42. {
  43. log.Error(e.Message);
  44. session.onException(e);
  45. }
  46. return;
  47. }
  48. // 关键数据包发送响应
  49. if (message.type == Constants.PACKET_HINGE)
  50. {
  51. bool repeatMsg = true;
  52. if (message.serial > iceClient.RecvLastSerial)
  53. {
  54. iceClient.RecvLastSerial = message.serial;
  55. repeatMsg = false;
  56. }
  57. // 发送响应确认
  58. try
  59. {
  60. iceClient.Connector.Sender.SendAck(recvPort, message.serial);
  61. }
  62. catch (Exception e)
  63. {
  64. log.Error(e.Message);
  65. session.onException(e);
  66. }
  67. // 重发的消息不处理
  68. if (repeatMsg)
  69. {
  70. return;
  71. }
  72. }
  73. // 数据解码
  74. IMessage imessage = null;
  75. try
  76. {
  77. Codec.doDecode(message, out imessage);
  78. }
  79. catch (Exception e)
  80. {
  81. Env.ENV_ERR_CODE = Error.ERR_CODE_4;
  82. session.onException(e);
  83. return;
  84. }
  85. if (imessage == null)
  86. {
  87. return;
  88. }
  89. // 数据处理
  90. try
  91. {
  92. long startTickCount = Environment.TickCount;
  93. session.onReceive(imessage, message.length);
  94. long endTickCount = Environment.TickCount;
  95. long interval = endTickCount - startTickCount;
  96. if (interval > Env.MSG_PROC_TIME_OUT)
  97. {
  98. log.Info("客户端消息处理超时,用时【" + interval + "】,消息编号【" + imessage.GetType() + "】,接收端口【" + recvPort + "】");
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. log.Error(e.Message);
  104. Env.ENV_ERR_CODE = Error.ERR_CODE_5;
  105. session.onException(e);
  106. }
  107. }
  108. }
  109. }