金铉Unity插件库 Unity版本2018.4.32f 目前包含本地化存储功能(根据类名存储读写),TCP客户端监听类型功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.3 KiB

using UnityEngine;
using QFramework;
using System;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net;
public class UDPUtility : IUtility
{
public string udpAddress;
public int udpPort;
public UdpClient udpClient;
public NetworkStream sendStream;
public BindableProperty<bool> isOpenUDP = new BindableProperty<bool>(false);
public bool isReceivedValue = false;
public BindableProperty<string> receivedData = new BindableProperty<string>("");
public BindableProperty<string> exceptionData = new BindableProperty<string>("");
public Thread reciveT;
public BindableProperty<bool> isTimeOut = new BindableProperty<bool>(false);
public UDPUtility()
{
Debug.LogWarning("使用无参数构造udp时,需要手动开启udp服务");
}
public UDPUtility(string UDPAddress, int UDPPort)
{
this.udpAddress = UDPAddress;
this.udpPort = UDPPort;
StartUDPClient(UDPAddress, UDPPort);
}
public bool StartUDPClient(string ip, int port)
{
isTimeOut.Value = false;
if (!isOpenUDP)
{
udpAddress = ip;
udpPort = port;
try
{
udpClient = new UdpClient(udpAddress, udpPort);
}
catch (Exception e)
{
exceptionData.Value = e.ToString();
return false;
}
isOpenUDP.Value = true;
reciveT = new Thread(RecciveMsg);
reciveT.IsBackground = true;
reciveT.Start();
return true;
}
return false;
}
public void CloseUDPClient()
{
sendStream.Close();
udpClient.Close();
isOpenUDP.Value = false;
reciveT.Abort();
}
public void sendData(string data)
{
if (isOpenUDP == false)
return;
Byte[] sendBytes = Encoding.Default.GetBytes(data);
udpClient.Send(sendBytes, sendBytes.Length);
}
public void RecciveMsg()
{
string msg = "";
//Debug.Log("StartReceiving!");
while (isOpenUDP.Value)
{
try
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
msg = Encoding.UTF8.GetString(receiveBytes);
if (msg != "")
{
receivedData.Value = msg;
isReceivedValue = true;
}
}
catch (Exception e)
{
Debug.LogWarning(e);
//断线发送异常
isTimeOut.Value = true;
exceptionData.Value = e.ToString();
//break;
}
}
Debug.Log("------------end While-------------");
}
/// <summary>
/// 线程接收到消息后
/// </summary>
/// <returns>接收到的消息</returns>
public string getReceivedValue()
{
if (isReceivedValue)
{
isReceivedValue = false;
return receivedData.Value;
}
else
{
return "";
}
}
public bool getTimeOutState()
{
return isTimeOut;
}
public bool IsReceivedMsg()
{
return sendStream.Length != 0;
}
public bool IsConnected()
{
return udpClient.Client.Connected;
}
/// <summary>
/// 获取本机IP
/// </summary>
/// <returns>string :ip地址</returns>
public string GetIP()
{
string output = "";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; //无线局域网适配器
if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up)
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
output = ip.Address.ToString();
}
}
}
}
return output;
}
}