Exemplo n.º 1
0
        static void OnSendCallback(IAsyncResult ar)
        {
            KClientNet clientNet = (KClientNet)ar.AsyncState;

            try
            {
                clientNet.m_Sock.EndSend(ar);
            }
            catch (SocketException e)
            {
                string strRes = "SendFailed";
                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_iRez    = KErrorCode.KERROR_SEND_CALLBACKFAILED;
                msg.m_iWsaRez = e.ErrorCode;
                clientNet.PushMessage(msg);
            }
            catch
            {
                string strRes = "SendFailed";
                strRes = String.Format("error");
                LogManager.LogError("KClientNet", strRes);

                KMessageOnError msg = new KMessageOnError();
                msg.m_iRez = KErrorCode.KERROR_SEND_CALLBACKFAILED;
                clientNet.PushMessage(msg);
            }
        }
Exemplo n.º 2
0
        static void OnConnectCallback(IAsyncResult ar)
        {
            KClientNet clientNet          = (KClientNet)ar.AsyncState;
            string     strRes             = "connected";
            KMessageOnConnectCallback msg = new KMessageOnConnectCallback();

            try
            {
                msg.m_iRez = KConnectRet.KCALLBACK_SUCCESS;
                clientNet.m_Sock.EndConnect(ar);
            }
            catch (SocketException e)
            {
                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;
                }
            }
            catch
            {
                strRes = String.Format("error");
                LogManager.LogError("KClientNet", strRes);
                msg.m_iRez = KConnectRet.KCALLBACK_CONNECTFAILED;
            }

            clientNet.PushMessage(msg);
        }
Exemplo n.º 3
0
        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);
            }
        }