UDPManger.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using IFramework.Net;
  2. using IFramework;
  3. using IFramework.Net.Udp;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using UnityEngine;
  9. namespace IFramework.Custom
  10. {
  11. public class UDPManger : MonoBehaviour
  12. {
  13. //#if !TEST
  14. private int serverPort = 16898;
  15. private int clientPort = 16897;
  16. private int buffClientPort = 16896;
  17. //#elif TEST
  18. //private int serverPort = 12345;
  19. //private int clientPort = 12346;
  20. //#endif
  21. public static UDPManger Instance;
  22. //private UDPClientProvider cameraClient;
  23. private bool initChaos = false;
  24. private bool alreadySendIp = false;
  25. private string buffClientIp = "";
  26. private void Awake()
  27. {
  28. Instance = this;
  29. Debug.Log("Version:" + Application.version);
  30. }
  31. void Start()
  32. {
  33. //cameraClient = new UDPClientProvider();
  34. //StartCoroutine(SendSDKData());
  35. }
  36. private void ClientBuffCallback(SegmentToken session)
  37. {
  38. Debug.Log("(客户端Buff)收到消息:" + Encoding.UTF8.GetString(session.Data.buffer, session.Data.offset,
  39. session.Data.size));
  40. }
  41. IEnumerator SendSDKData()
  42. {
  43. while(true)
  44. {
  45. yield return new WaitForEndOfFrame();
  46. SendCameraData();
  47. }
  48. }
  49. /// 获取时间戳
  50. public long GetTimeStampSecond()
  51. {
  52. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  53. try
  54. {
  55. return Convert.ToInt64(ts.TotalSeconds);
  56. }
  57. catch(Exception ex)
  58. {
  59. Debug.Log($"GetTimeStampSecond Error = {ex}");
  60. return 0;
  61. }
  62. }
  63. private bool SendIp()
  64. {
  65. if(!initChaos)
  66. return false;
  67. //TextureHelper.initTcp(buffClientIp, buffClientPort);
  68. return true;
  69. }
  70. public void SendCamerBuff(byte[] data)
  71. {
  72. Debug.LogError("CameraBuff:" + data.ToString());
  73. ;
  74. if(!initChaos)
  75. return;
  76. if(data != null)
  77. {
  78. //cameraBuffClient.ClientSend(data.ToString());
  79. }
  80. }
  81. public void SendCameraData()
  82. {
  83. if(initChaos)
  84. {
  85. //DataFromSDK data = GetHDMPoseData();
  86. //data.timestamp = GetTimeStampSecond();
  87. //data.virtualkey = -1;
  88. //data.hand_points = null;
  89. //string json = JsonUtil.Serialize(data);
  90. //cameraClient.ClientSend(json);
  91. }
  92. }
  93. public void SendVirtualkey(int virtualkey)
  94. {
  95. Debug.LogError("virtualkey:" + virtualkey);
  96. if(initChaos)
  97. {
  98. //DataFromSDK data = GetHDMPoseData();
  99. //data.timestamp = GetTimeStampSecond();
  100. //data.virtualkey = virtualkey;
  101. //data.hand_points = null;
  102. //string json = JsonUtil.Serialize(data);
  103. //cameraClient.ClientSend(json);
  104. }
  105. }
  106. public void SendHandPose(string gesture)
  107. {
  108. if(initChaos)
  109. {
  110. //DataFromSDK data = GetHDMPoseData();
  111. //data.timestamp = GetTimeStampSecond();
  112. //data.virtualkey = -1;
  113. //data.hand_points = gesture;
  114. }
  115. }
  116. //private OriginDataFromSDK GetOriginData()
  117. //{
  118. // OriginDataFromSDK data = new OriginDataFromSDK();
  119. // data.headPose = ARFrame.HeadPose;
  120. // data.fovs = ARFrame.CameraParams.fov;
  121. // data.left = ARFrame.CameraParams.leftProjection;
  122. // data.right = ARFrame.CameraParams.rightProjection;
  123. // return data;
  124. //}
  125. //public DataFromSDK GetHDMPoseData()
  126. //{
  127. // DataFromSDK data = new DataFromSDK();
  128. // Vector4 pose_v1 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(0);
  129. // Vector4 pose_v2 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(1);
  130. // Vector4 pose_v3 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(2);
  131. // Vector4 pose_v4 = HMDPoseTracker.Instance.centerCamera.cameraToWorldMatrix.GetColumn(3);
  132. // data.camera_pose = new float[,] {
  133. // {pose_v1.x,pose_v1.y,pose_v1.z,pose_v1.w },
  134. // {pose_v2.x,pose_v2.y,pose_v2.z,pose_v2.w},
  135. // {pose_v3.x,pose_v3.y,pose_v3.z,pose_v3.w },
  136. // {pose_v4.x,pose_v4.y,pose_v4.z,pose_v4.w }
  137. //};
  138. // Vector4 left_v1 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(0);
  139. // Vector4 left_v2 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(1);
  140. // Vector4 left_v3 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(2);
  141. // Vector4 left_v4 = HMDPoseTracker.Instance.leftCamera.cameraToWorldMatrix.GetColumn(3);
  142. // data.left_eye_transform = new float[,]{
  143. // { left_v1.x,left_v1.y,left_v1.z,left_v1.w},
  144. // { left_v2.x,left_v2.y,left_v2.z,left_v2.w},
  145. // { left_v3.x,left_v3.y,left_v3.z,left_v3.w},
  146. // { left_v4.x,left_v4.y,left_v4.z,left_v4.w}
  147. // };
  148. // Vector4 right_v1 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(0);
  149. // Vector4 right_v2 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(1);
  150. // Vector4 right_v3 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(2);
  151. // Vector4 right_v4 = HMDPoseTracker.Instance.rightCamera.cameraToWorldMatrix.GetColumn(3);
  152. // data.right_eye_transform = new float[,] {
  153. //{right_v1.x,right_v1.y,right_v1.z,right_v1.w},
  154. //{right_v2.x,right_v2.y,right_v2.z,right_v2.w},
  155. //{right_v3.x,right_v3.y,right_v3.z,right_v3.w },
  156. //{right_v4.x,right_v4.y,right_v4.z,right_v4.w }
  157. //};
  158. // data.origin_positon = new float[] {
  159. // ARFrame.HeadPose.position.x,
  160. // ARFrame.HeadPose.position.y,
  161. // ARFrame.HeadPose.position.z
  162. //};
  163. // data.origin_rotation = new float[] {
  164. // ARFrame.HeadPose.rotation.x,
  165. // ARFrame.HeadPose.rotation.y,
  166. // ARFrame.HeadPose.rotation.z,
  167. // ARFrame.HeadPose.rotation.w
  168. //};
  169. // return data;
  170. //}
  171. private void Update()
  172. {
  173. if(initChaos)
  174. {
  175. if(!alreadySendIp)
  176. {
  177. alreadySendIp = SendIp();
  178. }
  179. }
  180. }
  181. private void OnApplicationQuit()
  182. {
  183. //cameraClient.Dispose();
  184. //UDPServer.Instance.Dispose();
  185. //cameraBuffClient.Dispose();
  186. }
  187. }
  188. }