Skip to content

Commit d78fe19

Browse files
committed
fix(SimpleWebTransport): Too many calls to conn.Dispose
- WebsocketServer calls it from HandshakeAndReceiveLoop - WebSocketClientStandAlone calls it from ConnectAndReceiveLoop Both in try...finally -- that's enough and minimizes confusing logging
1 parent 8f26c2c commit d78fe19

File tree

6 files changed

+8
-14
lines changed

6 files changed

+8
-14
lines changed

Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Client/StandAlone/WebSocketClientStandAlone.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ void ConnectAndReceiveLoop(Uri serverAddress)
6161
if (!success)
6262
{
6363
Log.Warn("[SWT-WebSocketClientStandAlone]: Failed to create Stream with {0}", serverAddress);
64-
conn.Dispose();
6564
return;
6665
}
6766

6867
success = handshake.TryHandshake(conn, serverAddress);
6968
if (!success)
7069
{
7170
Log.Warn("[SWT-WebSocketClientStandAlone]: Failed Handshake with {0}", serverAddress);
72-
conn.Dispose();
7371
return;
7472
}
7573

@@ -106,7 +104,8 @@ void ConnectAndReceiveLoop(Uri serverAddress)
106104
finally
107105
{
108106
// close here in case connect fails
109-
conn?.Dispose();
107+
if (conn != null && !conn.hasDisposed)
108+
conn.Dispose();
110109
}
111110
}
112111

@@ -124,8 +123,8 @@ public override void Disconnect()
124123

125124
if (conn == null)
126125
state = ClientState.NotConnected;
127-
else
128-
conn?.Dispose();
126+
else if (!conn.hasDisposed)
127+
conn.Dispose();
129128
}
130129

131130
public override void Send(ArraySegment<byte> segment)

Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Common/Connection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal sealed class Connection : IDisposable
3535
public ConcurrentQueue<ArrayBuffer> sendQueue = new ConcurrentQueue<ArrayBuffer>();
3636

3737
public Action<Connection> onDispose;
38-
volatile bool hasDisposed;
38+
volatile internal bool hasDisposed;
3939

4040
public Connection(TcpClient client, Action<Connection> onDispose)
4141
{

Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Common/ReceiveLoop.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public static void Loop(Config config)
100100
finally
101101
{
102102
Profiler.EndThreadProfiling();
103-
conn.Dispose();
104103
}
105104
}
106105

@@ -237,8 +236,6 @@ static void HandleCloseMessage(Config config, byte[] buffer, int msgOffset, int
237236
// dump after mask off
238237
Log.DumpBuffer("[SWT-ReceiveLoop]: Message", buffer, msgOffset, payloadLength);
239238
Log.Verbose("[SWT-ReceiveLoop]: Close: {0} message:{1}", GetCloseCode(buffer, msgOffset), GetCloseMessage(buffer, msgOffset, payloadLength));
240-
241-
conn.Dispose();
242239
}
243240

244241
static string GetCloseMessage(byte[] buffer, int msgOffset, int payloadLength)

Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Common/SendLoop.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public static void Loop(Config config)
121121
finally
122122
{
123123
Profiler.EndThreadProfiling();
124-
conn.Dispose();
125124
maskHelper?.Dispose();
126125
}
127126
}

Assets/Mirror/Transports/SimpleWeb/SimpleWeb/Server/WebSocketServer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ void HandshakeAndReceiveLoop(Connection conn)
109109
if (!success)
110110
{
111111
Log.Warn("[SWT-WebSocketServer]: Failed to create SSL Stream {0}", conn);
112-
conn.Dispose();
113112
return;
114113
}
115114

@@ -118,7 +117,6 @@ void HandshakeAndReceiveLoop(Connection conn)
118117
if (!success)
119118
{
120119
Log.Warn("[SWT-WebSocketServer]: Handshake failed for connection {0}", conn);
121-
conn.Dispose();
122120
return;
123121
}
124122

Assets/Mirror/Transports/SimpleWeb/SimpleWebTransport.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ public override void ClientConnect(Uri uri)
234234

235235
public override void ClientDisconnect()
236236
{
237-
// don't set client null here of messages wont be processed
238-
client?.Disconnect();
237+
// don't set client null here or messages wont be processed
238+
if (client != null && client.ConnectionState != ClientState.NotConnected)
239+
client.Disconnect();
239240
}
240241

241242
public override void ClientSend(ArraySegment<byte> segment, int channelId)

0 commit comments

Comments
 (0)