using UnityEngine; using QFrameworkCP; using System; using System.Text; using System.Net.Sockets; using System.Threading; using System.Net; namespace JXSoft { public class UDPClientModel : DataEventModel { private LinkStateDefault udpState = LinkStateDefault.Close; private string udpAddress = ""; private int udpPort = 0; public string myAddress = ""; protected override void OnInit() { this.RegisterEvent(e => { if (this.GetUtility().isOpenUDP) { this.GetUtility().sendData(e.req.toProtocolData()); } else { Debug.LogWarning("请先开启UDP链接"); } }); } public override void setLinkState(int linkStatus) { this.udpState = (LinkStateDefault)linkStatus; this.SendEvent(new LinkStateChangedEvent(linkStatus)); } public override int getLinkState() { return (int)this.udpState; } /// /// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) /// /// ip地址 /// 端口号 public void setIP(string ip, int port) { this.udpAddress = ip; this.udpPort = port; //此处可以加ip校验 Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加"); } public override bool Start() { bool isSuccess = this.GetUtility().StartUDPClient(udpAddress, udpPort); if (udpState == LinkStateDefault.Close) { if (isSuccess) { setLinkState((int)LinkStateDefault.Open); } else { setLinkState((int)LinkStateDefault.Close); } } return isSuccess; } public override bool Close() { if (udpState == LinkStateDefault.Close) { if (this.GetUtility().isOpenUDP) { this.GetUtility().CloseUDPClient(); setLinkState((int)LinkStateDefault.Close); } } return true; } } public class UDPUtility : IUtility { public string udpAddress; public int udpPort; public UdpClient udpClient; public NetworkStream sendStream; public bool isOpenUDP = false; public bool isReceivedValue = false; public string receivedData = ""; public string exceptionData = ""; public Thread reciveT; public bool isTimeOut = false; private IPEndPoint RemoteIpEndPoint; 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 = false; if (!isOpenUDP) { udpAddress = ip; udpPort = port; try { RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, udpPort); udpClient = new UdpClient(RemoteIpEndPoint); } catch (Exception e) { exceptionData = e.ToString(); return false; } isOpenUDP = true; reciveT = new Thread(RecciveMsg); reciveT.IsBackground = true; reciveT.Start(); return true; } return false; } public void CloseUDPClient() { udpClient.Dispose(); isOpenUDP = 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 sendData(string data, string ip, int port) { if (isOpenUDP == false) return; Byte[] sendBytes = Encoding.Default.GetBytes(data); udpClient.Send(sendBytes, sendBytes.Length, ip, port); } public void RecciveMsg() { string msg = ""; //Debug.Log("StartReceiving!"); while (isOpenUDP) { try { Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); msg = Encoding.UTF8.GetString(receiveBytes); if (msg != "") { receivedData = msg; isReceivedValue = true; } } catch (Exception e) { isTimeOut = true; exceptionData = e.ToString(); } } Debug.Log("------------end While-------------"); } /// /// 线程接收到消息后 /// /// 接收到的消息 public string getReceivedValue() { if (isReceivedValue) { isReceivedValue = false; return receivedData; } else { return ""; } } public string getReceivedAddress() { return RemoteIpEndPoint.Address.ToString() + ":" + RemoteIpEndPoint.Port.ToString(); } } }