Skip to content

Commit 07d57ac

Browse files
committed
优化连接断开时等待断开时间可能过长的问题
1 parent c121a4a commit 07d57ac

File tree

7 files changed

+21
-19
lines changed

7 files changed

+21
-19
lines changed

Quick.Protocol.SerialPort/QpSerialPortClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class QpSerialPortClient : QpClient
1313
{
1414
private QpSerialPortClientOptions options;
1515
private System.IO.Ports.SerialPort serialPort;
16+
protected override bool ReadFromStreamReturnZeroMeansFault => false;
1617
public QpSerialPortClient(QpSerialPortClientOptions options) : base(options)
1718
{
1819
this.options = options;

Quick.Protocol.SerialPort/QpSerialPortServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected override Task InnerAcceptAsync(CancellationToken token)
6666
return;
6767
if (task.IsFaulted)
6868
return;
69-
OnNewChannelConnected(serialPort.BaseStream, $"SerialPort:{options.PortName}", token);
69+
OnNewChannelConnected(serialPort.BaseStream, $"SerialPort:{options.PortName}", token, false);
7070
});
7171
}
7272
}

Quick.Protocol/QpChannel_Recv.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace Quick.Protocol
1616
{
1717
public abstract partial class QpChannel
1818
{
19+
/// <summary>
20+
/// 从流中读取返回0是否代表错误
21+
/// </summary>
22+
protected virtual bool ReadFromStreamReturnZeroMeansFault { get; } = true;
1923
private DateTime lastReadDataTime;
2024
/// <summary>
2125
/// 当读取出错时
@@ -184,8 +188,12 @@ private async Task FillRecvPipeAsync(Stream stream, PipeWriter writer, Cancellat
184188
while (!token.IsCancellationRequested)
185189
{
186190
int bytesRead = await stream.ReadAsync(readBufferMemory, token);
191+
if (bytesRead < 0)
192+
throw new EndOfStreamException();
187193
if (bytesRead == 0)
188194
{
195+
if (ReadFromStreamReturnZeroMeansFault)
196+
throw new EndOfStreamException();
189197
await Task.Delay(100, token);
190198
continue;
191199
}

Quick.Protocol/QpServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ internal void RemoveChannel(QpServerChannel channel)
6161
}
6262
}
6363

64-
protected void OnNewChannelConnected(Stream stream, string channelName, CancellationToken token)
64+
protected void OnNewChannelConnected(Stream stream, string channelName, CancellationToken token, bool readFromStreamReturnZeroMeansFault = true)
6565
{
66-
var channel = new QpServerChannel(stream, channelName, token, options.Clone());
66+
var channel = new QpServerChannel(stream, channelName, token, options.Clone(), readFromStreamReturnZeroMeansFault);
6767

6868
//认证超时
6969
channel.AuchenticateTimeout += (sender, e) =>

Quick.Protocol/QpServerChannel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ public class QpServerChannel : QpChannel
3131
/// 认证超时
3232
/// </summary>
3333
public event EventHandler AuchenticateTimeout;
34+
protected override bool ReadFromStreamReturnZeroMeansFault { get; }
3435

35-
public QpServerChannel(Stream stream, string channelName, CancellationToken cancellationToken, QpServerOptions options) : base(options)
36+
public QpServerChannel(Stream stream, string channelName, CancellationToken cancellationToken, QpServerOptions options, bool readFromStreamReturnZeroMeansFault = true) : base(options)
3637
{
3738
this.stream = stream;
3839
this.channelName = channelName;
3940
this.options = options;
4041
this.authedCommandExecuterManagerList = options.CommandExecuterManagerList;
4142
this.authedNoticeHandlerManagerList = options.NoticeHandlerManagerList;
4243
serverCancellationToken = cancellationToken;
44+
ReadFromStreamReturnZeroMeansFault = readFromStreamReturnZeroMeansFault;
4345

4446
cts = new CancellationTokenSource();
4547

Quick.Protocol/Streams/QpStreamClient.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Text;
1+
using System.IO;
52
using System.Threading.Tasks;
63

74
namespace Quick.Protocol.Streams
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Text;
5-
using System.Threading;
1+
namespace Quick.Protocol.Streams;
62

7-
namespace Quick.Protocol.Streams
3+
public class QpStreamServerChannel : QpServerChannel
84
{
9-
public class QpStreamServerChannel : QpServerChannel
5+
public QpStreamServerChannel(QpStreamServerOptions options)
6+
: base(options.BaseStream, options.ChannelName, options.CancellationToken, options)
107
{
11-
public QpStreamServerChannel(QpStreamServerOptions options)
12-
: base(options.BaseStream, options.ChannelName, options.CancellationToken, options)
13-
{
14-
}
158
}
169
}
10+

0 commit comments

Comments
 (0)