diff --git a/Assets/StreamingAssets/Script/JsonIOExample.cs b/Assets/JsonIOTools/Example/JsonIOExample.cs similarity index 100% rename from Assets/StreamingAssets/Script/JsonIOExample.cs rename to Assets/JsonIOTools/Example/JsonIOExample.cs diff --git a/Assets/StreamingAssets/Script/JsonIOExample.cs.meta b/Assets/JsonIOTools/Example/JsonIOExample.cs.meta similarity index 100% rename from Assets/StreamingAssets/Script/JsonIOExample.cs.meta rename to Assets/JsonIOTools/Example/JsonIOExample.cs.meta diff --git a/Assets/StreamingAssets/Script.meta b/Assets/MsgTransmitTools.meta similarity index 77% rename from Assets/StreamingAssets/Script.meta rename to Assets/MsgTransmitTools.meta index cc34fb4..abbe3db 100644 --- a/Assets/StreamingAssets/Script.meta +++ b/Assets/MsgTransmitTools.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: ca6a524def5f09549b607ccfcb8a21ee +guid: 2eeb0a9ff9746c442a69591d3ba2aa5c folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/TCPClient.meta b/Assets/MsgTransmitTools/TCPClient.meta similarity index 100% rename from Assets/TCPClient.meta rename to Assets/MsgTransmitTools/TCPClient.meta diff --git a/Assets/TCPClient/Example.meta b/Assets/MsgTransmitTools/TCPClient/Example.meta similarity index 100% rename from Assets/TCPClient/Example.meta rename to Assets/MsgTransmitTools/TCPClient/Example.meta diff --git a/Assets/TCPClient/Example/Scenes.meta b/Assets/MsgTransmitTools/TCPClient/Example/Scenes.meta similarity index 100% rename from Assets/TCPClient/Example/Scenes.meta rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes.meta diff --git a/Assets/TCPClient/Example/Scenes/BluetoothExample.unity b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity similarity index 100% rename from Assets/TCPClient/Example/Scenes/BluetoothExample.unity rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity diff --git a/Assets/TCPClient/Example/Scenes/BluetoothExample.unity.meta b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity.meta similarity index 100% rename from Assets/TCPClient/Example/Scenes/BluetoothExample.unity.meta rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/BluetoothExample.unity.meta diff --git a/Assets/TCPClient/Example/Scenes/TCPLinkExample.unity b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity similarity index 100% rename from Assets/TCPClient/Example/Scenes/TCPLinkExample.unity rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity diff --git a/Assets/TCPClient/Example/Scenes/TCPLinkExample.unity.meta b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity.meta similarity index 100% rename from Assets/TCPClient/Example/Scenes/TCPLinkExample.unity.meta rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/TCPLinkExample.unity.meta diff --git a/Assets/TCPClient/Example/Scenes/UDPLinkExample.unity b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity similarity index 100% rename from Assets/TCPClient/Example/Scenes/UDPLinkExample.unity rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity diff --git a/Assets/TCPClient/Example/Scenes/UDPLinkExample.unity.meta b/Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity.meta similarity index 100% rename from Assets/TCPClient/Example/Scenes/UDPLinkExample.unity.meta rename to Assets/MsgTransmitTools/TCPClient/Example/Scenes/UDPLinkExample.unity.meta diff --git a/Assets/TCPClient/Prefeb.meta b/Assets/MsgTransmitTools/TCPClient/Prefeb.meta similarity index 100% rename from Assets/TCPClient/Prefeb.meta rename to Assets/MsgTransmitTools/TCPClient/Prefeb.meta diff --git a/Assets/TCPClient/Prefeb/TCPPrinter.prefab b/Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab similarity index 100% rename from Assets/TCPClient/Prefeb/TCPPrinter.prefab rename to Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab diff --git a/Assets/TCPClient/Prefeb/TCPPrinter.prefab.meta b/Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab.meta similarity index 100% rename from Assets/TCPClient/Prefeb/TCPPrinter.prefab.meta rename to Assets/MsgTransmitTools/TCPClient/Prefeb/TCPPrinter.prefab.meta diff --git a/Assets/TCPClient/Script.meta b/Assets/MsgTransmitTools/TCPClient/Script.meta similarity index 100% rename from Assets/TCPClient/Script.meta rename to Assets/MsgTransmitTools/TCPClient/Script.meta diff --git a/Assets/TCPClient/Script/Source.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source.meta similarity index 100% rename from Assets/TCPClient/Script/Source.meta rename to Assets/MsgTransmitTools/TCPClient/Script/Source.meta diff --git a/Assets/TCPClient/Script/Source/DataEventModel.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs similarity index 100% rename from Assets/TCPClient/Script/Source/DataEventModel.cs rename to Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs diff --git a/Assets/TCPClient/Script/Source/DataEventModel.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs.meta similarity index 100% rename from Assets/TCPClient/Script/Source/DataEventModel.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/Source/DataEventModel.cs.meta diff --git a/Assets/TCPClient/Script/Source/QFramework.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs similarity index 100% rename from Assets/TCPClient/Script/Source/QFramework.cs rename to Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs diff --git a/Assets/TCPClient/Script/Source/QFramework.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs.meta similarity index 100% rename from Assets/TCPClient/Script/Source/QFramework.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/Source/QFramework.cs.meta diff --git a/Assets/TCPClient/Script/Source/TCPEventModel.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs similarity index 100% rename from Assets/TCPClient/Script/Source/TCPEventModel.cs rename to Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs diff --git a/Assets/TCPClient/Script/Source/TCPEventModel.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs.meta similarity index 100% rename from Assets/TCPClient/Script/Source/TCPEventModel.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs.meta diff --git a/Assets/TCPClient/Script/Source/TCPUtility.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs similarity index 100% rename from Assets/TCPClient/Script/Source/TCPUtility.cs rename to Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs diff --git a/Assets/TCPClient/Script/Source/TCPUtility.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta similarity index 100% rename from Assets/TCPClient/Script/Source/TCPUtility.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta diff --git a/Assets/TCPClient/Script/View.meta b/Assets/MsgTransmitTools/TCPClient/Script/View.meta similarity index 100% rename from Assets/TCPClient/Script/View.meta rename to Assets/MsgTransmitTools/TCPClient/Script/View.meta diff --git a/Assets/TCPClient/Script/View/TCPClientView.cs b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs similarity index 100% rename from Assets/TCPClient/Script/View/TCPClientView.cs rename to Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs diff --git a/Assets/TCPClient/Script/View/TCPClientView.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs.meta similarity index 100% rename from Assets/TCPClient/Script/View/TCPClientView.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs.meta diff --git a/Assets/TCPClient/Script/View/TCPPrinter.cs b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs similarity index 100% rename from Assets/TCPClient/Script/View/TCPPrinter.cs rename to Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs diff --git a/Assets/TCPClient/Script/View/TCPPrinter.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs.meta similarity index 100% rename from Assets/TCPClient/Script/View/TCPPrinter.cs.meta rename to Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs.meta diff --git a/Assets/UDPClient.meta b/Assets/MsgTransmitTools/UDPClient.meta similarity index 100% rename from Assets/UDPClient.meta rename to Assets/MsgTransmitTools/UDPClient.meta diff --git a/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs b/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs new file mode 100644 index 0000000..fdbb57a --- /dev/null +++ b/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs @@ -0,0 +1,106 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; +using QFramework; +using System; + +public class UDPMangerArchitecture : Architecture +{ + 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(e => { + if (e.state == UDPLinkState.Linking) + { + Debug.Log("UDP开始链接"); + this.GetModel().linkServer(); + } + if (e.state == UDPLinkState.LinkSucess) + { + Debug.Log("UDP链接成功"); + this.GetModel().onReceive(); + this.GetModel().sendRequestCommand(new LinkUDPRequest(deviceId)); + onUDPLinkSuccess.Invoke(); + } + if (e.state == UDPLinkState.LinkFaild) + { + Debug.Log("UDP连接失败,请联系设备服务管理员"); + onUDPLinkFaild.Invoke(); + } + }); + + GetArchitecture().RegisterEvent(e => + { + if (e.res.GetType() == typeof(LinkSuccessResponse)) + { + Debug.Log("Link Server success"); + this.GetModel().offReceive(); + onServerConnected.Invoke(); + } + }); + + this.GetModel().setUDPState(UDPLinkState.NoIp); + this.GetModel().setIP(UDPAddress, UDPPort); + } + + public void restartUDPService() + { + onUDPReLink.Invoke(); + this.GetModel().closeServer(); + StartCoroutine(waitTwoSecond()); + } + public IEnumerator waitTwoSecond() + { + yield return new WaitForSeconds(2.0f); + this.GetModel().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); + } +} + diff --git a/Assets/UDPClient/UDPClientView.cs.meta b/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs.meta similarity index 100% rename from Assets/UDPClient/UDPClientView.cs.meta rename to Assets/MsgTransmitTools/UDPClient/UDPClientView.cs.meta diff --git a/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs new file mode 100644 index 0000000..92be64e --- /dev/null +++ b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs @@ -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(e => { + setUDPState(UDPLinkState.LinkTimeOut); + this.GetUtility().CloseUDPClient(); + }); + this.RegisterEvent(e => { + if (this.GetUtility().isOpenUDP) + { + this.GetUtility().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; + } + /// + /// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) + /// + /// ip地址 + /// 端口号 + public void setIP(string ip, int port) + { + this.udpAddress = ip; + this.udpPort = port; + //此处可以加ip校验 + Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加"); + setUDPState(UDPLinkState.Linking); + } + /// + /// 与服务端建立链接 + /// + public void linkServer() + { + if (udpState == UDPLinkState.Linking) + { + if (!this.GetUtility().isOpenUDP) + { + + bool isSuccess = this.GetUtility().StartUDPClient(udpAddress, udpPort); + if (isSuccess) + { + setUDPState(UDPLinkState.LinkSucess); + } + else + { + setUDPState(UDPLinkState.LinkFaild); + } + } + } + } + /// + /// 与服务端断开链接 + /// + public void closeServer() + { + if (udpState == UDPLinkState.LinkSucess) + { + if (this.GetUtility().isOpenUDP) + { + + this.GetUtility().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 \ No newline at end of file diff --git a/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs.meta b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs.meta new file mode 100644 index 0000000..de47fff --- /dev/null +++ b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b55fec30f758e8d43ac35ea3444023ec +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs b/Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs new file mode 100644 index 0000000..a07254d --- /dev/null +++ b/Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs @@ -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(); + } + + // Update is called once per frame + void Update() + { + if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue())) + { + GameObject item = Instantiate(udpMsgItem, udpMsgContent); + item.GetComponentInChildren().text = udpUtil.receivedData.Value; + this.GetModel().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().sendData(InputSendMsg.text); + } + public IArchitecture GetArchitecture() + { + return UDPMangerArchitecture.Interface; + } +} \ No newline at end of file diff --git a/Assets/UDPClient/UDPPrinter.cs.meta b/Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs.meta similarity index 100% rename from Assets/UDPClient/UDPPrinter.cs.meta rename to Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs.meta diff --git a/Assets/UDPClient/UDPUtility.cs b/Assets/MsgTransmitTools/UDPClient/UDPUtility.cs similarity index 77% rename from Assets/UDPClient/UDPUtility.cs rename to Assets/MsgTransmitTools/UDPClient/UDPUtility.cs index 8188bf5..8c53aee 100644 --- a/Assets/UDPClient/UDPUtility.cs +++ b/Assets/MsgTransmitTools/UDPClient/UDPUtility.cs @@ -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 public void CloseUDPClient() { - sendStream.Close(); - udpClient.Close(); + udpClient.Dispose(); isOpenUDP.Value = false; reciveT.Abort(); } @@ -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 { return udpClient.Client.Connected; } - - /// - /// 获取本机IP - /// - /// string :ip地址 - 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; - } -} +} \ No newline at end of file diff --git a/Assets/UDPClient/UDPUtility.cs.meta b/Assets/MsgTransmitTools/UDPClient/UDPUtility.cs.meta similarity index 100% rename from Assets/UDPClient/UDPUtility.cs.meta rename to Assets/MsgTransmitTools/UDPClient/UDPUtility.cs.meta diff --git a/Assets/Texture.meta b/Assets/Texture.meta new file mode 100644 index 0000000..c62cd86 --- /dev/null +++ b/Assets/Texture.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 53ce428612aa35f43b5b3ea3aa4ce9b8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UDPClient/UDPClientView.cs b/Assets/UDPClient/UDPClientView.cs deleted file mode 100644 index f2514d7..0000000 --- a/Assets/UDPClient/UDPClientView.cs +++ /dev/null @@ -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() - { - - } -} diff --git a/Assets/UDPClient/UDPPrinter.cs b/Assets/UDPClient/UDPPrinter.cs deleted file mode 100644 index f7daece..0000000 --- a/Assets/UDPClient/UDPPrinter.cs +++ /dev/null @@ -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(); - } - } -}