Skip to content

Commit 53827fb

Browse files
committed
Enhance performance.
1 parent 55162ab commit 53827fb

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

Quick.Protocol/QpChannel.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public abstract class QpChannel
4949
private ICryptoTransform enc;
5050
private ICryptoTransform dec;
5151
private Encoding encoding = Encoding.UTF8;
52-
52+
//发送包锁对象
53+
private object SEND_PACKAGE_LOCK_OBJ = new object();
54+
//断开连接锁对象
55+
private object DISCONNECT_LOCK_OBJ = new object();
5356
private Dictionary<string, Type> commandRequestTypeDict = new Dictionary<string, Type>();
5457
private Dictionary<string, Type> commandResponseTypeDict = new Dictionary<string, Type>();
5558
private Dictionary<Type, Type> commandRequestTypeResponseTypeDict = new Dictionary<Type, Type>();
@@ -118,15 +121,15 @@ protected set
118121
/// </summary>
119122
public virtual void Disconnect()
120123
{
121-
lock (this)
124+
lock (DISCONNECT_LOCK_OBJ)
122125
{
123126
if (IsConnected)
124127
{
125-
IsConnected = false;
126-
InitQpPackageHandler_Stream(null);
128+
IsConnected = false;
127129
Disconnected?.Invoke(this, QpEventArgs.Empty);
128130
}
129131
}
132+
InitQpPackageHandler_Stream(null);
130133
}
131134

132135
/// <summary>
@@ -232,16 +235,28 @@ public QpChannel(QpChannelOptions options)
232235

233236
protected void InitQpPackageHandler_Stream(Stream stream)
234237
{
235-
if (QpPackageHandler_Stream != null)
236-
try { QpPackageHandler_Stream?.Dispose(); }
237-
catch { }
238+
var preStream = QpPackageHandler_Stream;
238239
QpPackageHandler_Stream = stream;
239240

241+
try { preStream?.Dispose(); }
242+
catch { }
243+
240244
options.InternalCompress = false;
241245
options.InternalEncrypt = false;
242246
ChangeTransportTimeout();
243247
}
244248

249+
/// <summary>
250+
/// 当发送出错时
251+
/// </summary>
252+
protected virtual void OnWriteError(Exception exception)
253+
{
254+
LastException = exception;
255+
LogUtils.Log("[WriteError]{0}: {1}", DateTime.Now, ExceptionUtils.GetExceptionString(exception));
256+
InitQpPackageHandler_Stream(null);
257+
Disconnect();
258+
}
259+
245260
/// <summary>
246261
/// 当读取出错时
247262
/// </summary>
@@ -337,7 +352,7 @@ private void sendPackage(Func<byte[], ArraySegment<byte>> getPackagePayloadFunc,
337352
if (stream == null)
338353
return;
339354

340-
lock (this)
355+
lock (SEND_PACKAGE_LOCK_OBJ)
341356
{
342357
var ret = getPackagePayloadFunc(sendBuffer);
343358
var packageTotalLength = ret.Count;
@@ -392,8 +407,7 @@ private void sendPackage(Func<byte[], ArraySegment<byte>> getPackagePayloadFunc,
392407
}
393408
catch (Exception ex)
394409
{
395-
LastException = ex;
396-
LogUtils.Log("[SendPackage]" + ExceptionUtils.GetExceptionString(ex));
410+
OnWriteError(ex);
397411
throw;
398412
}
399413
}
@@ -940,7 +954,7 @@ protected void BeginReadPackage(CancellationToken token)
940954
case QpPackageType.Heartbeat:
941955
{
942956
if (LogUtils.LogHeartbeat)
943-
LogUtils.Log("{0}: [Recv-HeartbetaPackage]", DateTime.Now);
957+
LogUtils.Log("{0}: [Recv-HeartbeatPackage]", DateTime.Now);
944958
HeartbeatPackageReceived?.Invoke(this, QpEventArgs.Empty);
945959
break;
946960
}

Quick.Protocol/QpClient.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public async Task ConnectAsync()
7474
BeginHeartBeat(token);
7575
}
7676
}
77+
protected override void OnWriteError(Exception exception)
78+
{
79+
base.OnWriteError(exception);
80+
Options.Init();
81+
cancellAll();
82+
Disconnect();
83+
}
7784

7885
protected override void OnReadError(Exception exception)
7986
{
@@ -85,11 +92,8 @@ protected override void OnReadError(Exception exception)
8592

8693
private void cancellAll()
8794
{
88-
if (cts != null)
89-
{
90-
cts.Cancel();
91-
cts = null;
92-
}
95+
cts?.Cancel();
96+
cts = null;
9397
}
9498

9599
/// <summary>

Quick.Protocol/QpServer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@ public virtual void Stop()
114114
{
115115
cts?.Cancel();
116116
cts = null;
117+
118+
QpServerChannel[] channels;
117119
lock (channelList)
118120
{
121+
channels = channelList.ToArray();
119122
channelList.Clear();
120-
Channels = channelList.ToArray();
123+
Channels = new QpServerChannel[0];
121124
}
125+
foreach (var channel in channels)
126+
try { channel.Disconnect(); }
127+
catch { }
122128
}
123129
}
124130
}

Quick.Protocol/QpServerChannel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public void Stop()
158158
}
159159
catch { }
160160
}
161+
protected override void OnWriteError(Exception exception)
162+
{
163+
Stop();
164+
base.OnWriteError(exception);
165+
}
161166

162167
protected override void OnReadError(Exception exception)
163168
{

0 commit comments

Comments
 (0)