Skip to content

Commit bf1779e

Browse files
committed
优化传输性能。。。
1 parent aa8056f commit bf1779e

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
6363

6464
protected override void Dispose(bool disposing)
6565
{
66+
cts.Cancel();
6667
cts.Dispose();
6768
webSocket.Dispose();
6869
base.Dispose(disposing);

Quick.Protocol/QpChannel_Send.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ private static void writePackageTotalLengthToBuffer(Memory<byte> memory, int pac
176176
writePackageTotalLengthToBuffer(memory.Span, packageTotalLength);
177177
}
178178

179-
private async Task UseSendPipe(Func<Pipe, Task> handler)
179+
private async Task UseSendPipe(QpPackageType packageType, Func<Pipe, Task<int>> packageBodyHandler = null, bool ignoreCompressAndEncrypt = false)
180180
{
181181
if (options.EnableNetstat)
182182
Interlocked.Increment(ref PackageSendQueueCount);
183183
await sendLock.WaitAsync().ConfigureAwait(false);
184184
try
185185
{
186-
await handler(sendPipe);
186+
var packageBodyLength = 0;
187+
if (packageBodyHandler != null)
188+
packageBodyLength = await packageBodyHandler(sendPipe);
189+
await writePackageBuffer(sendPipe.Reader, packageType, packageBodyLength, ignoreCompressAndEncrypt);
187190
}
188191
catch (Exception ex)
189192
{
@@ -203,18 +206,12 @@ private async Task UseSendPipe(Func<Pipe, Task> handler)
203206
/// </summary>
204207
public async Task SendHeartbeatPackage()
205208
{
206-
await UseSendPipe(async pipe =>
207-
{
208-
var bodyLength = 0;
209-
await writePackageBuffer(pipe.Reader,
210-
QpPackageType.Heartbeat,
211-
bodyLength).ConfigureAwait(false);
212-
});
209+
await UseSendPipe(QpPackageType.Heartbeat);
213210
}
214211

215212
public async Task SendNoticePackage(string noticePackageTypeName, string noticePackageContent)
216213
{
217-
await UseSendPipe(async pipe =>
214+
await UseSendPipe(QpPackageType.Notice, async pipe =>
218215
{
219216
var writer = pipe.Writer;
220217

@@ -247,12 +244,10 @@ await UseSendPipe(async pipe =>
247244
bodyLength += byteCount;
248245
}
249246
}
250-
_ = writer.FlushAsync();
247+
await writer.FlushAsync();
251248
if (LogUtils.LogNotice)
252249
LogUtils.Log("{0}: [Send-NoticePackage]Type:{1},Content:{2}", DateTime.Now, typeName, LogUtils.LogContent ? content : LogUtils.NOT_SHOW_CONTENT_MESSAGE);
253-
await writePackageBuffer(pipe.Reader,
254-
QpPackageType.Notice,
255-
bodyLength).ConfigureAwait(false);
250+
return bodyLength;
256251
});
257252
}
258253

@@ -266,7 +261,7 @@ public Task SendCommandRequestPackage(string commandId, string typeName, string
266261
/// </summary>
267262
private async Task SendCommandRequestPackage(string commandId, string typeName, string content, bool ignoreCompressAndEncrypt)
268263
{
269-
await UseSendPipe(async pipe =>
264+
await UseSendPipe(QpPackageType.CommandRequest, async pipe =>
270265
{
271266
var writer = pipe.Writer;
272267
var bodyLength = 0;
@@ -295,24 +290,21 @@ await UseSendPipe(async pipe =>
295290
writer.Advance(contentLength);
296291
bodyLength += contentLength;
297292
}
298-
_ = writer.FlushAsync();
293+
await writer.FlushAsync();
299294

300295
if (LogUtils.LogCommand)
301296
LogUtils.Log("{0}: [Send-CommandRequestPackage]CommandId:{1},Type:{2},Content:{3}", DateTime.Now, commandId, typeName, LogUtils.LogContent ? content : LogUtils.NOT_SHOW_CONTENT_MESSAGE);
302297

303-
await writePackageBuffer(pipe.Reader,
304-
QpPackageType.CommandRequest,
305-
bodyLength,
306-
ignoreCompressAndEncrypt).ConfigureAwait(false);
307-
});
298+
return bodyLength;
299+
}, ignoreCompressAndEncrypt);
308300
}
309301

310302
/// <summary>
311303
/// 发送命令响应包
312304
/// </summary>
313305
public async Task SendCommandResponsePackage(string commandId, byte code, string message, string typeName, string content)
314306
{
315-
await UseSendPipe(async pipe =>
307+
await UseSendPipe(QpPackageType.CommandResponse, async pipe =>
316308
{
317309
var writer = pipe.Writer;
318310
var bodyLength = 0;
@@ -369,9 +361,7 @@ await UseSendPipe(async pipe =>
369361
LogUtils.Log("{0}: [Send-CommandResponsePackage]CommandId:{1},Code:{2},Message:{3}", DateTime.Now, commandId, code, message);
370362
}
371363
_ = writer.FlushAsync();
372-
await writePackageBuffer(pipe.Reader,
373-
QpPackageType.CommandResponse,
374-
bodyLength).ConfigureAwait(false);
364+
return bodyLength;
375365
});
376366
}
377367
}

0 commit comments

Comments
 (0)