WebSocketClientStandAlone.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public class WebSocketClientStandAlone : SimpleWebClient
  7. {
  8. readonly ClientSslHelper sslHelper;
  9. readonly ClientHandshake handshake;
  10. readonly TcpConfig tcpConfig;
  11. Connection conn;
  12. internal WebSocketClientStandAlone(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig) : base(maxMessageSize, maxMessagesPerTick)
  13. {
  14. #if UNITY_WEBGL && !UNITY_EDITOR
  15. throw new NotSupportedException();
  16. #else
  17. sslHelper = new ClientSslHelper();
  18. handshake = new ClientHandshake();
  19. this.tcpConfig = tcpConfig;
  20. #endif
  21. }
  22. public override void Connect(Uri serverAddress)
  23. {
  24. state = ClientState.Connecting;
  25. // create connection here before thread so that send queue exist before connected
  26. TcpClient client = new TcpClient();
  27. tcpConfig.ApplyTo(client);
  28. // create connection object here so dispose correctly disconnects on failed connect
  29. conn = new Connection(client, AfterConnectionDisposed);
  30. Thread receiveThread = new Thread(() => ConnectAndReceiveLoop(serverAddress));
  31. receiveThread.IsBackground = true;
  32. receiveThread.Start();
  33. }
  34. void ConnectAndReceiveLoop(Uri serverAddress)
  35. {
  36. try
  37. {
  38. // connection created above
  39. TcpClient client = conn.client;
  40. conn.receiveThread = Thread.CurrentThread;
  41. try
  42. {
  43. client.Connect(serverAddress.Host, serverAddress.Port);
  44. }
  45. catch (SocketException)
  46. {
  47. client.Dispose();
  48. throw;
  49. }
  50. bool success = sslHelper.TryCreateStream(conn, serverAddress);
  51. if (!success)
  52. {
  53. Log.Warn("Failed to create Stream");
  54. conn.Dispose();
  55. return;
  56. }
  57. success = handshake.TryHandshake(conn, serverAddress);
  58. if (!success)
  59. {
  60. Log.Warn("Failed Handshake");
  61. conn.Dispose();
  62. return;
  63. }
  64. Log.Info("HandShake Successful");
  65. state = ClientState.Connected;
  66. receiveQueue.Enqueue(new Message(EventType.Connected));
  67. Thread sendThread = new Thread(() =>
  68. {
  69. SendLoop.Config sendConfig = new SendLoop.Config(
  70. conn,
  71. bufferSize: Constants.HeaderSize + Constants.MaskSize + maxMessageSize,
  72. setMask: true);
  73. SendLoop.Loop(sendConfig);
  74. });
  75. conn.sendThread = sendThread;
  76. sendThread.IsBackground = true;
  77. sendThread.Start();
  78. ReceiveLoop.Config config = new ReceiveLoop.Config(conn,
  79. maxMessageSize,
  80. false,
  81. receiveQueue,
  82. bufferPool);
  83. ReceiveLoop.Loop(config);
  84. }
  85. catch (ThreadInterruptedException e) { Log.InfoException(e); }
  86. catch (ThreadAbortException e) { Log.InfoException(e); }
  87. catch (Exception e) { Log.Exception(e); }
  88. finally
  89. {
  90. // close here in case connect fails
  91. conn?.Dispose();
  92. }
  93. }
  94. void AfterConnectionDisposed(Connection conn)
  95. {
  96. state = ClientState.NotConnected;
  97. // make sure Disconnected event is only called once
  98. receiveQueue.Enqueue(new Message(EventType.Disconnected));
  99. }
  100. public override void Disconnect()
  101. {
  102. state = ClientState.Disconnecting;
  103. Log.Info("Disconnect Called");
  104. if (conn == null)
  105. {
  106. state = ClientState.NotConnected;
  107. }
  108. else
  109. {
  110. conn?.Dispose();
  111. }
  112. }
  113. public override void Send(ArraySegment<byte> segment)
  114. {
  115. ArrayBuffer buffer = bufferPool.Take(segment.Count);
  116. buffer.CopyFrom(segment);
  117. conn.sendQueue.Enqueue(buffer);
  118. conn.sendPending.Set();
  119. }
  120. }
  121. }