/// <summary> /// Método base para recebimento dos dados. /// </summary> /// <param name="data"></param> /// <returns></returns> private CommunicationResult ReceiveBase(out byte[] data) { CommunicationResult receiveFailed = CommunicationResult.ReceiveFailed; data = null; try { lock (_tcpLock) { if (_tcpClient == null) { receiveFailed = CommunicationResult.Closed; } else { _tcpClient.Client.Blocking = true; receiveFailed = _reader.ReadStream(this.Stream, out data); } } } catch (System.Net.Sockets.SocketException exception) { receiveFailed = new CommunicationResult(exception.ErrorCode); } catch (ObjectDisposedException) { receiveFailed = CommunicationResult.Closed; } if ((receiveFailed != 0) && (base.RemoteEndPoint != null) && data != null) { LogAccessor.Log.Verbose("<TCP>Received from [{0}]: {1}", new object[] { base.RemoteEndPoint.ToString(), BinaryTransformer.GetString(data, 0, data.Length) }); } return(receiveFailed); }
/// <summary> /// Envia os dados. /// </summary> /// <param name="data">Dados que serão enviados.</param> /// <param name="size">Tamanho dos dados.</param> /// <returns></returns> public override CommunicationResult Send(byte[] data, int size) { CommunicationResult success = CommunicationResult.Success; try { lock (_tcpLock) { if (Stream != null) { Stream.Write(data, 0, size); Stream.Flush(); } else { success = CommunicationResult.Closed; } } if ((success != 0) && (base.RemoteEndPoint != null)) { LogAccessor.Log.Verbose("<TCP>Sent to [{0}]: {1}", base.RemoteEndPoint.ToString(), BinaryTransformer.GetString(data, 0, size)); } } catch (ArgumentNullException) { return(this.HandleResult(CommunicationResult.SendFailed)); } catch (ArgumentOutOfRangeException) { return(this.HandleResult(CommunicationResult.SendFailed)); } catch (ArgumentException) { return(this.HandleResult(CommunicationResult.SendFailed)); } catch (NotSupportedException) { return(this.HandleResult(CommunicationResult.SendFailed)); } catch (ObjectDisposedException) { return(this.HandleResult(CommunicationResult.Closed)); } catch (System.IO.IOException exception) { var innerException = exception.InnerException as System.Net.Sockets.SocketException; if (innerException != null) { return(HandleResult(new CommunicationResult(innerException.ErrorCode))); } return(HandleResult(CommunicationResult.Closed)); } return(HandleResult(success)); }
/// <summary> /// Recebe os dados. /// </summary> /// <param name="data">Buffer dos dados recebidos.</param> /// <param name="expectedSize">Tamanho esperado.</param> /// <returns></returns> public CommunicationResult Receive(out byte[] data, int expectedSize) { CommunicationResult receiveFailed = CommunicationResult.ReceiveFailed; lock (_tcpLock) receiveFailed = _reader.ReadStream(this.Stream, out data, expectedSize); if (receiveFailed == 0) { return(HandleResult(receiveFailed)); } if (base.RemoteEndPoint != null) { LogAccessor.Log.Verbose("<TCP>Received from [{0}]: {1}", base.RemoteEndPoint.ToString(), BinaryTransformer.GetString(data, 0, data.Length)); } return(receiveFailed); }
/// <summary> /// Lê o buffer. /// </summary> private void ReadBuffer() { object obj2; while (true) { System.Threading.Monitor.Enter(obj2 = this._actionLock); try { byte[] buffer; if (_reader.ReadBuffer(out buffer)) { if (buffer != null) { if (base.RemoteEndPoint != null) { LogAccessor.Log.Verbose("<TCP>Received from [{0}]: {1}", base.RemoteEndPoint.ToString(), BinaryTransformer.GetString(buffer, 0, buffer.Length)); } try { if (_actionCallBack != null) { _actionCallBack(buffer); } } catch (NullReferenceException) { LogAccessor.Log.Verbose("<TCP>ReadBuffer aborted (null action)."); return; } catch (ObjectDisposedException) { return; } } continue; } break; } finally { System.Threading.Monitor.Exit(obj2); } } }
/// <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); } }