/// <summary>
 /// Finaliza o recebimento assincrono dos dados.
 /// </summary>
 /// <returns></returns>
 public override CommunicationResult EndReceive()
 {
     try
     {
         lock (_tcpLock)
         {
             _actionCallBack = null;
             if (_readResult == null)
             {
                 return(CommunicationResult.Success);
             }
             if (_readResult.AsyncWaitHandle == null)
             {
                 return(CommunicationResult.Success);
             }
             TcpCommunicationState asyncState = (TcpCommunicationState)_readResult.AsyncState;
             _readResult.AsyncWaitHandle.Close();
             LogAccessor.Log.Verbose("<TCP> End Receive");
         }
         return(CommunicationResult.Success);
     }
     catch (ObjectDisposedException)
     {
         return(CommunicationResult.Success);
     }
 }
 /// <summary>
 /// Inicia o processo assincrono para recebimento dos dados.
 /// </summary>
 /// <param name="action"></param>
 /// <returns></returns>
 public CommunicationResult BeginReceive(Action <byte[]> action)
 {
     try
     {
         lock (_actionLock)
             _actionCallBack = action;
         LogAccessor.Log.Verbose("<TCP> Begin Receive");
         this.ReadBuffer();
         CommunicationResult success = CommunicationResult.Success;
         lock (_tcpLock)
         {
             if (_receiving || ((_readResult != null) && !_readResult.IsCompleted))
             {
                 LogAccessor.Log.Verbose("<TCP> Already Receiving=>Skip Begin Receive");
             }
             else if (this.Stream == null)
             {
                 success = CommunicationResult.Closed;
                 LogAccessor.Log.Verbose("<TCP> Connection is Closed=>Skip Begin Receive");
             }
             else
             {
                 byte[] data = new byte[1024];
                 TcpCommunicationState state = new TcpCommunicationState(data);
                 _receiving  = true;
                 _readResult = Stream.BeginRead(data, 0, data.Length, new AsyncCallback(TcpStreamReceived), state);
             }
         }
         return(HandleResult(success));
     }
     catch (System.IO.IOException)
     {
         _receiving = false;
         return(this.HandleResult(CommunicationResult.Closed));
     }
     catch (ObjectDisposedException)
     {
         _receiving = false;
         return(HandleResult(CommunicationResult.Closed));
     }
     catch (ArgumentException)
     {
         _receiving = false;
     }
     catch (NotSupportedException)
     {
         _receiving = false;
         return(HandleResult(CommunicationResult.Closed));
     }
     catch (InvalidOperationException)
     {
         _receiving = false;
         return(HandleResult(CommunicationResult.Closed));
     }
     return(HandleResult(CommunicationResult.ReceiveFailed));
 }
        /// <summary>
        /// Método acionado quando dados forem recebidos da stream da conexão.
        /// </summary>
        /// <param name="result"></param>
        private void TcpStreamReceived(IAsyncResult result)
        {
            CommunicationResult success = CommunicationResult.Success;

            try
            {
                int length = 0;
                try
                {
                    lock (_tcpLock)
                    {
                        _receiving = false;
                        if (Stream == null)
                        {
                            return;
                        }
                        length = Stream.EndRead(result);
                        if (length == 0)
                        {
                            if (base.RemoteEndPoint != null)
                            {
                                LogAccessor.Log.Verbose("<TCP>Connection closed by remote host [{0}]", new object[] {
                                    base.RemoteEndPoint.ToString()
                                });
                            }
                            success = CommunicationResult.Closed;
                            return;
                        }
                        TcpCommunicationState asyncState = (TcpCommunicationState)result.AsyncState;
                        if (_actionCallBack != null)
                        {
                            byte[] data = asyncState.GetData();
                            LogAccessor.Log.Verbose("<TCP> Packet: {0}", new object[] {
                                BinaryTransformer.GetString(data, 0, length, Encoding.ASCII)
                            });
                            success = _reader.RegisterBuffer(data, length);
                        }
                    }
                }
                catch (ArgumentNullException)
                {
                    success = CommunicationResult.ReceiveFailed;
                    return;
                }
                catch (ArgumentException)
                {
                    success = CommunicationResult.ReceiveFailed;
                    return;
                }
                catch (System.IO.IOException)
                {
                    BeginReceive(_actionCallBack);
                    return;
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                if (success != 0)
                {
                    ReadBuffer();
                    BeginReceive(_actionCallBack);
                }
            }
            finally
            {
                HandleResult(success);
            }
        }