51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
|
#if !NETFX_CORE
|
|||
|
|
|||
|
using System;
|
|||
|
using System.Net;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Plane.Communication
|
|||
|
{
|
|||
|
internal class UdpServerConnection : UdpConnectionBase
|
|||
|
{
|
|||
|
private IPEndPoint _remoteEP;
|
|||
|
|
|||
|
private Func<byte[], int, IPEndPoint, Task<int>> _sendFunc;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 初始化 <see cref="UdpServerConnection"/> 的实例,并指定远程终结点和发送数据所使用的方法。
|
|||
|
/// </summary>
|
|||
|
public UdpServerConnection(IPEndPoint remoteEP, Func<byte[], int, IPEndPoint, Task<int>> sendFunc)
|
|||
|
{
|
|||
|
_remoteEP = remoteEP;
|
|||
|
_sendFunc = sendFunc;
|
|||
|
}
|
|||
|
|
|||
|
public override void Close()
|
|||
|
{
|
|||
|
IsOpen = false;
|
|||
|
}
|
|||
|
|
|||
|
public override Task OpenAsync()
|
|||
|
{
|
|||
|
IsOpen = true;
|
|||
|
return TaskUtils.CompletedTask;
|
|||
|
}
|
|||
|
|
|||
|
protected override async Task SendAsync(byte[] datagram, int bytes)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
await _sendFunc(datagram, bytes, _remoteEP).ConfigureAwait(false);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Close();
|
|||
|
RaiseExceptionThrown(ex);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endif
|