SimpleWebClient.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Concurrent;
  3. using UnityEngine;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public enum ClientState
  7. {
  8. NotConnected = 0,
  9. Connecting = 1,
  10. Connected = 2,
  11. Disconnecting = 3,
  12. }
  13. /// <summary>
  14. /// Client used to control websockets
  15. /// <para>Base class used by WebSocketClientWebGl and WebSocketClientStandAlone</para>
  16. /// </summary>
  17. public abstract class SimpleWebClient
  18. {
  19. public static SimpleWebClient Create(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig)
  20. {
  21. #if UNITY_WEBGL && !UNITY_EDITOR
  22. return new WebSocketClientWebGl(maxMessageSize, maxMessagesPerTick);
  23. #else
  24. return new WebSocketClientStandAlone(maxMessageSize, maxMessagesPerTick, tcpConfig);
  25. #endif
  26. }
  27. readonly int maxMessagesPerTick;
  28. protected readonly int maxMessageSize;
  29. public readonly ConcurrentQueue<Message> receiveQueue = new ConcurrentQueue<Message>();
  30. protected readonly BufferPool bufferPool;
  31. protected ClientState state;
  32. protected SimpleWebClient(int maxMessageSize, int maxMessagesPerTick)
  33. {
  34. this.maxMessageSize = maxMessageSize;
  35. this.maxMessagesPerTick = maxMessagesPerTick;
  36. bufferPool = new BufferPool(5, 20, maxMessageSize);
  37. }
  38. public ClientState ConnectionState => state;
  39. public event Action onConnect;
  40. public event Action onDisconnect;
  41. public event Action<ArraySegment<byte>> onData;
  42. public event Action<Exception> onError;
  43. /// <summary>
  44. /// Processes all new messages
  45. /// </summary>
  46. public void ProcessMessageQueue()
  47. {
  48. ProcessMessageQueue(null);
  49. }
  50. /// <summary>
  51. /// Processes all messages while <paramref name="behaviour"/> is enabled
  52. /// </summary>
  53. /// <param name="behaviour"></param>
  54. public void ProcessMessageQueue(MonoBehaviour behaviour)
  55. {
  56. int processedCount = 0;
  57. bool skipEnabled = behaviour == null;
  58. // check enabled every time in case behaviour was disabled after data
  59. while (
  60. (skipEnabled || behaviour.enabled) &&
  61. processedCount < maxMessagesPerTick &&
  62. // Dequeue last
  63. receiveQueue.TryDequeue(out Message next)
  64. )
  65. {
  66. processedCount++;
  67. switch (next.type)
  68. {
  69. case EventType.Connected:
  70. onConnect?.Invoke();
  71. break;
  72. case EventType.Data:
  73. onData?.Invoke(next.data.ToSegment());
  74. next.data.Release();
  75. break;
  76. case EventType.Disconnected:
  77. onDisconnect?.Invoke();
  78. break;
  79. case EventType.Error:
  80. onError?.Invoke(next.exception);
  81. break;
  82. }
  83. }
  84. }
  85. public abstract void Connect(Uri serverAddress);
  86. public abstract void Disconnect();
  87. public abstract void Send(ArraySegment<byte> segment);
  88. }
  89. }