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.
204 lines
6.1 KiB
204 lines
6.1 KiB
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<RequestMsgEvent>(e => { |
|
if (this.GetUtility<UDPUtility>().isOpenUDP) |
|
{ |
|
this.GetUtility<UDPUtility>().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; |
|
} |
|
/// <summary> |
|
/// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) |
|
/// </summary> |
|
/// <param name="ip">ip地址</param> |
|
/// <param name="port">端口号</param> |
|
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<UDPUtility>().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<UDPUtility>().isOpenUDP) |
|
{ |
|
|
|
this.GetUtility<UDPUtility>().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-------------"); |
|
} |
|
|
|
/// <summary> |
|
/// 线程接收到消息后 |
|
/// </summary> |
|
/// <returns>接收到的消息</returns> |
|
public string getReceivedValue() |
|
{ |
|
if (isReceivedValue) |
|
{ |
|
isReceivedValue = false; |
|
return receivedData; |
|
} |
|
else |
|
{ |
|
return ""; |
|
} |
|
} |
|
|
|
public string getReceivedAddress() { |
|
return RemoteIpEndPoint.Address.ToString() + ":" + RemoteIpEndPoint.Port.ToString(); |
|
} |
|
} |
|
} |