示例#1
0
        private void ReceiveCallBack(IAsyncResult ar)
        {
            bool isError = false;

            //if (isConnect == false)
            //{
            //    Connect();
            //}

            if (isConnect)
            {
                try
                {
                    int len = client.EndReceive(ar);
                    if (len == 0)
                    {
                        Disconnect();
                        if (socketEvent != null)
                        {
                            socketEvent.ReceiveFailEvent(this);
                        }
                        isError = true;
                        return;
                    }
                    byte[] newMsgArr = new byte[len];
                    Buffer.BlockCopy(msgArr, 0, newMsgArr, 0, len);


                    dataPack.AddMsg(newMsgArr);

                    if (socketEvent != null)
                    {
                        socketEvent.ReceiveSuccessEvent(this);
                    }
                }
                catch (Exception e)
                {
                    isError = true;
                    //ToolClass.printInfo(e);
                    LogManger.Instance.Error(e);
                    if (socketEvent != null)
                    {
                        socketEvent.ReceiveFailEvent(this);
                    }
                }
                finally
                {
                    if (!isError)
                    {
                        client.BeginReceive(msgArr, 0, ToolClass.msgArrLen, SocketFlags.None, ReceiveCallBack, null);
                    }
                    else
                    {
                        Disconnect();
                    }
                }
            }
        }
示例#2
0
        private void ReceiveCallBack(IAsyncResult ar)
        {
            bool isError = false;

            try
            {
                int len = client.EndReceive(ar);
                if (len == 0)
                {
                    isError = true;
                    if (socketEvent != null)
                    {
                        socketEvent.ReceiveFailEvent(this);
                    }
                    return;
                }
                byte[] newMsgArr = new byte[len];
                Buffer.BlockCopy(msgArr, 0, newMsgArr, 0, len);

                DataPack.AddMsg(newMsgArr);
                if (socketEvent != null)
                {
                    socketEvent.ReceiveSuccessEvent(this);
                }
            }
            catch (Exception e)
            {
                LogManger.Instance.Error(e);
                isError = true;
                if (socketEvent != null)
                {
                    socketEvent.ReceiveFailEvent(this);
                }
            }
            finally
            {
                if (isError)
                {
                    Disconnect();
                }
                else
                {
                    BeginReceive();
                }
            }
        }