Skip to content

Commit 2b498ea

Browse files
author
andrei.hilevich
committed
Slightly reworked forced disconnection case
1 parent 885c2f4 commit 2b498ea

File tree

6 files changed

+69
-34
lines changed

6 files changed

+69
-34
lines changed

src/SharpRpc.Builder/SharpRpc.Builder.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>8.0</LangVersion>
66
<Configurations>Debug;Release;DebugBuilder;ReleasePfCounters;ReleaseIde</Configurations>
7-
<Version>1.9.8</Version>
7+
<Version>1.9.9</Version>
88
<Authors>Andrei Hilevich</Authors>
99
<Company>Soft-Fx</Company>
1010
<Product>SharpRpc</Product>
@@ -16,7 +16,7 @@
1616
<PackageReleaseNotes></PackageReleaseNotes>
1717
<PackageTags>#RPC RPC</PackageTags>
1818
<IncludeBuildOutput>false</IncludeBuildOutput>
19-
<AssemblyVersion>1.9.8.0</AssemblyVersion>
19+
<AssemblyVersion>1.9.9.0</AssemblyVersion>
2020
</PropertyGroup>
2121

2222
<PropertyGroup Condition="'$(Configuration)'=='DebugBuilder'">

src/SharpRpc/Channel.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private void TriggerClose(RpcResult reason, bool isConnectionLost, out Task clos
221221
}
222222

223223
if (forceDisconnect)
224-
DropTransport();
224+
AbortConnection();
225225

226226
if (invokeDisconnect)
227227
DoDisconnect();
@@ -349,7 +349,7 @@ private async void ConnectRoutine(CancellationToken cancelToken)
349349
private void OnLogoutTimeout()
350350
{
351351
Logger.Warn(Id, "Logout operation timed out!");
352-
DropTransport();
352+
AbortConnection();
353353
}
354354

355355
//private Task TriggerCloseComponents(bool isAbortion)
@@ -406,7 +406,7 @@ private async Task CloseComponents()
406406
private void OnCloseComponentsTimeout()
407407
{
408408
Logger.Warn(Id, "Close operation timed out!");
409-
DropTransport();
409+
AbortConnection();
410410
}
411411

412412
private void DisposeTransport()
@@ -445,18 +445,28 @@ private async void DoDisconnect()
445445
_disconnectEvent.SetResult(RpcResult.Ok);
446446
}
447447

448-
private void DropTransport()
448+
// In case of connection loss, timeout, fatal error, or something ungraceful
449+
private void AbortConnection()
449450
{
451+
bool callDispose = false;
452+
450453
lock (_stateSyncObj)
451454
{
452455
_coordinator.AbortCoordination();
453-
if (_isTransportDisposed)
454-
return;
455-
_isTransportDisposed = true;
456+
if (!_isTransportDisposed)
457+
{
458+
_isTransportDisposed = true;
459+
callDispose = true;
460+
}
456461
}
462+
463+
Dispatcher.Abort(_channelFault);
457464

458-
Logger.Verbose(Id, "Disposing the transport...");
459-
_transport?.Dispose();
465+
if (callDispose)
466+
{
467+
Logger.Verbose(Id, "Disposing the transport...");
468+
_transport?.Dispose();
469+
}
460470
}
461471

462472
private void SetClosedState()

src/SharpRpc/Disptaching/MessageDispatcher.NoThreading.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
using SharpRpc.Disptaching;
99
using SharpRpc.Lib;
10-
using System;
1110
using System.Collections.Generic;
12-
using System.Diagnostics;
13-
using System.Linq;
14-
using System.Text;
1511
using System.Threading;
1612
using System.Threading.Tasks;
1713

@@ -24,7 +20,8 @@ private class NoThreading : MessageDispatcher
2420
private readonly object _lockObj = new object();
2521
private bool _isStarted;
2622
private bool _isProcessing;
27-
private bool _closed;
23+
private bool _isClosed;
24+
private bool _isEnabled = true;
2825
private TaskCompletionSource<bool> _startedEvent = new TaskCompletionSource<bool>();
2926
private TaskCompletionSource<bool> _closeCompletion = new TaskCompletionSource<bool>();
3027

@@ -50,30 +47,55 @@ public override RpcResult Start()
5047

5148
public override Task Stop(RpcResult fault)
5249
{
50+
List<IDispatcherOperation> operationsToCancel = null;
51+
5352
lock (_lockObj)
5453
{
55-
if (_closed)
56-
return _closeCompletion.Task;
57-
58-
_closed = true;
59-
6054
if (!_isStarted)
6155
{
6256
_isStarted = true;
6357
_startedEvent.SetResult(true);
6458
}
6559

66-
Core.OnStop(fault);
60+
if (_isEnabled)
61+
{
62+
_isEnabled = false;
63+
operationsToCancel = Core.OnStop(fault);
64+
}
65+
66+
if (_isClosed)
67+
return _closeCompletion.Task;
68+
69+
_isClosed = true;
6770

6871
if (!_isProcessing)
6972
CompleteClose();
7073
}
7174

72-
Core.CompleteStop();
75+
if (operationsToCancel != null)
76+
Core.CancelOperations(operationsToCancel);
7377

7478
return _closeCompletion.Task;
7579
}
7680

81+
public override void Abort(RpcResult fault)
82+
{
83+
List<IDispatcherOperation> operationsToCancel = null;
84+
85+
lock (_lockObj)
86+
{
87+
if (_isEnabled)
88+
{
89+
_isEnabled = false;
90+
operationsToCancel = Core.OnStop(fault);
91+
}
92+
}
93+
94+
if (operationsToCancel != null)
95+
Core.CancelOperations(operationsToCancel);
96+
}
97+
98+
7799
#if NET5_0_OR_GREATER
78100
public override async ValueTask OnMessages()
79101
#else
@@ -84,7 +106,7 @@ public override async Task OnMessages()
84106

85107
lock (_lockObj)
86108
{
87-
if (_closed)
109+
if (!_isEnabled)
88110
return;
89111

90112
if (!_isStarted)
@@ -100,10 +122,11 @@ public override async Task OnMessages()
100122

101123
lock (_lockObj)
102124
{
103-
if (_closed)
125+
if (!_isEnabled)
104126
{
105127
_isProcessing = false;
106-
CompleteClose();
128+
if (_isClosed)
129+
CompleteClose();
107130
return;
108131
}
109132
}
@@ -116,7 +139,7 @@ public override async Task OnMessages()
116139
{
117140
_isProcessing = false;
118141

119-
if (_closed)
142+
if (_isClosed)
120143
CompleteClose();
121144
}
122145
}

src/SharpRpc/Disptaching/MessageDispatcher.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ protected void OnError(RpcRetCode code, string message)
7070

7171
public void TriggerStop(RpcResult fault) => Stop(fault);
7272

73-
public abstract Task Stop(RpcResult fault);
73+
public abstract Task Stop(RpcResult fault); // graceful close
7474
public abstract RpcResult Register(IDispatcherOperation operation);
7575
public abstract void Unregister(IDispatcherOperation operation);
76+
public abstract void Abort(RpcResult fault); // In case of connection loss, timeout, or something ungraceful
77+
7678
protected abstract void CancelOutgoingCall(IDispatcherOperation callObj);
7779
protected abstract void DoCall(IRequestMessage requestMsg, IDispatcherOperation callObj, CancellationToken cToken);
7880

src/SharpRpc/Disptaching/MessageDispatcherCore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal class MessageDispatcherCore
2020
private readonly Dictionary<string, IDispatcherOperation> _operations = new Dictionary<string, IDispatcherOperation>();
2121
private bool _completed;
2222
private RpcResult _fault;
23-
private List<IDispatcherOperation> _tasksToCancel;
2423

2524
public MessageDispatcherCore(TxPipeline txNode, RpcCallHandler msgHandler, Action<RpcRetCode, string> onErrorAction)
2625
{
@@ -67,21 +66,22 @@ public void ProcessMessage(IMessage message)
6766
ProcessOneWayMessage(message);
6867
}
6968

70-
public void OnStop(RpcResult fault)
69+
public List<IDispatcherOperation> OnStop(RpcResult fault)
7170
{
7271
lock (_operations)
7372
{
7473
_completed = true;
7574
_fault = fault;
7675

77-
_tasksToCancel = _operations.Values.ToList();
76+
var tasksToCancel = _operations.Values.ToList();
7877
_operations.Clear();
78+
return tasksToCancel;
7979
}
8080
}
8181

82-
public void CompleteStop()
82+
public void CancelOperations(IEnumerable<IDispatcherOperation> operations)
8383
{
84-
foreach (var task in _tasksToCancel)
84+
foreach (var task in operations)
8585
task.Terminate(_fault);
8686
}
8787

src/SharpRpc/SharpRpc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>net6.0;net472;netstandard2.0</TargetFrameworks>
55
<Configurations>Debug;Release;ReleasePfCounters;ReleaseIde</Configurations>
6-
<Version>1.9.8</Version>
6+
<Version>1.9.9</Version>
77
<Company>Soft-Fx</Company>
88
<Authors>Andrei Hilevich</Authors>
99
<PackageId>SharpRpc.Core</PackageId>

0 commit comments

Comments
 (0)