Пример #1
0
 private void RcvHeadData(IAsyncResult ar)
 {
     try
     {
         IYPkg pack = (IYPkg)ar.AsyncState;
         if (skt.Available == 0)
         {
             OnDisConnected();
             Clear();
             return;
         }
         int len = skt.EndReceive(ar);
         if (len > 0)
         {
             pack.headIndex += len;
             if (pack.headIndex < pack.headLen)
             {
                 skt.BeginReceive(
                     pack.headBuff,
                     pack.headIndex,
                     pack.headLen - pack.headIndex,
                     SocketFlags.None,
                     new AsyncCallback(RcvHeadData),
                     pack);
             }
             else
             {
                 pack.InitBodyBuff();
                 skt.BeginReceive(pack.bodyBuff,
                                  0,
                                  pack.bodyLen,
                                  SocketFlags.None,
                                  new AsyncCallback(RcvBodyData),
                                  pack);
             }
         }
         else
         {
             OnDisConnected();
             Clear();
         }
     }
     catch (Exception e)
     {
         IYTool.LogMsg("RcvHeadError:" + e.Message, LogLevel.Error);
     }
 }
Пример #2
0
        private void RcvBodyData(IAsyncResult ar)
        {
            try
            {
                IYPkg pack = (IYPkg)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 = IYTool.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)
            {
                IYTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error);
            }
        }
Пример #3
0
        public void StartRcvData(Socket skt, Action closeCB)
        {
            try
            {
                this.skt     = skt;
                this.closeCB = closeCB;

                OnConnected();

                IYPkg pack = new IYPkg();
                skt.BeginReceive(
                    pack.headBuff,
                    0,
                    pack.headLen,
                    SocketFlags.None,
                    new AsyncCallback(RcvHeadData),
                    pack);
            }
            catch (Exception e)
            {
                IYTool.LogMsg("StartRcvData:" + e.Message, LogLevel.Error);
            }
        }