コード例 #1
0
ファイル: ZChannel.cs プロジェクト: zhangandding/dp2
        bool DoIdle()
        {
            if (CommIdle == null)
            {
                return(false);
            }

            CommIdleEventArgs e = new CommIdleEventArgs();

            this.CommIdle(this, e);
            if (e.Cancel == true)
            {
                return(true);
            }

            return(false);
        }
コード例 #2
0
ファイル: ZChannel.cs プロジェクト: zhangandding/dp2
        // 接收响应包
        public int RecvTcpPackage(out byte[] baPackage,
                                  out int nLen,
                                  out string strError)
        {
            strError = "";

            int  nInLen;
            int  wRet        = 0;
            bool bInitialLen = false;

            Debug.Assert(client != null, "client为空");

            baPackage = new byte[4096];
            nInLen    = 0;
            nLen      = 4096; //COMM_BUFF_LEN;

            long lIdleCount = 0;

            while (nInLen < nLen)
            {
                if (client != null)
                {
                    /*
                     * if (client.Client.Poll(-1, SelectMode.SelectError))
                     * {
                     *  strError = "通讯中断0";
                     *  goto ERROR1;
                     * }
                     *
                     *
                     * if (client.Client.Connected == false)
                     * {
                     *  strError = "通讯中断0";
                     *  goto ERROR1;
                     * }
                     * */
                }

                if (CommIdle != null &&
                    client != null &&
                    lIdleCount < 20)
                {
                    int nCount = 0;

                    try
                    {
                        nCount = client.Available;
                    }
                    catch (SocketException ex)
                    {
                        if (ex.ErrorCode == 0)
                        {
                            strError = "通讯中断0";
                            goto ERROR1;
                        }
                    }
                    if (nCount == 0)
                    {
                        CommIdleEventArgs e = new CommIdleEventArgs();
                        this.CommIdle(this, e);
                        if (e.Cancel == true)
                        {
                            strError = e.ErrorInfo;
                            goto ERROR1;
                        }
                        System.Threading.Thread.Sleep(10);
                        lIdleCount++;
                        continue;
                    }
                }

                if (client == null)
                {
                    strError = "通讯中断";
                    goto ERROR1;
                }

                try
                {
                    wRet = client.GetStream().Read(baPackage,
                                                   nInLen,
                                                   baPackage.Length - nInLen);

                    lIdleCount = 0;

                    /*
                     * wRet = client.Client.Receive(baPackage,
                     *  nInLen,
                     *  SocketFlags.None);
                     * */
                }
                catch (SocketException ex)
                {
                    if (ex.ErrorCode == 10035)
                    {
                        CommIdleEventArgs e = new CommIdleEventArgs();
                        this.CommIdle(this, e);
                        if (e.Cancel == true)
                        {
                            strError = e.ErrorInfo;
                            goto ERROR1;
                        }
                        System.Threading.Thread.Sleep(100);
                        continue;
                    }
                    strError = "recv出错: " + ex.Message;
                    goto ERROR1;
                }
                catch (Exception ex)
                {
                    strError = "recv出错: " + ex.Message;
                    goto ERROR1;
                }

                if (wRet == 0)
                {
                    strError = "Closed by remote peer";
                    goto ERROR1;
                }

                // 得到包的长度

                if ((wRet >= 1 || nInLen >= 1) &&
                    bInitialLen == false)
                {
                    long remainder = 0;
                    bool bRet      = BerNode.IsCompleteBER(baPackage,
                                                           0,
                                                           nInLen + wRet,
                                                           out remainder);
                    if (bRet == true)
                    {
                        /*
                         * // 正式分配缓冲区尺寸
                         * byte[] temp = new byte[nLen];
                         * Array.Copy(baPackage, 0, temp, 0, nInLen + wRet);
                         * baPackage = temp;
                         *
                         * bInitialLen = true;
                         * */
                        nLen = nInLen + wRet;
                        break;
                    }
                }

                nInLen += wRet;
                if (nInLen >= baPackage.Length &&
                    bInitialLen == false)
                {
                    // 扩大缓冲区
                    byte[] temp = new byte[baPackage.Length + 4096];
                    Array.Copy(baPackage, 0, temp, 0, nInLen);
                    baPackage = temp;
                    nLen      = baPackage.Length;
                }
            }

            // 最后规整缓冲区尺寸,如果必要的话
            if (baPackage.Length > nLen)
            {
                byte[] temp = new byte[nLen];
                Array.Copy(baPackage, 0, temp, 0, nLen);
                baPackage = temp;
            }

            return(0);

ERROR1:
            this.CloseSocket();
            baPackage = null;
            return(-1);
        }