Exemplo n.º 1
0
        /// <summary>
        /// 先接收到了数据头后,再调用接受数据身子,获得完整的数据
        /// </summary>
        /// <param name="ar"></param>
        private void RcvBodyData(IAsyncResult ar)
        {
            try
            {
                PEPkg pack = (PEPkg)ar.AsyncState;
                int   len  = skt.EndReceive(ar);
                if (len > 0)
                {
                    pack.bodyIndex += len;
                    //同样,如果接收到的数据长度比目标数据长度要短,则继续接受直到数据完整
                    if (pack.bodyIndex < pack.bodyLen)
                    {
                        skt.BeginReceive(pack.bodyBuff,
                                         pack.bodyIndex,
                                         pack.bodyLen - pack.bodyIndex,
                                         SocketFlags.None,
                                         new AsyncCallback(RcvBodyData),
                                         pack);
                    }
                    else
                    {
                        //反序列化数据
                        T msg = PETool.DeSerialize <T>(pack.bodyBuff);
                        OnReciveMsg(msg);

                        //loop recive,重置pack的设置后,继续开始接受数据,直到关闭连接
                        pack.ResetData();
                        skt.BeginReceive(
                            pack.headBuff,
                            0,
                            pack.headLen,
                            SocketFlags.None,
                            new AsyncCallback(RcvHeadData),
                            pack);
                    }
                }
                //同样,防止空数据,要关闭连接
                else
                {
                    OnDisConnected();
                    Clear();
                }
            }
            catch (Exception e)
            {
                PETool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error);
            }
        }
Exemplo n.º 2
0
        private void RcvBodyData(IAsyncResult ar)
        {
            try {
                PEPkg pack = (PEPkg)ar.AsyncState;
                int   len  = skt.EndReceive(ar);
                if (len > 0)
                {
                    pack.bodyIndex += len;
                    if (pack.bodyIndex < pack.bodyLen)
                    {
                        skt.BeginReceive(pack.bodyBuff,
                                         pack.bodyIndex,
                                         pack.bodyLen - pack.bodyIndex,
                                         SocketFlags.None,
                                         new AsyncCallback(RcvBodyData),
                                         pack);
                    }
                    else
                    {
                        T msg = PETool.DeSerialize <T>(pack.bodyBuff);
                        OnReciveMsg(msg);

                        //loop recive
                        pack.ResetData();
                        skt.BeginReceive(
                            pack.headBuff,
                            0,
                            pack.headLen,
                            SocketFlags.None,
                            new AsyncCallback(RcvHeadData),
                            pack);
                    }
                }
                else
                {
                    OnDisConnected();
                    Clear();
                }
            }
            catch (Exception e) {
                PETool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error);
            }
        }