NetWorkLANManager.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using BeinLab.Util;
  2. using Newtonsoft.Json;
  3. using SC.XR.Unity;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public class NetWorkLANManager : SingletonMono<NetWorkLANManager>
  8. {
  9. public bool isServer = false;
  10. private client client;
  11. private server server;
  12. public Queue<string> queueMsg;
  13. public Queue<string> createDesMsg;
  14. private void Start()
  15. {
  16. client = this.GetComponent<client>();
  17. server = this.GetComponent<server>();
  18. queueMsg = new Queue<string>();
  19. createDesMsg = new Queue<string>();
  20. if (isServer)
  21. server.enabled = true;
  22. else
  23. client.enabled = true;
  24. TimerMgr.Instance.CreateTimer(() => { RecMessage(); }, 0.1f, -1);
  25. }
  26. public void SendData(string msg)
  27. {
  28. if(isServer)
  29. {
  30. server.SendData();
  31. }
  32. else
  33. {
  34. client.SendData();
  35. }
  36. }
  37. public void RecMessage()
  38. {
  39. if(isServer)
  40. {
  41. if (server.queueRecMsg.Count < 1)
  42. return;
  43. MessageData data = JsonConvert.DeserializeObject<MessageData>(server.queueRecMsg.Dequeue());
  44. // 同步管理器
  45. SynchronizeManager.Instance.RecMessage(data);
  46. }
  47. else
  48. {
  49. if (client.queueRecMsg.Count < 1)
  50. return;
  51. MessageData data = JsonConvert.DeserializeObject<MessageData>(client.queueRecMsg.Dequeue());
  52. // 同步管理器
  53. SynchronizeManager.Instance.RecMessage(data);
  54. }
  55. }
  56. public void AddMsgData(bool isSyn, string msg)
  57. {
  58. if (isSyn)
  59. {
  60. if (queueMsg.Count > 10)
  61. {
  62. queueMsg.Dequeue();
  63. }
  64. queueMsg.Enqueue(msg);
  65. }
  66. else
  67. createDesMsg.Enqueue(msg);
  68. }
  69. }