123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using IFramework.Net;
- using IFramework.Net.Udp;
- using Rokid.MRC;
- using System;
- using System.Collections;
- using System.Text;
- using UnityEngine;
- namespace IFramework.Custom
- {
-
- public class UDPClientProvider : MonoBehaviour
- {
- private IUdpClientProvider clientProvider;
- private bool isConnect = false;
- public int port = 16898;
- private string lastIP = "127.0.0.1";
- private bool receivedMsg;
- private float receivedTime;
- private float curTime;
- private float heartBeatInterval = 5;
- private float reconnectInterval = 3;
- private float sendMsgInterval = 2;
- private void Awake()
- {
- Init(ClientCallback);
- StartCoroutine(TryConnect());
- StartCoroutine(CheckSendIP());
- }
-
- private void Init( Action<SegmentToken> ReceiveCallback = null)
- {
- clientProvider = NetTool.CreateUdpClient(4096, 4);
-
- clientProvider.ReceivedOffsetHandler = new OnReceivedSegmentHandler((SegmentToken session) =>
- {
- ReceiveCallback?.Invoke(session);
- });
- }
- private IEnumerator CheckSendIP()
- {
- yield return new WaitForSeconds(sendMsgInterval);
- if(clientProvider != null)
- {
- Send(SystemInfo.deviceName);
- }
- StartCoroutine(CheckSendIP());
-
-
-
- }
- private void Connect(int port)
- {
-
- string curIP = SDKManager.Instance.GetConnectedDeviceIP();
-
-
-
-
-
-
-
-
-
-
-
-
- clientProvider.Connect(port, curIP);
- receivedMsg = false;
- lastIP = curIP;
- isConnect = true;
-
- }
- private IEnumerator TryConnect()
- {
-
-
-
-
- Connect(port);
- yield return new WaitForSeconds(reconnectInterval);
- if(!receivedMsg)
- {
- StartCoroutine(TryConnect());
- }
- else
- {
-
- StartCoroutine(HeartBeat());
- }
- }
- private IEnumerator HeartBeat()
- {
- yield return new WaitUntil(() =>
- {
- if(!receivedMsg || (receivedMsg && (curTime - receivedTime) > heartBeatInterval))
- {
-
- return true;
- }
- return false;
- });
- StartCoroutine(TryConnect());
- }
- private void ClientCallback(SegmentToken session)
- {
- string msg = Encoding.UTF8.GetString(session.Data.buffer, session.Data.offset, session.Data.size);
-
- if(msg == "OK")
- {
- receivedMsg = true;
- receivedTime = curTime;
- }
- }
-
- private bool CheckConnected()
- {
- UdpClientProvider provider = (UdpClientProvider)clientProvider;
- if(provider == null || provider.socket == null)
- {
- return false;
- }
- bool connected = provider.socket.Connected;
- Debug.Log($"Socket Connected : {connected}");
- return connected;
- }
- public void SendMsgTest(string msg)
- {
-
-
-
-
-
-
-
- Send(msg);
- }
- public void Send(string message)
- {
- if(isConnect)
- {
-
- clientProvider.Send(new SegmentOffset(Encoding.UTF8.GetBytes(message)));
- }
- else
- {
- Debug.LogWarning("未连接");
- }
- }
- private void Update()
- {
- curTime = Time.realtimeSinceStartup;
- }
- private void OnDestroy()
- {
- Dispose();
- }
- public void Dispose()
- {
- clientProvider?.Dispose();
- clientProvider = null;
- receivedMsg = false;
- }
-
- public void OnApplicationPause(bool pause)
- {
- if(!pause)
- {
-
- }
- }
- }
- }
|