Skip to content

Commit 2c1ad77

Browse files
committed
尝试修复一个内存泄漏问题
1 parent 6853eb1 commit 2c1ad77

File tree

5 files changed

+33
-54
lines changed

5 files changed

+33
-54
lines changed

Quick.Protocol.WebSocket.Client/WebSocketClientStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public override void Write(byte[] buffer, int offset, int count)
6666

6767
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
6868
{
69-
return client.SendAsync(new ArraySegment<byte>(buffer, offset, count), System.Net.WebSockets.WebSocketMessageType.Binary, true, CancellationToken.None);
69+
return client.SendAsync(new ArraySegment<byte>(buffer, offset, count), System.Net.WebSockets.WebSocketMessageType.Binary, true, cancellationToken);
7070
}
7171

7272
protected override void Dispose(bool disposing)

Quick.Protocol/QpChannel.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -271,47 +271,39 @@ public async Task SendCommandRequest(object request)
271271
await SendCommandRequestPackage(CommandContext.GenerateNewId(), typeName, requestContent).ConfigureAwait(false);
272272
}
273273

274-
protected void BeginHeartBeat(CancellationToken cancellationToken)
274+
protected async Task BeginHeartBeat(CancellationToken cancellationToken)
275275
{
276-
if (options.HeartBeatInterval > 0)
277-
_ = Task.Delay(options.HeartBeatInterval, cancellationToken).ContinueWith(t =>
276+
if (options.HeartBeatInterval < 0)
277+
return;
278+
while (!cancellationToken.IsCancellationRequested)
279+
{
280+
await Task.Delay(options.HeartBeatInterval, cancellationToken);
281+
if (QpPackageHandler_Stream == null)
282+
return;
283+
var lastSendPackageToNowSeconds = (DateTime.Now - lastSendPackageTime).TotalMilliseconds;
284+
//如果离最后一次发送数据包的时间大于心跳间隔,则发送心跳包
285+
if (lastSendPackageToNowSeconds > options.HeartBeatInterval)
278286
{
279-
if (t.IsCanceled)
280-
return;
281-
if (QpPackageHandler_Stream == null)
282-
return;
283-
284-
var lastSendPackageToNowSeconds = (DateTime.Now - lastSendPackageTime).TotalMilliseconds;
285-
286-
//如果离最后一次发送数据包的时间大于心跳间隔,则发送心跳包
287-
if (lastSendPackageToNowSeconds > options.HeartBeatInterval)
288-
{
289-
_ = SendHeartbeatPackage();
290-
}
291-
BeginHeartBeat(cancellationToken);
292-
});
287+
await SendHeartbeatPackage();
288+
}
289+
}
293290
}
294291

295-
protected void BeginNetstat(CancellationToken cancellationToken)
292+
protected async Task BeginNetstat(CancellationToken cancellationToken)
296293
{
297294
if (!options.EnableNetstat)
298295
return;
299-
if (cancellationToken.IsCancellationRequested)
300-
return;
301296

302-
long preBytesReceived = BytesReceived;
303-
long preBytesSent = BytesSent;
304-
305-
_ = Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ContinueWith(t =>
297+
while (!cancellationToken.IsCancellationRequested)
306298
{
307-
if (t.IsCanceled)
308-
return;
299+
long preBytesReceived = BytesReceived;
300+
long preBytesSent = BytesSent;
301+
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
309302
if (QpPackageHandler_Stream == null)
310303
return;
311304
BytesReceivedPerSec = BytesReceived - preBytesReceived;
312305
BytesSentPerSec = BytesSent - preBytesSent;
313-
BeginNetstat(cancellationToken);
314-
});
306+
}
315307
}
316308

317309

Quick.Protocol/QpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task ConnectAsync()
4040
//开始读取其他数据包
4141
BeginReadPackage(token);
4242
//开始统计网络数据
43-
BeginNetstat(token);
43+
_ = BeginNetstat(token);
4444

4545
var repConnect = await SendCommand(new Commands.Connect.Request()
4646
{
@@ -67,7 +67,7 @@ public async Task ConnectAsync()
6767
if (Options.HeartBeatInterval > 0)
6868
{
6969
//定时发送心跳包
70-
BeginHeartBeat(token);
70+
_ = BeginHeartBeat(token);
7171
}
7272
}
7373
protected override void OnWriteError(Exception exception)

Quick.Protocol/QpServer.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Text;
65
using System.Threading;
76
using System.Threading.Tasks;
87

@@ -49,7 +48,7 @@ public QpServer(QpServerOptions options)
4948
public virtual void Start()
5049
{
5150
cts = new CancellationTokenSource();
52-
beginAccept(cts.Token);
51+
_ = beginAccept(cts.Token);
5352
}
5453

5554
internal void RemoveChannel(QpServerChannel channel)
@@ -95,19 +94,12 @@ protected void OnNewChannelConnected(Stream stream, string channelName, Cancella
9594

9695
protected abstract Task InnerAcceptAsync(CancellationToken token);
9796

98-
private void beginAccept(CancellationToken token)
97+
private async Task beginAccept(CancellationToken token)
9998
{
100-
if (token.IsCancellationRequested)
101-
return;
102-
103-
InnerAcceptAsync(token).ContinueWith(task =>
99+
while (!token.IsCancellationRequested)
104100
{
105-
if (task.IsCanceled)
106-
return;
107-
if (task.IsFaulted)
108-
return;
109-
beginAccept(token);
110-
});
101+
await InnerAcceptAsync(token);
102+
}
111103
}
112104

113105
public virtual void Stop()

Quick.Protocol/QpServerChannel.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public QpServerChannel(Stream stream, string channelName, CancellationToken canc
4040
this.authedNoticeHandlerManagerList = options.NoticeHandlerManagerList;
4141

4242
cts = new CancellationTokenSource();
43-
cancellationToken.Register(() => Stop());
43+
cancellationToken.Register(Stop);
4444

4545
//初始化连接相关指令处理器
4646
var connectAndAuthCommandExecuterManager = new CommandExecuterManager();
@@ -56,7 +56,7 @@ public QpServerChannel(Stream stream, string channelName, CancellationToken canc
5656
//开始读取其他数据包
5757
BeginReadPackage(token);
5858
//开始统计网络数据
59-
BeginNetstat(token);
59+
_ = BeginNetstat(token);
6060

6161
//如果认证超时时间后没有通过认证,则断开连接
6262
if (options.AuthenticateTimeout > 0)
@@ -71,13 +71,9 @@ public QpServerChannel(Stream stream, string channelName, CancellationToken canc
7171

7272
if (stream != null)
7373
{
74-
try
75-
{
76-
stream.Close();
77-
stream.Dispose();
78-
stream = null;
79-
}
74+
try { stream.Dispose(); }
8075
catch { }
76+
stream = null;
8177
}
8278
AuchenticateTimeout?.Invoke(this, EventArgs.Empty);
8379
});
@@ -128,7 +124,7 @@ private Commands.HandShake.Response handShake(QpChannel handler, Commands.HandSh
128124

129125
//开始心跳
130126
if (options.HeartBeatInterval > 0)
131-
BeginHeartBeat(cts.Token);
127+
_ = BeginHeartBeat(cts.Token);
132128
return new Commands.HandShake.Response();
133129
}
134130

@@ -148,7 +144,6 @@ public void Stop()
148144
try
149145
{
150146
cts?.Cancel();
151-
stream?.Close();
152147
stream?.Dispose();
153148
}
154149
catch { }

0 commit comments

Comments
 (0)