using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TeamAAS.Communication.Attributes;
using TeamAAS.Communication.Base;
using TeamAAS.Communication.Interfaces;
namespace TeamAAS.Communication.Devices
{
///
/// UDP 通讯(数据报协议,无连接,广播/多播支持)。
///
[Communication("UDP", "基础通讯", "UDP 数据报通讯")]
public class UdpCommunication : BindableCommunicationBase
{
[JsonIgnore]
private UdpClient _udpClient;
private readonly ConcurrentQueue _received = new ConcurrentQueue();
private string _localIp = "0.0.0.0";
public string LocalIp
{
get { return _localIp; }
set { if (SetProperty(ref _localIp, value)) Notify(nameof(EndpointUrl)); }
}
private int _localPort = 8888;
public int LocalPort
{
get { return _localPort; }
set { if (SetProperty(ref _localPort, value)) Notify(nameof(EndpointUrl)); }
}
private string _remoteIp = "127.0.0.1";
public string RemoteIp
{
get { return _remoteIp; }
set { if (SetProperty(ref _remoteIp, value)) Notify(nameof(EndpointUrl)); }
}
private int _remotePort = 9999;
public int RemotePort
{
get { return _remotePort; }
set { if (SetProperty(ref _remotePort, value)) Notify(nameof(EndpointUrl)); }
}
public override string EndpointUrl
{
get { return $"udp://{LocalIp}:{LocalPort} -> {RemoteIp}:{RemotePort}"; }
set
{
if (!string.IsNullOrWhiteSpace(value) && value.StartsWith("udp://"))
{
var uri = new Uri(value);
if (!string.IsNullOrWhiteSpace(uri.Host)) LocalIp = uri.Host;
if (uri.Port > 0) LocalPort = uri.Port;
}
}
}
[JsonIgnore]
public override bool IsConnected => _udpClient != null;
public override event Action