Skip to content

Commit c722956

Browse files
committed
Buf fix:When stop QpWebSocketServer Channels didn't clear.
1 parent 69a6a98 commit c722956

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

Quick.Protocol.WebSocket.Server.AspNetCore/QpWebSocketServer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ private class WebSocketContext
1717
{
1818
public string ConnectionInfo { get; set; }
1919
public System.Net.WebSockets.WebSocket WebSocket { get; set; }
20-
public CancellationTokenSource Cts { get; set; }
2120

2221
public WebSocketContext(string connectionInfo, System.Net.WebSockets.WebSocket webSocket, CancellationTokenSource cts)
2322
{
2423
ConnectionInfo = connectionInfo;
2524
WebSocket = webSocket;
26-
Cts = cts;
2725
}
2826
}
2927

@@ -86,7 +84,7 @@ protected override async Task InnerAcceptAsync(CancellationToken token)
8684
{
8785
if (LogUtils.LogConnection)
8886
LogUtils.Log("[Connection]{0} connected.", context.ConnectionInfo);
89-
OnNewChannelConnected(new WebSocketServerStream(context.WebSocket, context.Cts), context.ConnectionInfo, token);
87+
OnNewChannelConnected(new WebSocketServerStream(context.WebSocket, token), context.ConnectionInfo, token);
9088
}
9189
catch (Exception ex)
9290
{

Quick.Protocol.WebSocket.Server.AspNetCore/Quick.Protocol.WebSocket.Server.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<PackageProjectUrl>https://github.com/QuickProtocol</PackageProjectUrl>
77
<RepositoryUrl>https://github.com/QuickProtocol/QuickProtocol_CSharp</RepositoryUrl>
8-
<Version>2.1.8</Version>
8+
<Version>2.1.9</Version>
99
<Authors>scbeta</Authors>
1010
<Company />
1111
<Product>Quick.Protocol</Product>

Quick.Protocol.WebSocket.Server.AspNetCore/WebSocketServerStream.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace Quick.Protocol.WebSocket.Server.AspNetCore
1111
internal class WebSocketServerStream : Stream
1212
{
1313
private System.Net.WebSockets.WebSocket webSocket;
14-
private CancellationTokenSource cts = null;
14+
private CancellationToken cancellationToken;
1515
private const int ReadSize = 1024 * 4;
1616
private byte[] readBuffer = new byte[ReadSize];
1717

18-
public WebSocketServerStream(System.Net.WebSockets.WebSocket webSocket, CancellationTokenSource cts)
18+
public WebSocketServerStream(System.Net.WebSockets.WebSocket webSocket, CancellationToken cancellationToken)
1919
{
2020
this.webSocket = webSocket;
21-
this.cts = cts;
21+
this.cancellationToken = cancellationToken;
2222
}
2323

2424
public override bool CanSeek => throw new NotImplementedException();
@@ -35,39 +35,23 @@ public override void Flush() { }
3535

3636
public override int Read(byte[] buffer, int offset, int count)
3737
{
38-
try
39-
{
40-
var result = webSocket.ReceiveAsync(new ArraySegment<byte>(buffer, offset, count), cts.Token).Result;
41-
return result.Count;
42-
}
43-
catch
44-
{
45-
cts.Cancel();
46-
return 0;
47-
}
38+
var result = webSocket.ReceiveAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken).Result;
39+
return result.Count;
4840
}
4941

5042

5143
public override void Write(byte[] buffer, int offset, int count)
5244
{
53-
try
54-
{
55-
webSocket.SendAsync(
56-
new ArraySegment<byte>(buffer, offset, count),
57-
System.Net.WebSockets.WebSocketMessageType.Binary,
58-
true,
59-
cts.Token)
60-
.Wait();
61-
}
62-
catch
63-
{
64-
cts.Cancel();
65-
}
45+
webSocket.SendAsync(
46+
new ArraySegment<byte>(buffer, offset, count),
47+
System.Net.WebSockets.WebSocketMessageType.Binary,
48+
true,
49+
cancellationToken)
50+
.Wait();
6651
}
6752

6853
protected override void Dispose(bool disposing)
6954
{
70-
cts.Cancel();
7155
webSocket.Dispose();
7256
base.Dispose(disposing);
7357
}

0 commit comments

Comments
 (0)