Skip to content

Commit b7fbb0f

Browse files
authored
Merge pull request #46 from jacqueskang/bugfix-client-cancellation
bugfix: client cancellation request is not taken into account after t…
2 parents 99e246c + 0532a7a commit b7fbb0f

File tree

4 files changed

+76
-47
lines changed

4 files changed

+76
-47
lines changed

src/IpcServiceSample.ConsoleClient/Program.cs

+61-43
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Net;
55
using System.Text;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
namespace IpcServiceSample.ConsoleClient
@@ -18,58 +19,75 @@ private static async Task MainAsync(string[] args)
1819
{
1920
try
2021
{
21-
IpcServiceClient<IComputingService> computingClient = new IpcServiceClientBuilder<IComputingService>()
22-
.UseNamedPipe("pipeName")
23-
.Build();
22+
Console.WriteLine("Press any key to stop.");
23+
var source = new CancellationTokenSource();
2424

25-
IpcServiceClient<ISystemService> systemClient = new IpcServiceClientBuilder<ISystemService>()
26-
.UseTcp(IPAddress.Loopback, 45684)
27-
.Build();
28-
29-
// test 1: call IPC service method with primitive types
30-
float result1 = await computingClient.InvokeAsync(x => x.AddFloat(1.23f, 4.56f));
31-
Console.WriteLine($"[TEST 1] sum of 2 floating number is: {result1}");
32-
33-
// test 2: call IPC service method with complex types
34-
ComplexNumber result2 = await computingClient.InvokeAsync(x => x.AddComplexNumber(
35-
new ComplexNumber(0.1f, 0.3f),
36-
new ComplexNumber(0.2f, 0.6f)));
37-
Console.WriteLine($"[TEST 2] sum of 2 complexe number is: {result2.A}+{result2.B}i");
38-
39-
// test 3: call IPC service method with an array of complex types
40-
ComplexNumber result3 = await computingClient.InvokeAsync(x => x.AddComplexNumbers(new[]
25+
await Task.WhenAll(RunTestsAsync(source.Token), Task.Run(() =>
4126
{
42-
new ComplexNumber(0.5f, 0.4f),
43-
new ComplexNumber(0.2f, 0.1f),
44-
new ComplexNumber(0.3f, 0.5f),
27+
Console.ReadKey();
28+
Console.WriteLine("Cancelling...");
29+
source.Cancel();
4530
}));
46-
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i");
31+
}
32+
catch (Exception ex)
33+
{
34+
Console.WriteLine(ex);
35+
}
36+
}
4737

48-
// test 4: call IPC service method without parameter or return
49-
await systemClient.InvokeAsync(x => x.DoNothing());
50-
Console.WriteLine($"[TEST 4] invoked DoNothing()");
38+
private static async Task RunTestsAsync(CancellationToken cancellationToken)
39+
{
40+
IpcServiceClient<IComputingService> computingClient = new IpcServiceClientBuilder<IComputingService>()
41+
.UseNamedPipe("pipeName")
42+
.Build();
5143

52-
// test 5: call IPC service method with enum parameter
53-
string text = await systemClient.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper));
54-
Console.WriteLine($"[TEST 5] {text}");
44+
IpcServiceClient<ISystemService> systemClient = new IpcServiceClientBuilder<ISystemService>()
45+
.UseTcp(IPAddress.Loopback, 45684)
46+
.Build();
5547

56-
// test 6: call IPC service method returning GUID
57-
Guid generatedId = await systemClient.InvokeAsync(x => x.GenerateId());
58-
Console.WriteLine($"[TEST 6] generated ID is: {generatedId}");
48+
// test 1: call IPC service method with primitive types
49+
float result1 = await computingClient.InvokeAsync(x => x.AddFloat(1.23f, 4.56f), cancellationToken);
50+
Console.WriteLine($"[TEST 1] sum of 2 floating number is: {result1}");
5951

60-
// test 7: call IPC service method with byte array
61-
byte[] input = Encoding.UTF8.GetBytes("Test");
62-
byte[] reversed = await systemClient.InvokeAsync(x => x.ReverseBytes(input));
63-
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");
52+
// test 2: call IPC service method with complex types
53+
ComplexNumber result2 = await computingClient.InvokeAsync(x => x.AddComplexNumber(
54+
new ComplexNumber(0.1f, 0.3f),
55+
new ComplexNumber(0.2f, 0.6f)), cancellationToken);
56+
Console.WriteLine($"[TEST 2] sum of 2 complexe number is: {result2.A}+{result2.B}i");
6457

65-
// test 8: call IPC service method with generic parameter
66-
string print = await systemClient.InvokeAsync(x => x.Printout(DateTime.UtcNow));
67-
Console.WriteLine($"[TEST 8] print out value: {print}");
68-
}
69-
catch (Exception ex)
58+
// test 3: call IPC service method with an array of complex types
59+
ComplexNumber result3 = await computingClient.InvokeAsync(x => x.AddComplexNumbers(new[]
7060
{
71-
Console.WriteLine(ex);
72-
}
61+
new ComplexNumber(0.5f, 0.4f),
62+
new ComplexNumber(0.2f, 0.1f),
63+
new ComplexNumber(0.3f, 0.5f),
64+
}), cancellationToken);
65+
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i", cancellationToken);
66+
67+
// test 4: call IPC service method without parameter or return
68+
await systemClient.InvokeAsync(x => x.DoNothing(), cancellationToken);
69+
Console.WriteLine($"[TEST 4] invoked DoNothing()");
70+
71+
// test 5: call IPC service method with enum parameter
72+
string text = await systemClient.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper), cancellationToken);
73+
Console.WriteLine($"[TEST 5] {text}");
74+
75+
// test 6: call IPC service method returning GUID
76+
Guid generatedId = await systemClient.InvokeAsync(x => x.GenerateId(), cancellationToken);
77+
Console.WriteLine($"[TEST 6] generated ID is: {generatedId}");
78+
79+
// test 7: call IPC service method with byte array
80+
byte[] input = Encoding.UTF8.GetBytes("Test");
81+
byte[] reversed = await systemClient.InvokeAsync(x => x.ReverseBytes(input), cancellationToken);
82+
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");
83+
84+
// test 8: call IPC service method with generic parameter
85+
string print = await systemClient.InvokeAsync(x => x.Printout(DateTime.UtcNow), cancellationToken);
86+
Console.WriteLine($"[TEST 8] print out value: {print}");
87+
88+
// test 9: call slow IPC service method
89+
await systemClient.InvokeAsync(x => x.SlowOperation(), cancellationToken);
90+
Console.WriteLine($"[TEST 9] Called slow operation");
7391
}
7492
}
7593
}

src/IpcServiceSample.ConsoleServer/SystemService.cs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Globalization;
44
using System.Linq;
5+
using System.Threading;
56

67
namespace IpcServiceSample.ConsoleServer
78
{
@@ -37,5 +38,10 @@ public byte[] ReverseBytes(byte[] input)
3738
{
3839
return input.Reverse().ToArray();
3940
}
41+
42+
public void SlowOperation()
43+
{
44+
Thread.Sleep(10000);
45+
}
4046
}
4147
}

src/IpcServiceSample.ServiceContracts/ISystemService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface ISystemService
99
Guid GenerateId();
1010
byte[] ReverseBytes(byte[] input);
1111
string Printout<T>(T value);
12+
void SlowOperation();
1213
}
1314
}

src/JKang.IpcServiceFramework.Client/Tcp/TcpIpcServiceClient.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ protected override async Task<Stream> ConnectToServerAsync(CancellationToken can
3030

3131
await Task.Run(() =>
3232
{
33-
// poll every 1 second to check cancellation request
34-
while (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(1000), false))
33+
// poll every 100ms to check cancellation request
34+
while (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(100), false))
3535
{
3636
if (cancellationToken.IsCancellationRequested)
3737
{
38-
client.Close();
38+
client.EndConnect(result);
3939
cancellationToken.ThrowIfCancellationRequested();
4040
}
4141
}
42-
client.EndConnect(result);
42+
});
43+
44+
cancellationToken.Register(() =>
45+
{
46+
client.Close();
4347
});
4448

4549
return client.GetStream();

0 commit comments

Comments
 (0)