Browse Source

修改udp通讯套件

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
1b350fd998
  1. 0
      Assets/JsonIOTools/Example/JsonIOExample.cs
  2. 0
      Assets/JsonIOTools/Example/JsonIOExample.cs.meta
  3. 2
      Assets/MsgTransmitTools.meta
  4. 0
      Assets/MsgTransmitTools/TCPClient.meta
  5. 0
      Assets/MsgTransmitTools/TCPClient/Example.meta
  6. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes.meta
  7. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity
  8. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity.meta
  9. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity
  10. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity.meta
  11. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity
  12. 0
      Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity.meta
  13. 0
      Assets/MsgTransmitTools/TCPClient/Prefeb.meta
  14. 0
      Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab
  15. 0
      Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab.meta
  16. 0
      Assets/MsgTransmitTools/TCPClient/Script.meta
  17. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source.meta
  18. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs
  19. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs.meta
  20. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs
  21. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs.meta
  22. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs
  23. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs.meta
  24. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs
  25. 0
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta
  26. 0
      Assets/MsgTransmitTools/TCPClient/Script/View.meta
  27. 0
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs
  28. 0
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs.meta
  29. 0
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs
  30. 0
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs.meta
  31. 0
      Assets/MsgTransmitTools/UDPClient.meta
  32. 106
      Assets/MsgTransmitTools/UDPClient/UDPClientView.cs
  33. 0
      Assets/MsgTransmitTools/UDPClient/UDPClientView.cs.meta
  34. 117
      Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs
  35. 11
      Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs.meta
  36. 57
      Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs
  37. 0
      Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs.meta
  38. 37
      Assets/MsgTransmitTools/UDPClient/UDPUtility.cs
  39. 0
      Assets/MsgTransmitTools/UDPClient/UDPUtility.cs.meta
  40. 8
      Assets/Texture.meta
  41. 17
      Assets/UDPClient/UDPClientView.cs
  42. 35
      Assets/UDPClient/UDPPrinter.cs

0
Assets/StreamingAssets/Script/JsonIOExample.cs → Assets/JsonIOTools/Example/JsonIOExample.cs

0
Assets/StreamingAssets/Script/JsonIOExample.cs.meta → Assets/JsonIOTools/Example/JsonIOExample.cs.meta

2
Assets/StreamingAssets/Script.meta → Assets/MsgTransmitTools.meta

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: ca6a524def5f09549b607ccfcb8a21ee
guid: 2eeb0a9ff9746c442a69591d3ba2aa5c
folderAsset: yes
DefaultImporter:
externalObjects: {}

0
Assets/TCPClient.meta → Assets/MsgTransmitTools/TCPClient.meta

0
Assets/TCPClient/Example.meta → Assets/MsgTransmitTools/TCPClient/Example.meta

0
Assets/TCPClient/Example/Scenes.meta → Assets/MsgTransmitTools/TCPClient/Example/Scenes.meta

0
Assets/TCPClient/Example/Scenes/BluetoothExample.unity → Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity

0
Assets/TCPClient/Example/Scenes/BluetoothExample.unity.meta → Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity.meta

0
Assets/TCPClient/Example/Scenes/TCPLinkExample.unity → Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity

0
Assets/TCPClient/Example/Scenes/TCPLinkExample.unity.meta → Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity.meta

0
Assets/TCPClient/Example/Scenes/UDPLinkExample.unity → Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity

0
Assets/TCPClient/Example/Scenes/UDPLinkExample.unity.meta → Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity.meta

0
Assets/TCPClient/Prefeb.meta → Assets/MsgTransmitTools/TCPClient/Prefeb.meta

0
Assets/TCPClient/Prefeb/TCPPrinter.prefab → Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab

0
Assets/TCPClient/Prefeb/TCPPrinter.prefab.meta → Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab.meta

0
Assets/TCPClient/Script.meta → Assets/MsgTransmitTools/TCPClient/Script.meta

0
Assets/TCPClient/Script/Source.meta → Assets/MsgTransmitTools/TCPClient/Script/Source.meta

0
Assets/TCPClient/Script/Source/DataEventModel.cs → Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs

0
Assets/TCPClient/Script/Source/DataEventModel.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs.meta

0
Assets/TCPClient/Script/Source/QFramework.cs → Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs

0
Assets/TCPClient/Script/Source/QFramework.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs.meta

0
Assets/TCPClient/Script/Source/TCPEventModel.cs → Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs

0
Assets/TCPClient/Script/Source/TCPEventModel.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs.meta

0
Assets/TCPClient/Script/Source/TCPUtility.cs → Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs

0
Assets/TCPClient/Script/Source/TCPUtility.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta

0
Assets/TCPClient/Script/View.meta → Assets/MsgTransmitTools/TCPClient/Script/View.meta

0
Assets/TCPClient/Script/View/TCPClientView.cs → Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs

0
Assets/TCPClient/Script/View/TCPClientView.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs.meta

0
Assets/TCPClient/Script/View/TCPPrinter.cs → Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs

0
Assets/TCPClient/Script/View/TCPPrinter.cs.meta → Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs.meta

0
Assets/UDPClient.meta → Assets/MsgTransmitTools/UDPClient.meta

106
Assets/MsgTransmitTools/UDPClient/UDPClientView.cs

@ -0,0 +1,106 @@ @@ -0,0 +1,106 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using QFramework;
using System;
public class UDPMangerArchitecture : Architecture<UDPMangerArchitecture>
{
protected override void Init()
{
this.RegisterUtility(new UDPUtility());
this.RegisterModel(new UDPEventModel());
}
}
public class UDPClientView : MonoBehaviour, IController, ICanSendEvent
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onUDPLinkSuccess;
public UnityEvent onUDPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onUDPReLink;
private string UDPAddress;
private int UDPPort;
private int deviceId;
// Start is called before the first frame update
void Awake()
{
initUDPService();
DontDestroyOnLoad(this);
}
public void initUDPService()
{
UDPAddress = "127.0.0.1";
UDPPort = 20000;
deviceId = 1;
GetArchitecture().RegisterEvent<UDPStateChangedEvent>(e => {
if (e.state == UDPLinkState.Linking)
{
Debug.Log("UDP开始链接");
this.GetModel<UDPEventModel>().linkServer();
}
if (e.state == UDPLinkState.LinkSucess)
{
Debug.Log("UDP链接成功");
this.GetModel<UDPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<UDPEventModel>().sendRequestCommand(new LinkUDPRequest(deviceId));
onUDPLinkSuccess.Invoke();
}
if (e.state == UDPLinkState.LinkFaild)
{
Debug.Log("UDP连接失败,请联系设备服务管理员");
onUDPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e =>
{
if (e.res.GetType() == typeof(LinkSuccessResponse))
{
Debug.Log("Link Server success");
this.GetModel<UDPEventModel>().offReceive<LinkSuccessResponse>();
onServerConnected.Invoke();
}
});
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.NoIp);
this.GetModel<UDPEventModel>().setIP(UDPAddress, UDPPort);
}
public void restartUDPService()
{
onUDPReLink.Invoke();
this.GetModel<UDPEventModel>().closeServer();
StartCoroutine(waitTwoSecond());
}
public IEnumerator waitTwoSecond()
{
yield return new WaitForSeconds(2.0f);
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.Linking);
}
public IArchitecture GetArchitecture()
{
return UDPMangerArchitecture.Interface;
}
}
public class LinkUDPRequest : IRequest
{
public string id;
public int type;
public LinkUDPRequest(int id)
{
this.id = id.ToString();
this.type = 1;
}
public string toJson()
{
return JsonUtility.ToJson(this);
}
}

0
Assets/UDPClient/UDPClientView.cs.meta → Assets/MsgTransmitTools/UDPClient/UDPClientView.cs.meta

117
Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs

@ -0,0 +1,117 @@ @@ -0,0 +1,117 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
public class UDPEventModel : DataEventModel
{
private UDPLinkState udpState = UDPLinkState.NoIp;
private string udpAddress = "";
private int udpPort = 0;
public string myAddress = "";
protected override void OnInit()
{
this.RegisterEvent<onUDPLinkException>(e => {
setUDPState(UDPLinkState.LinkTimeOut);
this.GetUtility<UDPUtility>().CloseUDPClient();
});
this.RegisterEvent<RequestMsgEvent>(e => {
if (this.GetUtility<UDPUtility>().isOpenUDP)
{
this.GetUtility<UDPUtility>().sendData(e.req.toJson());
}
else
{
Debug.LogWarning("请先开启UDP链接");
}
});
}
public void setUDPState(UDPLinkState state)
{
this.udpState = state;
this.SendEvent(new UDPStateChangedEvent(state));
}
public UDPLinkState getState()
{
return 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以及端口号校验,日后有需求可以增加");
setUDPState(UDPLinkState.Linking);
}
/// <summary>
/// 与服务端建立链接
/// </summary>
public void linkServer()
{
if (udpState == UDPLinkState.Linking)
{
if (!this.GetUtility<UDPUtility>().isOpenUDP)
{
bool isSuccess = this.GetUtility<UDPUtility>().StartUDPClient(udpAddress, udpPort);
if (isSuccess)
{
setUDPState(UDPLinkState.LinkSucess);
}
else
{
setUDPState(UDPLinkState.LinkFaild);
}
}
}
}
/// <summary>
/// 与服务端断开链接
/// </summary>
public void closeServer()
{
if (udpState == UDPLinkState.LinkSucess)
{
if (this.GetUtility<UDPUtility>().isOpenUDP)
{
this.GetUtility<UDPUtility>().CloseUDPClient();
setUDPState(UDPLinkState.NoIp);
}
}
}
}
#region enum
public enum UDPLinkState
{
NoIp = 0,
Linking = 1,
LinkFaild = 2,
LinkSucess = 3,
LinkTimeOut = 4
}
#endregion
#region event
public struct UDPStateChangedEvent
{
public UDPLinkState state;
public UDPStateChangedEvent(UDPLinkState state_)
{
state = state_;
}
}
public struct onUDPLinkException
{
public string exceptionMsg;
public onUDPLinkException(string exceptionMsg_)
{
exceptionMsg = exceptionMsg_;
}
}
#endregion

11
Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs.meta

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b55fec30f758e8d43ac35ea3444023ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

57
Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
public class UDPPrinter : MonoBehaviour, IController, ICanSendEvent, ICanGetUtility
{
private UDPUtility udpUtil;
public Transform udpMsgContent;
public GameObject udpMsgItem;
public InputField InputSendMsg;
private bool isUDPInit;
// Start is called before the first frame update
void Start()
{
udpUtil = GetArchitecture().GetUtility<UDPUtility>();
}
// Update is called once per frame
void Update()
{
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue()))
{
GameObject item = Instantiate(udpMsgItem, udpMsgContent);
item.GetComponentInChildren<Text>().text = udpUtil.receivedData.Value;
this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData.Value);
}
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP)
{
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData.Value));
udpUtil.isOpenUDP.Value = false;
}
}
private void OnDestroy()
{
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running)
{
udpUtil.reciveT.Abort();
}
}
public void sendMsg()
{
this.GetUtility<UDPUtility>().sendData(InputSendMsg.text);
}
public IArchitecture GetArchitecture()
{
return UDPMangerArchitecture.Interface;
}
}

0
Assets/UDPClient/UDPPrinter.cs.meta → Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs.meta

37
Assets/UDPClient/UDPUtility.cs → Assets/MsgTransmitTools/UDPClient/UDPUtility.cs

@ -3,12 +3,10 @@ using QFramework; @@ -3,12 +3,10 @@ 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;
@ -61,8 +59,7 @@ public class UDPUtility : IUtility @@ -61,8 +59,7 @@ public class UDPUtility : IUtility
public void CloseUDPClient()
{
sendStream.Close();
udpClient.Close();
udpClient.Dispose();
isOpenUDP.Value = false;
reciveT.Abort();
}
@ -98,7 +95,7 @@ public class UDPUtility : IUtility @@ -98,7 +95,7 @@ public class UDPUtility : IUtility
//断线发送异常
isTimeOut.Value = true;
exceptionData.Value = e.ToString();
//break;
break;
}
}
Debug.Log("------------end While-------------");
@ -134,30 +131,4 @@ public class UDPUtility : IUtility @@ -134,30 +131,4 @@ public class UDPUtility : IUtility
{
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;
}
}
}

0
Assets/UDPClient/UDPUtility.cs.meta → Assets/MsgTransmitTools/UDPClient/UDPUtility.cs.meta

8
Assets/Texture.meta

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 53ce428612aa35f43b5b3ea3aa4ce9b8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

17
Assets/UDPClient/UDPClientView.cs

@ -1,17 +0,0 @@ @@ -1,17 +0,0 @@

using UnityEngine;
public class UDPClientView : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

35
Assets/UDPClient/UDPPrinter.cs

@ -1,35 +0,0 @@ @@ -1,35 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class UDPPrinter : MonoBehaviour
{
private UDPUtility udpUtil;
// Start is called before the first frame update
void Start()
{
udpUtil = new UDPUtility("192.168.1.41", 20000);
udpUtil.sendData("hello everyone!");
}
// Update is called once per frame
void Update()
{
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue()))
{
Debug.Log(udpUtil.receivedData.Value);
}
}
private void OnDestroy()
{
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running)
{
udpUtil.reciveT.Abort();
}
}
}
Loading…
Cancel
Save