Skip to content

Commit

Permalink
fix(websocket): Remove send queues (they never worked) and SSL (tempo…
Browse files Browse the repository at this point in the history
…rarily) (#879)
  • Loading branch information
Katori authored and vis2k committed May 20, 2019
1 parent 4fea1d0 commit 3c60b08
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ internal class WebSocketImplementation : WebSocket
const int MAX_PING_PONG_PAYLOAD_LEN = 125;
WebSocketCloseStatus? _closeStatus;
string _closeStatusDescription;
bool sendingMessage = false;
Queue<ArraySegment<byte>> messagesToSend = new Queue<ArraySegment<byte>>();
Queue<ArraySegment<byte>> pongMessagesToSend = new Queue<ArraySegment<byte>>();
Queue<ArraySegment<byte>> pingMessagesToSend = new Queue<ArraySegment<byte>>();

public event EventHandler<PongEventArgs> Pong;

Expand Down Expand Up @@ -222,24 +218,6 @@ public override async Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
/// If it is a multi-part message then false (and true for the last message)</param>
/// <param name="cancellationToken">the cancellation token</param>
public override async Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
{
// workaround for: https://forum.unity.com/threads/unity-2017-1-tls-1-2-still-not-working-with-net-4-6.487415/
// In SslStream, only one SendAsync can be going at a time
// if Send is called multiple time, only the first one calls SendAsync,
// the other ones queue up the message
messagesToSend.Enqueue(buffer);
if (!sendingMessage)
{
sendingMessage = true;
while (messagesToSend.Count > 0)
{
await SendAsyncInternal(messagesToSend.Dequeue(), messageType, endOfMessage, cancellationToken);
}
sendingMessage = false;
}
}

private async Task SendAsyncInternal(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
{
using (MemoryStream stream = _recycledStreamFactory())
{
Expand Down Expand Up @@ -282,20 +260,6 @@ public async Task SendPingAsync(ArraySegment<byte> payload, CancellationToken ca
throw new InvalidOperationException($"Cannot send Ping: Max ping message size {MAX_PING_PONG_PAYLOAD_LEN} exceeded: {payload.Count}");
}

pingMessagesToSend.Enqueue(payload);
if (!sendingMessage)
{
sendingMessage = true;
while (pingMessagesToSend.Count > 0)
{
await SendPingAsyncInternal(pingMessagesToSend.Dequeue(), cancellationToken);
}
sendingMessage = false;
}
}

async Task SendPingAsyncInternal(ArraySegment<byte> payload, CancellationToken cancellationToken)
{
if (_state == WebSocketState.Open)
{
using (MemoryStream stream = _recycledStreamFactory())
Expand Down Expand Up @@ -448,15 +412,14 @@ async Task SendPongAsync(ArraySegment<byte> payload, CancellationToken cancellat

try
{
pongMessagesToSend.Enqueue(payload);
if (!sendingMessage)
if (_state == WebSocketState.Open)
{
sendingMessage = true;
while (pongMessagesToSend.Count > 0)
using (MemoryStream stream = _recycledStreamFactory())
{
await SendPongAsyncInternal(pongMessagesToSend.Dequeue(), cancellationToken);
WebSocketFrameWriter.Write(WebSocketOpCode.Pong, payload, stream, true, _isClient);
Events.Log.SendingFrame(_guid, WebSocketOpCode.Pong, true, payload.Count, false);
await WriteStreamToNetwork(stream, cancellationToken);
}
sendingMessage = false;
}
}
catch (Exception ex)
Expand All @@ -466,19 +429,6 @@ async Task SendPongAsync(ArraySegment<byte> payload, CancellationToken cancellat
}
}

async Task SendPongAsyncInternal(ArraySegment<byte> payload, CancellationToken cancellationToken)
{
if (_state == WebSocketState.Open)
{
using (MemoryStream stream = _recycledStreamFactory())
{
WebSocketFrameWriter.Write(WebSocketOpCode.Pong, payload, stream, true, _isClient);
Events.Log.SendingFrame(_guid, WebSocketOpCode.Pong, true, payload.Count, false);
await WriteStreamToNetwork(stream, cancellationToken);
}
}
}

/// <summary>
/// Called when a Close frame is received
/// Send a response close frame if applicable
Expand Down
27 changes: 1 addition & 26 deletions Assets/Mirror/Runtime/Transport/Websocket/WebsocketTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ public class WebsocketTransport : Transport
[Tooltip("Nagle Algorithm can be disabled by enabling NoDelay")]
public bool NoDelay = true;

public bool Secure = false;

public string CertificatePath;

public string CertificatePassword;

public WebsocketTransport()
{
// dispatch the events from the server
Expand Down Expand Up @@ -57,14 +51,7 @@ public override bool Available()

public override void ClientConnect(string host)
{
if (Secure)
{
client.Connect(new Uri($"wss://{host}:{port}"));
}
else
{
client.Connect(new Uri($"ws://{host}:{port}"));
}
client.Connect(new Uri($"ws://{host}:{port}"));
}

public override bool ClientSend(int channelId, byte[] data) { client.Send(data); return true; }
Expand All @@ -76,18 +63,6 @@ public override void ClientConnect(string host)

public override void ServerStart()
{

if (Secure)
{
server._secure = Secure;
server._sslConfig = new Server.SslConfiguration
{
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Application.dataPath + CertificatePath, CertificatePassword),
ClientCertificateRequired = false,
CheckCertificateRevocation = false,
EnabledSslProtocols = System.Security.Authentication.SslProtocols.Default
};
}
server.Listen(port);
}

Expand Down

0 comments on commit 3c60b08

Please sign in to comment.