60 lines
1.7 KiB
C#
60 lines
1.7 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 IPEndPoint _boardaddEP;
|
||
|
||
private Func<byte[], int, IPEndPoint, Task<int>> _sendFunc;
|
||
|
||
/// <summary>
|
||
/// 初始化 <see cref="UdpServerConnection"/> 的实例,并指定远程终结点和发送数据所使用的方法。
|
||
/// </summary>
|
||
public UdpServerConnection(IPEndPoint remoteEP, Func<byte[], int, IPEndPoint, Task<int>> sendFunc)
|
||
{
|
||
_boardaddEP = new IPEndPoint(IPAddress.Parse("192.168.62.255"), remoteEP.Port);
|
||
_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);
|
||
/*
|
||
//如果是广播包,用于广播消息,比如RTK等
|
||
if (false)
|
||
await _sendFunc(datagram, bytes, _boardaddEP).ConfigureAwait(false);
|
||
else
|
||
await _sendFunc(datagram, bytes, _remoteEP).ConfigureAwait(false);
|
||
*/
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Close();
|
||
RaiseExceptionThrown(ex);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#endif
|