#if !NETFX_CORE using System; using System.IO; using System.Net.Sockets; using System.Threading.Tasks; namespace Plane.Communication { /// /// 提供 TCP 通信的部分实现。 /// public abstract class TcpConnectionBase : ExceptionThrownEventSource, IConnection { protected TcpClient _client; protected bool _isBroken; protected Stream _stream; public int Available { get { try { if (_client.Connected) return _client.Available; else return 0; } catch (ObjectDisposedException ex) { RaiseExceptionThrown(ex); return 0; } } } public bool IsOnline { get { try { // bool bret ; // bret = _client.Client != null; // bret = bret && !((_client.Client.Poll(1000, SelectMode.SelectRead) && (_client.Client.Available == 0)) || !_client.Client.Connected); return _client != null && _client.Client != null && !((_client.Client.Poll(1000, SelectMode.SelectRead) && (_client.Client.Available == 0)) || !_client.Client.Connected); } catch (ObjectDisposedException) { return false; } } } public bool IsOpen { get { try { return !_isBroken && _client.Connected; } catch (ObjectDisposedException) { return false; } } } public void Close() { _stream?.Close(); _client?.Close(); } public abstract Task OpenAsync(); public int BytesToRead() { return 0; } public int Read(byte[] buffer, int offset, int count) { try { return _stream.Read(buffer, offset, count); } catch (ArgumentOutOfRangeException ex) { RaiseExceptionThrown(ex); return 0; } catch (Exception ex) // 常见的是 IOException。 { _isBroken = true; RaiseExceptionThrown(ex); return 0; } } public async Task ReadAsync(byte[] buffer, int offset, int count) { try { while (Available < count) { if (Available > 0) Console.WriteLine("Available = " + Available); //if (!IsOpen) // return 0; if (!IsOnline) return 0; await Task.Delay(5).ConfigureAwait(false); } return await _stream.ReadAsync(buffer, offset, count); } catch (Exception ex) { _isBroken = true; RaiseExceptionThrown(ex); return 0; } } public void Write(byte[] buffer, int offset, int count) { try { _stream.Write(buffer, offset, count); } catch (ArgumentOutOfRangeException ex) { RaiseExceptionThrown(ex); } catch (Exception ex) { _isBroken = true; RaiseExceptionThrown(ex); } } public async Task WriteAsync(byte[] buffer, int offset, int count) { try { await _stream.WriteAsync(buffer, offset, count); } catch (Exception ex) { _isBroken = true; RaiseExceptionThrown(ex); } } } } #endif