// 逻辑处理相关 public void Tick() { KMessage[] arMsg = null; lock (m_lMessageWait) { if (m_lMessageWait.Count > 0) { arMsg = new KMessage[m_lMessageWait.Count]; m_lMessageWait.CopyTo(arMsg, 0); m_lMessageWait.Clear(); } } if (arMsg == null) { return; } foreach (KMessage msg in arMsg) { switch (msg.ID) { case KMessage.KMessageType.KMessageType_ConnectCallback: { KMessageOnConnectCallback msgConnectcallback = (KMessageOnConnectCallback)msg; if (m_Sock.Connected) { // 连接成功 m_iState = KClientNetState.KClientNet_Connected; // 开始接收操作 try { AsyncCallback callBack = new AsyncCallback(OnRecvCallback); m_Sock.BeginReceive(m_recvBuf, 0, m_iRecvBufLen, SocketFlags.None, callBack, this); } catch (SocketException e) { string strRes = String.Format("{0}:{1} ErrorCode={2}", e.ToString(), e.Message, e.ErrorCode); LogManager.LogError("KClientNet", strRes); KMessageOnError msgE = new KMessageOnError(); msgE.m_iRez = KErrorCode.KERROR_RECV_FAILED; msgE.m_iWsaRez = e.ErrorCode; PushMessage(msgE); } catch { string strRes = String.Format("error"); LogManager.LogError("KClientNet", strRes); KMessageOnError msgE = new KMessageOnError(); msgE.m_iRez = KErrorCode.KERROR_RECV_FAILED; PushMessage(msgE); } } OnConnectEnd(msgConnectcallback.m_iRez); } break; case KMessage.KMessageType.KMessageType_OnError: { // 发生了异常 KMessageOnError msgError = (KMessageOnError)msg; OnError(msgError.m_iRez, msgError.m_iWsaRez); } break; case KMessage.KMessageType.KMessageType_OnData: { KMessageOnData msgOndata = (KMessageOnData)msg; KMessageMgr.OnProtocolExecute(msgOndata.m_iMsgId, msgOndata.m_content); } break; } } }
static void OnRecvCallback(IAsyncResult ar) { KClientNet clientNet = (KClientNet)ar.AsyncState; AsyncCallback callBack = new AsyncCallback(OnRecvCallback); try { int byterecv = clientNet.m_Sock.EndReceive(ar); if (byterecv <= 0) { // 连接断开 KMessageOnError msg = new KMessageOnError(); msg.m_iRez = KErrorCode.KERROR_DISCONNECT_BYREMOTE; clientNet.PushMessage(msg); return; } // 进行消息处理 clientNet.m_iRecvedLen = clientNet.m_iRecvedLen + byterecv; while (clientNet.m_iRecvedLen >= m_iMessageHead && clientNet.m_iRecvedLen >= BitConverter.ToUInt16(clientNet.m_recvBuf, 0)) { // 长度大于2字节长度+2字节ID,并且大于包长 UInt16 iLen = BitConverter.ToUInt16(clientNet.m_recvBuf, 0); if (iLen < m_iMessageHead) { // 报文异常 throw new Exception(); } //进行报文处理 UInt16 iId = BitConverter.ToUInt16(clientNet.m_recvBuf, 2); byte[] data = new byte[iLen - 4]; Array.Copy(clientNet.m_recvBuf, 4, data, 0, iLen - 4); Array.Copy(clientNet.m_recvBuf, iLen, clientNet.m_recvBuf, 0, clientNet.m_iRecvedLen - iLen); clientNet.m_iRecvedLen -= iLen; if (!KMessageMgr.ValidMessage(iId)) { // pdl错误 KMessageOnError msg = new KMessageOnError(); msg.m_iRez = KErrorCode.KERROR_RECV_ID_ERROR; clientNet.PushMessage(msg); break; } KMessageOnData msgOnData = new KMessageOnData(); msgOnData.m_iMsgId = iId; msgOnData.m_content = data; clientNet.PushMessage(msgOnData); } // 开始接收操作 clientNet.m_Sock.BeginReceive(clientNet.m_recvBuf, clientNet.m_iRecvedLen, m_iRecvBufLen - clientNet.m_iRecvedLen, SocketFlags.None, callBack, clientNet); } catch (SocketException e) { string strRes = String.Format("{0}:{1} ErrorCode={2}", e.ToString(), e.Message, e.ErrorCode); LogManager.LogError("KClientNet", strRes); switch (e.ErrorCode) { case 10060: // 连接超时 //msg.m_iRez = KConnectRet.KCALLBACK_TIMEOUT; break; case 10051: // 目标不可达(本地网络没有目标网络路由) //msg.m_iRez = KConnectRet.KCALLBACK_CONNECTFAILED; break; case 10061: // 目标拒绝连接,一般是端口没有开启,端口错误 //msg.m_iRez = KConnectRet.KCALLBACK_REFUSED; break; default: // 连接异常 //msg.m_iRez = KConnectRet.KCALLBACK_CONNECTFAILED; break; } KMessageOnError msg = new KMessageOnError(); msg.m_iWsaRez = e.ErrorCode; msg.m_iRez = KErrorCode.KERROR_RECV_CALLBACKFAILED; clientNet.PushMessage(msg); } catch { string strRes = String.Format("error"); LogManager.LogError("KClientNet", strRes); KMessageOnError msg = new KMessageOnError(); msg.m_iRez = KErrorCode.KERROR_RECV_CALLBACKFAILED; clientNet.PushMessage(msg); } }