123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CommonNetwork_ICE.Common;
- using CommonLang.Protocol;
- using CommonNetwork_ICE.Util;
- using CommonNetwork_ICE.Session;
- using Slice;
- using CommonLang.Log;
- using CommonNetwork_ICE.Client;
- namespace CommonNetwork_ICE.Msg
- {
- /// <summary>
- /// ICE消息委托处理类:根据客户端消息接收类的委托,处理服务器发来的ICE网络消息
- /// </summary>
- internal class ClientMsgHandlerUdpDelegate : ClientMsgHandler
- {
- private static Logger log = LoggerFactory.GetLogger("ClientMsgHandlerUdpDelegate");
- public ClientMsgHandlerUdpDelegate(IceClientConnector iceClient, IceClientNetSession session)
- : base(iceClient, session)
- {
- }
- /// <summary>
- /// 收到数据包
- /// </summary>
- /// <param name="message">数据包</param>
- /// <param name="ip">发送方IP</param>
- /// <param name="port">发送方端口</param>
- public override void RecvData(int recvPort, TransMessage message, String ip, int port)
- {
- log.Trace("收到服务器发来数据,地址IP【" + ip + "】,端口【" + port + "】,编号:" + message.serial);
- //session.TotalRecvBytes += message.length;
- if (message.type == Constants.PACKET_HINGE && message.serial == iceClient.RecvLastSerial)
- {
- // 如果是之前已经收到的关键响应数据包,只发送响应确认
- try
- {
- iceClient.Connector.Sender.SendAck(recvPort, message.serial);
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- session.onException(e);
- }
- return;
- }
- // 关键数据包发送响应
- if (message.type == Constants.PACKET_HINGE)
- {
- bool repeatMsg = true;
- if (message.serial > iceClient.RecvLastSerial)
- {
- iceClient.RecvLastSerial = message.serial;
- repeatMsg = false;
- }
- // 发送响应确认
- try
- {
- iceClient.Connector.Sender.SendAck(recvPort, message.serial);
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- session.onException(e);
- }
- // 重发的消息不处理
- if (repeatMsg)
- {
- return;
- }
- }
- // 数据解码
- IMessage imessage = null;
- try
- {
- Codec.doDecode(message, out imessage);
- }
- catch (Exception e)
- {
- Env.ENV_ERR_CODE = Error.ERR_CODE_4;
- session.onException(e);
- return;
- }
- if (imessage == null)
- {
- return;
- }
- // 数据处理
- try
- {
- long startTickCount = Environment.TickCount;
- session.onReceive(imessage, message.length);
- long endTickCount = Environment.TickCount;
- long interval = endTickCount - startTickCount;
- if (interval > Env.MSG_PROC_TIME_OUT)
- {
- log.Info("客户端消息处理超时,用时【" + interval + "】,消息编号【" + imessage.GetType() + "】,接收端口【" + recvPort + "】");
- }
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- Env.ENV_ERR_CODE = Error.ERR_CODE_5;
- session.onException(e);
- }
- }
- }
- }
|