详细描述 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
164 lines
4.1 KiB
C#
164 lines
4.1 KiB
C#
#if !NETFX_CORE
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Plane.Communication
|
|
{
|
|
/// <summary>
|
|
/// 提供 TCP 通信的部分实现。
|
|
/// </summary>
|
|
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<int> 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
|