SimpleWebServer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public class SimpleWebServer
  7. {
  8. readonly int maxMessagesPerTick;
  9. readonly WebSocketServer server;
  10. readonly BufferPool bufferPool;
  11. public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig)
  12. {
  13. this.maxMessagesPerTick = maxMessagesPerTick;
  14. // use max because bufferpool is used for both messages and handshake
  15. int max = Math.Max(maxMessageSize, handshakeMaxSize);
  16. bufferPool = new BufferPool(5, 20, max);
  17. server = new WebSocketServer(tcpConfig, maxMessageSize, handshakeMaxSize, sslConfig, bufferPool);
  18. }
  19. public bool Active { get; private set; }
  20. public event Action<int> onConnect;
  21. public event Action<int> onDisconnect;
  22. public event Action<int, ArraySegment<byte>> onData;
  23. public event Action<int, Exception> onError;
  24. public void Start(ushort port)
  25. {
  26. server.Listen(port);
  27. Active = true;
  28. }
  29. public void Stop()
  30. {
  31. server.Stop();
  32. Active = false;
  33. }
  34. public void SendAll(List<int> connectionIds, ArraySegment<byte> source)
  35. {
  36. ArrayBuffer buffer = bufferPool.Take(source.Count);
  37. buffer.CopyFrom(source);
  38. buffer.SetReleasesRequired(connectionIds.Count);
  39. // make copy of array before for each, data sent to each client is the same
  40. foreach (int id in connectionIds)
  41. {
  42. server.Send(id, buffer);
  43. }
  44. }
  45. public void SendOne(int connectionId, ArraySegment<byte> source)
  46. {
  47. ArrayBuffer buffer = bufferPool.Take(source.Count);
  48. buffer.CopyFrom(source);
  49. server.Send(connectionId, buffer);
  50. }
  51. public bool KickClient(int connectionId)
  52. {
  53. return server.CloseConnection(connectionId);
  54. }
  55. public string GetClientAddress(int connectionId)
  56. {
  57. return server.GetClientAddress(connectionId);
  58. }
  59. public void ProcessMessageQueue(MonoBehaviour behaviour)
  60. {
  61. int processedCount = 0;
  62. // check enabled every time in case behaviour was disabled after data
  63. while (
  64. behaviour.enabled &&
  65. processedCount < maxMessagesPerTick &&
  66. // Dequeue last
  67. server.receiveQueue.TryDequeue(out Message next)
  68. )
  69. {
  70. processedCount++;
  71. switch (next.type)
  72. {
  73. case EventType.Connected:
  74. onConnect?.Invoke(next.connId);
  75. break;
  76. case EventType.Data:
  77. onData?.Invoke(next.connId, next.data.ToSegment());
  78. next.data.Release();
  79. break;
  80. case EventType.Disconnected:
  81. onDisconnect?.Invoke(next.connId);
  82. break;
  83. case EventType.Error:
  84. onError?.Invoke(next.connId, next.exception);
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. }