Client.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.Text;
  9. using System;
  10. using System.IO;
  11. using UnityEngine.UI;
  12. public enum RemoteAssistCMD { videoCallRequest, callStop, audioCallRequest, messageSend, imageSend, SMOPP_TASK_INFO };
  13. public enum ImageCMD { receiveImage, streamStart, streamStop, receiveVideo, pause, unpause };
  14. public class Client : MonoBehaviour
  15. {
  16. const byte PING_PACKET = 30;
  17. Texture2D textureReceived;
  18. public GameObject UserInfo;
  19. public GameObject UserCameraImage;
  20. public GameObject RemoteAssistCanvas;
  21. public static Client instance;
  22. public const int socketPort = 87;
  23. public const int webglPort = 86;
  24. public uint account_id = 0; //id акка
  25. public uint company_id;
  26. string ACCOUNT_NAME = "";
  27. bool login_sent;
  28. bool entered = false;
  29. bool dc = false;
  30. public bool connected = false;
  31. int lag = 0;
  32. double last_ping_time = 0;
  33. static string[] Servers = { "dev.prmsys.net", "localhost", "corp.prmsys.net" };
  34. static bool ShowPacketsSent = true;
  35. static bool printPacketsShort = true;
  36. static bool ShowPackets = true; //ставится только в редакторе
  37. double initial_timer;
  38. int internal_timer = 0;
  39. public double timer
  40. {
  41. get
  42. {
  43. return DateTime.Now.Ticks / 10000000.0; //секунды
  44. }
  45. }
  46. public static string ServerName;
  47. public static bool StartConnectFlag = false;
  48. #if UNITY_WEBGL
  49. public static WebSocket Mysocket;
  50. #else
  51. public static Socket Mysocket;
  52. #endif
  53. Dictionary<int, int> PacketLength = new Dictionary<int, int>();
  54. public delegate void OnReceive(byte[] bytedata);
  55. public static OnReceive[] packets = new OnReceive[255];
  56. public string _dataPath = "";
  57. GameObject drone;
  58. public static ClientType clientType;
  59. public enum ClientType
  60. {
  61. Desktop,
  62. Web
  63. }
  64. public static void SendEnqueue(byte[] bytedata)
  65. {
  66. SendQueue.Enqueue(bytedata);
  67. }
  68. private void Awake()
  69. {
  70. textureReceived = new Texture2D(2, 2);
  71. instance = this;
  72. ServerName = PlayerPrefs.GetString("server");
  73. if (ServerName == "")
  74. ServerName = Servers[0];
  75. Register(1, Disconnect, 5);
  76. Register(2, Login, 9);
  77. Register(3, User.CoordinatesReceive);
  78. Register(30, Myping, 3);
  79. Register(47, User.UsersReceive);
  80. Register(48, Beacon.ReceiveBeacons);
  81. Register(51, Wall.ReceiveWalls);
  82. Register(52, Zone.ReceiveZones);
  83. Register(54, Location.ReceiveLocations);
  84. Register(55, ImageReceive);
  85. Register(57, Location.TextureGetURL);
  86. Register(58, User.LogInOut, 6);
  87. Register(59, RemoteAssistCallCallback);
  88. //set data path
  89. //if (Application.platform == RuntimePlatform.WindowsWebPlayer ||
  90. // Application.platform == RuntimePlatform.OSXWebPlayer)
  91. if (Application.platform == RuntimePlatform.WebGLPlayer)
  92. {
  93. _dataPath = Application.dataPath + "/StreamingAssets";
  94. clientType = ClientType.Web;
  95. }
  96. else if (Application.platform == RuntimePlatform.Android)
  97. {
  98. _dataPath = "jar:file://" + Application.dataPath + "!/assets";
  99. clientType = ClientType.Web;
  100. }
  101. else
  102. {
  103. _dataPath = "file://" + Application.dataPath + "/StreamingAssets";
  104. clientType = ClientType.Desktop;
  105. }
  106. _dataPath = Application.streamingAssetsPath;
  107. //Debug.Log("_dataPath " + _dataPath);
  108. remoteScript_ = FindObjectOfType<RemoteClickAction>();
  109. }
  110. public int LagCount
  111. {
  112. get
  113. {
  114. return lagpoints.Count;
  115. }
  116. }
  117. int totallag
  118. {
  119. get
  120. {
  121. int q = 0;
  122. foreach (var g in lagpoints)
  123. {
  124. q += g;
  125. }
  126. return q;
  127. }
  128. }
  129. Queue<int> lagpoints = new Queue<int>();
  130. public int Averagelag
  131. {
  132. get
  133. {
  134. if (lagpoints.Count > 0)
  135. return totallag / lagpoints.Count;
  136. return 0;
  137. }
  138. }
  139. //private Button exitButton;
  140. private Button loginButton;
  141. private Button cancelButton;
  142. //private Text sysInfoTxt;
  143. private Text loginInputText;
  144. private Text passInputText;
  145. private InputField passInputField;
  146. private Animator loginCanvasAnim;
  147. private RemoteClickAction remoteScript_;
  148. /// <summary>
  149. /// Videocall accept from mobile client
  150. /// </summary>
  151. void RemoteAssistCallCallback(byte[] bytedata)
  152. {
  153. var cmd = (RemoteAssistCMD)bytedata[5];
  154. uint fromId = BitConverter.ToUInt32(bytedata, 6);
  155. RemoteAssistCanvas.SetActive(true);
  156. Debug.Log(cmd);
  157. switch (cmd)
  158. {
  159. case RemoteAssistCMD.videoCallRequest:
  160. //Сделать активной кнопку звонка!!!
  161. //RemoteAssistCanvas.set
  162. remoteScript_.StartSession(fromId);
  163. break;
  164. case RemoteAssistCMD.SMOPP_TASK_INFO:
  165. uint taskId = BitConverter.ToUInt32(bytedata, 10);
  166. int nameLen = BitConverter.ToInt32(bytedata, 14);
  167. string name = Encoding.UTF8.GetString(bytedata, 18, nameLen);
  168. Debug.Log("taskId " + taskId + " Команда "+name +" Команда " + name + " name "+name);
  169. var user = User.Find(remoteScript_.SessionCallerId);
  170. if (user != null)
  171. remoteScript_.callerName_.text = $"Звонок от [{ user.id }] { user.name_user} Проблема с командой: [{taskId}] {name}";
  172. else
  173. remoteScript_.callerName_.text = $"Звонок от сотрудника [ID: { remoteScript_.SessionCallerId }] Проблема с командой: [{taskId}] {name}";
  174. break;
  175. }
  176. }
  177. public void Myping(byte[] bytedata)
  178. {
  179. last_ping_time = timer;
  180. int lag = BitConverter.ToUInt16(bytedata, 1);
  181. //print("Myping " + lag);
  182. this.lag = lag;
  183. if (lagpoints.Count > 5)
  184. lagpoints.Dequeue();
  185. lagpoints.Enqueue(lag);
  186. List<byte> list = new List<byte>();
  187. list.Add(30);
  188. list.Add(1);
  189. SendEnqueue(list.ToArray());
  190. }
  191. public void Disconnect(byte[] bytedata)
  192. {
  193. uint bid = BitConverter.ToUInt32(bytedata, 1);
  194. if (bid == account_id)
  195. Exit();
  196. else
  197. {
  198. print("disc id " + bid);
  199. }
  200. }
  201. public void Exit()
  202. {
  203. print("Client Exit");
  204. connected = false;
  205. login_sent = false;
  206. if (Mysocket != null)
  207. {
  208. Mysocket.Close();
  209. Mysocket = null;
  210. }
  211. connect_started = new DateTime();
  212. totalbytes = 0;
  213. account_id = 0;
  214. ACCOUNT_NAME = "";
  215. //connected = false;
  216. sendDone = false;
  217. receiveDone = false;
  218. connectDone = false;
  219. StartConnectFlag = false;
  220. UnityEngine.SceneManagement.SceneManager.LoadScene(0);
  221. }
  222. //пакет с 1 параметром = 1
  223. public static void SendOneBytePacket(byte num)
  224. {
  225. SendOneByteParamPacket(num, 1);
  226. }
  227. public static void SendOneByteTwoParamsPacket(byte num, byte param1, byte param2)
  228. {
  229. byte[] data = { num, param1, param2 };
  230. SendEnqueue(data);
  231. }
  232. public static void SendThreeParamsIntPacket(byte num, int param1, int param2, int param3)
  233. {
  234. List<byte> list = new List<byte>();
  235. list.Add(num);
  236. list.AddRange(BitConverter.GetBytes(param1));
  237. list.AddRange(BitConverter.GetBytes(param2));
  238. list.AddRange(BitConverter.GetBytes(param3));
  239. SendEnqueue(list.ToArray());
  240. }
  241. //пакет с 1 1-байтовым параметром
  242. public static void SendOneByteParamPacket(byte num, byte param)
  243. {
  244. byte[] data = { num, param };
  245. byteSend(data);
  246. }
  247. //пакет с 1 4-байтовым параметром
  248. public static void SendTwoByteParamPacket(byte num, ushort param)
  249. {
  250. List<byte> list = new List<byte>();
  251. list.Add(num);
  252. list.AddRange(BitConverter.GetBytes(param));
  253. SendEnqueue(list.ToArray());
  254. }
  255. //пакет с 1 4-байтовым параметром
  256. public static void SendFourByteParamPacket(byte num, uint param)
  257. {
  258. List<byte> list = new List<byte>();
  259. list.Add(num);
  260. list.AddRange(BitConverter.GetBytes(param));
  261. SendEnqueue(list.ToArray());
  262. }
  263. public static DateTime connect_started;
  264. public void Login(byte[] bytedata)
  265. {
  266. uint accid = BitConverter.ToUInt32(bytedata, 1);
  267. if (accid == 0xFFFFFFFF)
  268. {
  269. Debug.LogError("Login or password incorrect");
  270. AuthorizationController.error = true;
  271. return;
  272. }
  273. else if (accid == 0xFFFFFFFE)
  274. {
  275. Debug.LogError("Account was already connected");
  276. return;
  277. }
  278. else if (accid == 0xFFFFFFFD)
  279. {
  280. Debug.LogError("Incorrect client version");
  281. return;
  282. }
  283. company_id = BitConverter.ToUInt32(bytedata, 5);
  284. AuthorizationController.success = true;
  285. account_id = accid;
  286. //Location.LocationsRequest();
  287. }
  288. #region system functions
  289. public void Register(int packnum, OnReceive func)
  290. {
  291. packets[packnum] = func;
  292. }
  293. private void Register(int packnum, OnReceive func, int len)
  294. {
  295. packets[packnum] = func;
  296. if (len > 1)
  297. PacketLength[packnum] = len;
  298. }
  299. public int GetLength(byte[] data, int num, int offset)
  300. {
  301. int leng;
  302. if (!PacketLength.ContainsKey(num))
  303. {
  304. var rest = data.Length - offset;
  305. //Debug.Log("GetLength rest " + rest+" offset "+ offset);
  306. //printBytes(data);
  307. //packet not full
  308. if (rest < 5)
  309. return 1;
  310. leng = BitConverter.ToInt32(data, offset + 1);
  311. }
  312. else
  313. leng = PacketLength[num];
  314. return leng;
  315. }
  316. int maximumPacketsPerUpdate = 50;
  317. public string tempflag;
  318. public static void printBytes(byte[] bytedata, bool sent = false)
  319. {
  320. var num = bytedata[0];
  321. if (num == PING_PACKET)// || num==55)
  322. return;
  323. var func = packets[num];
  324. string callBackName = "Unknown packet ";
  325. if (func != null)
  326. callBackName = packets[num].Method.Name + " ";
  327. string prefix;
  328. if (!sent)
  329. {
  330. if (!ShowPackets)
  331. return;
  332. prefix = "received ";
  333. }
  334. else
  335. {
  336. if (!ShowPacketsSent)
  337. return;
  338. prefix = "sent ";
  339. }
  340. byte[] newbuf = new byte[bytedata.Length];
  341. Array.Copy(bytedata, newbuf, bytedata.Length);
  342. string mygg = prefix + $"{callBackName}[{num}] length " + bytedata.Length; //Печатаем отправленные байты!
  343. if (!printPacketsShort)
  344. {
  345. mygg += " bytes: ";
  346. foreach (byte b in newbuf)
  347. {
  348. mygg += b + " ";
  349. }
  350. }
  351. Debug.Log(mygg);
  352. }
  353. public void Update()
  354. {
  355. #if UNITY_WEBGL
  356. Receive();
  357. #endif
  358. if (instance == null || !entered) //вход не нажат, ждем
  359. {
  360. return;
  361. }
  362. else
  363. {
  364. if (!StartConnectFlag)
  365. {
  366. StartCoroutine(Connect());
  367. StartConnectFlag = true;
  368. }
  369. }
  370. int from_connect = DateTime.Now.Subtract(connect_started).Seconds;
  371. if (from_connect > 7 && from_connect != DateTime.Now.Second && account_id == 0)
  372. {
  373. Exit();
  374. }
  375. var processedCount = 0;
  376. while (PacketQueue.Count > 0 && processedCount < maximumPacketsPerUpdate) //если длина очереди с пакетами ==0
  377. {
  378. byte[] packetbytes;
  379. lock (packetqueuelock)
  380. {
  381. packetbytes = PacketQueue.Dequeue();
  382. }
  383. int offset = 0;
  384. int totallen = 0;
  385. int num = packetbytes[0];
  386. int leng = GetLength(packetbytes, packetbytes[0], 0);
  387. if (leng <= packetbytes.Length)
  388. {
  389. while (offset < packetbytes.Length)
  390. {
  391. num = packetbytes[offset];
  392. leng = GetLength(packetbytes, num, offset);
  393. totallen += leng;
  394. byte[] newpack = new byte[leng];
  395. Array.Copy(packetbytes, offset, newpack, 0, leng);
  396. offset += leng;
  397. printBytes(newpack);
  398. processedCount++;
  399. packets[num](newpack); //запустить OnReceive функцию
  400. }
  401. }
  402. }
  403. }
  404. public void Start()
  405. {
  406. tempflag = "";
  407. entered = true;
  408. CreateCameraImage();
  409. }
  410. void CreateCameraImage()
  411. {
  412. UserInfo = new GameObject();
  413. var canvas = GameObject.Find("Canvas");
  414. UserCameraImage = new GameObject();
  415. UserCameraImage.transform.parent = canvas.transform;
  416. UserCameraImage.name = "UserCameraImage";
  417. UserCameraImage.transform.localScale = new Vector3(1, 1, 1);
  418. UserCameraImage.transform.localPosition = new Vector3(0, 0, 0);
  419. UserCameraImage.transform.localEulerAngles = new Vector3(0, 0, 0);
  420. var rawimg = UserCameraImage.AddComponent<RawImage>();
  421. rawimg.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0f, 640f);
  422. rawimg.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0f, 480f);
  423. var button = UserCameraImage.AddComponent<Button>();
  424. button.onClick.AddListener(CloseInfo);
  425. UserCameraImage.SetActive(false);
  426. }
  427. void SetResolution(float width, float height)
  428. {
  429. if (UserCameraImage != null)
  430. {
  431. UserCameraImage.transform.localEulerAngles = new Vector3(0, 0, 0);
  432. var rawimg = UserCameraImage.GetComponent<RawImage>();
  433. if (rawimg != null)
  434. {
  435. var proportions = width / height;
  436. width = 480f * proportions;
  437. height = 480f;
  438. // RawImage.transform.localScale = new Vector3(1, proportions, 1);
  439. //else RawImage.transform.localScale = new Vector3(targets[select_target].x / targets[select_target].y, 1, 1);
  440. rawimg.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0f, width);
  441. rawimg.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0f, height);
  442. }
  443. }
  444. }
  445. void CloseInfo()
  446. {
  447. if (UserCameraImage != null)
  448. {
  449. UserCameraImage.SetActive(false);
  450. var player = PlayerController.instance;
  451. player.UserInfo.SetActive(false);
  452. }
  453. }
  454. public void LateUpdate()
  455. {
  456. while (SendQueue.Count > 0)
  457. {
  458. //print("SendQueue.Count " + SendQueue.Count);
  459. byte[] sendstring = SendQueue.Dequeue();
  460. //print("sendstring len " + sendstring.Length);
  461. byteSend(sendstring);
  462. }
  463. }
  464. public class StateObject
  465. {
  466. // Client socket.
  467. // Size of receive buffer.
  468. public const int BufferSize = 128000;
  469. // Receive buffer.
  470. public byte[] buffer = new byte[BufferSize];
  471. // Received data string.
  472. }
  473. public static Queue<byte[]> PacketQueue = new Queue<byte[]>();
  474. static Queue<byte[]> SendQueue = new Queue<byte[]>();
  475. public static bool receiveDone, connectDone, sendDone = false;
  476. public static StateObject state = new StateObject();
  477. private IEnumerator Connect()
  478. {
  479. Debug.Log("Connect");
  480. // Connect to a remote server.
  481. #if UNITY_WEBGL
  482. var uristring = $"ws://{ServerName}:{webglPort}/ws";
  483. Debug.Log($"WebSocket tryconnect: {uristring}");
  484. WebSocket client = new WebSocket(new Uri(uristring));
  485. Mysocket = client;
  486. yield return StartCoroutine(Mysocket.Connect());
  487. connectDone = (Mysocket.error == null);
  488. if (!connectDone)
  489. {
  490. Debug.Log("WebSocket error: " + Mysocket.error);
  491. yield break;
  492. }
  493. #else
  494. // Establish the remote endpoint for the socket.
  495. /*IPAddress ip = */
  496. new IPAddress(new byte[] { 127, 0, 0, 1 });
  497. IPHostEntry ipHostInfo = Dns.GetHostEntry(ServerName);
  498. //string localHost = Dns.GetHostName();
  499. //print("localHost " + ipHostInfo.AddressList[0]);
  500. //IPHostEntry ipHostInfo = Dns.GetHostEntry(localHost);
  501. IPAddress[] ipv4Addresses = Array.FindAll(ipHostInfo.AddressList, a => a.AddressFamily == AddressFamily.InterNetwork);
  502. //print("localHost v4 " + ipv4Addresses[0]);
  503. // IPAddress ipAddress = ipHostInfo.AddressList[0];
  504. IPAddress ipAddress = ipv4Addresses[0];
  505. //IPEndPoint remoteEP = new IPEndPoint(ip, socketPort);
  506. IPEndPoint remoteEP = new IPEndPoint(ipAddress, socketPort);
  507. // Create a TCP/IP socket.
  508. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  509. //Socket client = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  510. print("remoteEP.AddressFamily " + AddressFamily.InterNetwork);
  511. print("client " + client.AddressFamily);
  512. Mysocket = client;
  513. // Connect to the remote endpoint.
  514. try
  515. {
  516. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
  517. }
  518. catch (Exception e)
  519. {
  520. print(e.Message);
  521. }
  522. int num = 0;
  523. while (!connectDone && num < 10)
  524. {
  525. num++;
  526. yield return new WaitForSeconds(0.2f);
  527. }
  528. if (!connectDone)
  529. {
  530. print(socketPort + " port failed");
  531. yield break;
  532. }
  533. #endif
  534. print("connected");
  535. connected = true;
  536. while (true)
  537. {
  538. if (!entered)
  539. break;
  540. while (!sendDone && connected)
  541. {
  542. yield return 1;
  543. }
  544. Receive();
  545. while (!receiveDone && connected)
  546. {
  547. yield return 1;
  548. }
  549. receiveDone = false;
  550. break;
  551. }
  552. yield break;
  553. }
  554. private static void ConnectCallback(IAsyncResult ar)
  555. {
  556. // Retrieve the socket from the state object.
  557. Socket client = (Socket)ar.AsyncState;
  558. connect_started = DateTime.Now;
  559. // Complete the connection.
  560. try
  561. {
  562. client.EndConnect(ar);
  563. }
  564. catch (Exception e)
  565. {
  566. print(e.Message);
  567. }
  568. // Signal that the connection has been made.
  569. connectDone = true;
  570. }
  571. private void Receive()
  572. {
  573. #if UNITY_WEBGL
  574. CheckWebSocket();
  575. if (Mysocket == null)
  576. return;
  577. byte[] packet = null;
  578. do
  579. {
  580. packet = Mysocket.Recv();
  581. if (packet != null)
  582. {
  583. lock (packetqueuelock)
  584. {
  585. PacketQueue.Enqueue(packet);
  586. }
  587. receiveDone = true;
  588. totalbytes += packet.Length;
  589. }
  590. }
  591. while (packet != null);
  592. #else
  593. if (Mysocket != null)
  594. {
  595. if (state != null)
  596. Mysocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
  597. }
  598. #endif
  599. }
  600. public static object packetqueuelock = new object();
  601. public static Queue<byte[]> tempqueue = new Queue<byte[]>();
  602. public bool CheckLength(byte[] packetbytes)
  603. {
  604. if (packetbytes.Length > 0) //если длина очереди с пакетами ==0
  605. {
  606. int offset = 0;
  607. //TODO проверка на мусор
  608. int num = packetbytes[0];
  609. int leng = GetLength(packetbytes, packetbytes[0], 0);
  610. if (leng <= packetbytes.Length)
  611. {
  612. while (offset < packetbytes.Length)
  613. {
  614. num = packetbytes[offset];
  615. leng = GetLength(packetbytes, num, offset);
  616. if (leng == 1)
  617. {
  618. print("Error: CheckLength: packet not complete");
  619. return false;
  620. }
  621. //print("Checking Length of " + num + " len " + leng);
  622. if (leng == 0)
  623. {
  624. print("CheckLength2 break! length error " + num);
  625. dc = true;
  626. break;
  627. }
  628. offset += leng;
  629. }
  630. }
  631. if (offset == packetbytes.Length)
  632. return true;
  633. return false;
  634. }
  635. return false;
  636. }
  637. public static int totalbytes = 0;
  638. #if !UNITY_WEBGL
  639. private void ReceiveCallback(IAsyncResult ar) //TODO если приходят данные больше длины буфера, пакеты режутся на части и складываются в обратном порядке - fix, пока что увеличим буфер
  640. {
  641. // Retrieve the state object and the client socket
  642. // from the asynchronous state object.
  643. StateObject state = (StateObject)ar.AsyncState;
  644. // Read data from the remote device.
  645. int bytesRead = 0;
  646. bytesRead = Mysocket.EndReceive(ar);
  647. if (bytesRead > 0)
  648. {
  649. // All the data has arrived; put it in response.
  650. byte[] newbuf = new Byte[bytesRead];
  651. Array.Copy(state.buffer, newbuf, bytesRead);
  652. //if (ShowPackets)
  653. //{
  654. // if (newbuf[0] != 30) //не пакет пинга
  655. // {
  656. // string mygg = ""; //Печатаем принятые байты!
  657. // foreach (byte b in newbuf)
  658. // {
  659. // mygg += b + " ";
  660. // }
  661. // print(newbuf.Length + " bytes: " + mygg);
  662. // }
  663. // totalbytes += bytesRead;
  664. //}
  665. byte[] tocheck = newbuf;
  666. if (tempqueue.Count > 0)
  667. {
  668. Queue<byte[]> myqueue = new Queue<byte[]>(tempqueue);
  669. List<byte> list = new List<byte>();
  670. while (myqueue.Count > 0)
  671. {
  672. list.AddRange(myqueue.Dequeue());
  673. }
  674. list.AddRange(newbuf);
  675. tocheck = list.ToArray();
  676. }
  677. if (!CheckLength(tocheck))
  678. {
  679. tempqueue.Clear();
  680. tempqueue.Enqueue(tocheck);
  681. }
  682. else
  683. {
  684. tempqueue.Clear();
  685. lock (packetqueuelock)
  686. {
  687. PacketQueue.Enqueue(tocheck);
  688. }
  689. }
  690. receiveDone = true;
  691. Receive();
  692. }
  693. }
  694. #endif
  695. #if UNITY_WEBGL
  696. private static void CheckWebSocket()
  697. {
  698. if (Mysocket != null && Mysocket.error != null)
  699. {
  700. Debug.LogError(Mysocket.error);
  701. instance.Exit();
  702. }
  703. }
  704. #endif
  705. private static void byteSend(byte[] tosend)
  706. {
  707. if (tosend == null)
  708. {
  709. Debug.LogError("tosend null");
  710. return;
  711. }
  712. printBytes(tosend, true);
  713. #if UNITY_WEBGL
  714. byte[] webglbuf = new Byte[tosend.Length];
  715. Array.Copy(tosend, webglbuf, tosend.Length);
  716. CheckWebSocket();
  717. if (Mysocket != null)
  718. {
  719. Mysocket.Send(webglbuf);
  720. sendDone = true;
  721. Debug.Log("websocket sendDone " + webglbuf.Length);
  722. }
  723. #else
  724. try
  725. {
  726. //print("buffer size " + Mysocket.SendBufferSize + " tosend.Length " + tosend.Length);
  727. Mysocket.BeginSend(tosend, 0, tosend.Length, 0, SendCallback, Mysocket);
  728. }
  729. catch (Exception e)
  730. {
  731. if (instance != null)
  732. {
  733. print(e.Message);
  734. instance.Exit();
  735. }
  736. }
  737. #endif
  738. }
  739. #if !UNITY_WEBGL
  740. private static void SendCallback(IAsyncResult ar)
  741. {
  742. // Retrieve the socket from the state object.
  743. Socket client = (Socket)ar.AsyncState;
  744. // Complete sending the data to the remote device.
  745. /*int bytesSent = */
  746. client.EndSend(ar);
  747. //print("Sent " + bytesSent + " bytes to server.");
  748. // Signal that all bytes have been sent.
  749. sendDone = true;
  750. }
  751. #endif
  752. #endregion
  753. public static string GetMD5Hash(string input)
  754. {
  755. System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
  756. byte[] bs = Encoding.UTF8.GetBytes(input);
  757. bs = x.ComputeHash(bs);
  758. StringBuilder s = new StringBuilder();
  759. foreach (byte b in bs)
  760. {
  761. s.Append(b.ToString("x2").ToLower());
  762. }
  763. string password = s.ToString();
  764. return password;
  765. }
  766. /// <summary>
  767. /// Запрос координат с сервера, timeStart, timeEnd == DateTime.Ticks
  768. /// </summary>
  769. public void CoordinatesRequest(long timeStart, long timeEnd, byte resolution, uint locationID, uint account_id)
  770. {
  771. login_sent = true;
  772. Debug.Log("SendRequestCoordinates");
  773. List<byte> list = new List<byte>();
  774. list.Add(3);
  775. list.AddRange(BitConverter.GetBytes(account_id));
  776. list.AddRange(BitConverter.GetBytes(timeStart));
  777. list.AddRange(BitConverter.GetBytes(timeEnd));
  778. list.Add(resolution);
  779. list.AddRange(BitConverter.GetBytes(locationID));
  780. SendEnqueue(list.ToArray());
  781. }
  782. public static byte[] ConstructVariablePacket(byte num, byte[] vardatabytes)
  783. {
  784. List<byte> bytedata = new List<byte>();
  785. bytedata.Add(num);
  786. int len = vardatabytes.Length + 5;
  787. bytedata.AddRange(BitConverter.GetBytes(len));
  788. bytedata.AddRange(vardatabytes);
  789. return bytedata.ToArray();
  790. }
  791. public void MessageReceiver(string data)
  792. {
  793. var expl = data.Split(' ');
  794. var accname = expl[0];
  795. //account_id = Convert.ToUInt32(expl[1]);
  796. //company_id = Convert.ToUInt32(expl[2]);
  797. SendLogin(accname, "333666999");
  798. //tempflag = data + " " + accname;
  799. }
  800. /// <summary>
  801. /// Sender: current account, receiver account: target
  802. /// </summary>
  803. /// <param name="target">receiver account</param>
  804. public void RemoteAssistRequestVideoCall(uint targetId, RemoteAssistCMD cmd)
  805. {
  806. MemoryStream ms = new MemoryStream();
  807. ms.WriteByte((byte)cmd);
  808. BinaryWriter bw = new BinaryWriter(ms);
  809. bw.Write(targetId);
  810. var data = ConstructVariablePacket(59, ms.ToArray());
  811. SendEnqueue(data);
  812. }
  813. public void RemoteAssistRequestAudioCall(User target)
  814. {
  815. }
  816. public void RemoteAssistSendChatMessage(User target)
  817. {
  818. }
  819. public void RemoteAssistScreenshotReceive()
  820. {
  821. }
  822. public void RemoteAssistScreenshotSend()
  823. {
  824. }
  825. public void SendLogin(string login, string passwordToEdit)
  826. {
  827. if (!connected)
  828. {
  829. //Debug.LogError("Couldn't connect");
  830. //Exit();
  831. return;
  832. }
  833. if (!login_sent)
  834. {
  835. login_sent = true;
  836. string pass = GetMD5Hash(passwordToEdit);
  837. byte[] bpass = Encoding.UTF8.GetBytes(pass);
  838. byte[] blogin = Encoding.UTF8.GetBytes(login);
  839. List<byte> list = new List<byte>();
  840. byte loginFlag = 1;
  841. int leng = (bpass.Length + blogin.Length + 6); //+имя+длина
  842. list.Add(2);
  843. list.AddRange(BitConverter.GetBytes(leng));
  844. list.Add(loginFlag);
  845. list.AddRange(bpass);
  846. list.AddRange(blogin);
  847. SendEnqueue(list.ToArray());
  848. }
  849. }
  850. #region test functions
  851. public void ImageReceive(byte[] bytedata)
  852. {
  853. var cmd = (ImageCMD)bytedata[5];
  854. switch (cmd)
  855. {
  856. case ImageCMD.receiveImage:
  857. var fromId = BitConverter.ToUInt32(bytedata, 6); //кому пришло
  858. User from = User.Find(fromId);
  859. if (from != null)
  860. {
  861. var imglen = bytedata.Length - 10;
  862. byte[] rawdata = new byte[imglen];
  863. Array.Copy(bytedata, 10, rawdata, 0, imglen);
  864. textureReceived.LoadImage(rawdata);
  865. if (remoteScript_.SessionCallerId == fromId)
  866. {
  867. //UserCameraImage == remoteassist image
  868. remoteScript_.remoteCamImage.texture = textureReceived;
  869. }
  870. else if (UserCameraImage != null)
  871. {
  872. SetResolution(textureReceived.width, textureReceived.height);
  873. UserCameraImage.SetActive(true);
  874. UserCameraImage.GetComponent<RawImage>().texture = textureReceived;
  875. var player = PlayerController.instance;
  876. UserCameraImage.transform.SetParent(player.UserInfo.transform);
  877. player.UserInfo.SetActive(player.broadcast);
  878. }
  879. }
  880. else
  881. {
  882. Debug.LogError("Image sender not found, id: " + fromId);
  883. User.SendGetUser(fromId);
  884. }
  885. break;
  886. case ImageCMD.streamStart: break;
  887. case ImageCMD.streamStop: break;
  888. }
  889. }
  890. public void ImageStreamStartSend(uint targetAccountId)
  891. {
  892. MemoryStream ms = new MemoryStream();
  893. ms.WriteByte((byte)ImageCMD.streamStart);
  894. BinaryWriter bw = new BinaryWriter(ms);
  895. bw.Write(targetAccountId);
  896. //ms.Write(bdata, 0, bdata.Length);
  897. //Debug.Log("imagesend bytes "+ms.ToArray().Length);
  898. var data = ConstructVariablePacket(55, ms.ToArray());
  899. SendEnqueue(data);
  900. }
  901. public void ImageStreamStopSend(uint accid)
  902. {
  903. MemoryStream ms = new MemoryStream();
  904. ms.WriteByte((byte)ImageCMD.streamStop);
  905. BinaryWriter bw = new BinaryWriter(ms);
  906. bw.Write(accid);
  907. //ms.Write(bdata, 0, bdata.Length);
  908. //Debug.Log("imagesend bytes "+ms.ToArray().Length);
  909. var data = ConstructVariablePacket(55, ms.ToArray());
  910. SendEnqueue(data);
  911. }
  912. public static void ImageStreamControlCommandSend(uint accid, ImageCMD cmd)
  913. {
  914. MemoryStream ms = new MemoryStream();
  915. ms.WriteByte((byte) cmd);
  916. BinaryWriter bw = new BinaryWriter(ms);
  917. bw.Write(accid);
  918. //ms.Write(bdata, 0, bdata.Length);
  919. //Debug.Log("imagesend bytes "+ms.ToArray().Length);
  920. var data = ConstructVariablePacket(55, ms.ToArray());
  921. SendEnqueue(data);
  922. }
  923. public void ImageSend(byte[] bdata, uint targetAccountId)
  924. {
  925. print("ImageSend " + targetAccountId);
  926. MemoryStream ms = new MemoryStream();
  927. ms.WriteByte((byte)ImageCMD.receiveImage);
  928. ms.Write(BitConverter.GetBytes(targetAccountId), 0, 4);
  929. ms.Write(bdata, 0, bdata.Length);
  930. var data = ConstructVariablePacket(55, ms.ToArray());
  931. SendEnqueue(data);
  932. }
  933. public void OnGUI()
  934. {
  935. //if (GUI.Button(new Rect(250, 350, 160, 64), "Test VideoCall!"))
  936. //{
  937. // RemoteAssistRequestVideoCall(4);
  938. //}
  939. //if (GUI.Button(new Rect(250, 280, 160, 64), "Test"))
  940. //{
  941. // var fbytes = File.ReadAllBytes("Assets/Resources/Image/photo_2020-04-15_15-40-54.jpg");
  942. // print("fbytes " + fbytes.Length);
  943. // ImageSend(fbytes);
  944. //}
  945. //if (GUI.Button(new Rect(250, 350, 160, 64), "Test Login!"))
  946. //{
  947. // SendLogin("Admin12", "333666999");
  948. // account_id = 1;
  949. // company_id = 2;
  950. // AuthorizationController.success = true;
  951. //}
  952. //if (tempflag != "")
  953. //{
  954. // if (GUI.Button(new Rect(250, 350, 160, 64), "Stop Stream Image!"))
  955. // {
  956. // ImageStreamStopSend(4);
  957. // }
  958. //}
  959. //if (tempflag != "")
  960. //{
  961. // if (GUI.Button(new Rect(250, 280, 160, 64), "Start Stream Image!"))
  962. // {
  963. // var fbytes = File.ReadAllBytes("Assets/Resources/Image/photo_2020-04-15_15-40-54.jpg");
  964. // print("fbytes " + fbytes.Length);
  965. // ImageSend(fbytes);
  966. // }
  967. //}
  968. }
  969. #endregion
  970. }