Exemplo n.º 1
0
 public void StartReceiveAsync()
 {
     try
     {
         bool willRaiseEvent = ConnectSocket.ReceiveAsync(ReceiveEventArgs); //投递接收请求
         if (!willRaiseEvent)
         {
             PacketProtocol.ReceiveComplate(null, ReceiveEventArgs);
         }
     }
     catch (Exception e)
     {
         loger.Fatal(e);
     }
 }
Exemplo n.º 2
0
        protected void ProcessReceive()
        {
            if (ConnectSocket == null)
            {
                return;
            }

            ActiveTime = DateTime.Now;
            if (ReceiveEventArgs.BytesTransferred > 0 && ReceiveEventArgs.SocketError == SocketError.Success)
            {
                int offset = ReceiveEventArgs.Offset;
                int count  = ReceiveEventArgs.BytesTransferred;

                if (count > 0) //处理接收数据
                {
                    //如果处理数据返回失败,则断开连接
                    if (!ProcessReceive(ReceiveEventArgs.Buffer, offset, count))
                    {
                        Close();
                    }
                    else //否则投递下次介绍数据请求
                    {
                        bool willRaiseEvent = ConnectSocket.ReceiveAsync(ReceiveEventArgs); //投递接收请求
                        if (!willRaiseEvent)
                        {
                            ProcessReceive();
                        }
                    }
                }
                else
                {
                    bool willRaiseEvent = ConnectSocket.ReceiveAsync(ReceiveEventArgs); //投递接收请求
                    if (!willRaiseEvent)
                    {
                        ProcessReceive();
                    }
                }
            }
            else
            {
                Close();
            }
        }