Skip to content

Commit

Permalink
Merge pull request #46 from jacqueskang/bugfix-client-cancellation
Browse files Browse the repository at this point in the history
bugfix: client cancellation request is not taken into account after t…
  • Loading branch information
jacqueskang authored Nov 20, 2018
2 parents 99e246c + 0532a7a commit b7fbb0f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 47 deletions.
104 changes: 61 additions & 43 deletions src/IpcServiceSample.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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

IpcServiceClient<ISystemService> systemClient = new IpcServiceClientBuilder<ISystemService>()
.UseTcp(IPAddress.Loopback, 45684)
.Build();

// test 1: call IPC service method with primitive types
float result1 = await computingClient.InvokeAsync(x => x.AddFloat(1.23f, 4.56f));
Console.WriteLine($"[TEST 1] sum of 2 floating number is: {result1}");

// test 2: call IPC service method with complex types
ComplexNumber result2 = await computingClient.InvokeAsync(x => x.AddComplexNumber(
new ComplexNumber(0.1f, 0.3f),
new ComplexNumber(0.2f, 0.6f)));
Console.WriteLine($"[TEST 2] sum of 2 complexe number is: {result2.A}+{result2.B}i");

// test 3: call IPC service method with an array of complex types
ComplexNumber result3 = await computingClient.InvokeAsync(x => x.AddComplexNumbers(new[]
await Task.WhenAll(RunTestsAsync(source.Token), Task.Run(() =>
{
new ComplexNumber(0.5f, 0.4f),
new ComplexNumber(0.2f, 0.1f),
new ComplexNumber(0.3f, 0.5f),
Console.ReadKey();
Console.WriteLine("Cancelling...");
source.Cancel();
}));
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

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

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

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

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

// test 8: call IPC service method with generic parameter
string print = await systemClient.InvokeAsync(x => x.Printout(DateTime.UtcNow));
Console.WriteLine($"[TEST 8] print out value: {print}");
}
catch (Exception ex)
// test 3: call IPC service method with an array of complex types
ComplexNumber result3 = await computingClient.InvokeAsync(x => x.AddComplexNumbers(new[]
{
Console.WriteLine(ex);
}
new ComplexNumber(0.5f, 0.4f),
new ComplexNumber(0.2f, 0.1f),
new ComplexNumber(0.3f, 0.5f),
}), cancellationToken);
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i", cancellationToken);

// test 4: call IPC service method without parameter or return
await systemClient.InvokeAsync(x => x.DoNothing(), cancellationToken);
Console.WriteLine($"[TEST 4] invoked DoNothing()");

// test 5: call IPC service method with enum parameter
string text = await systemClient.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper), cancellationToken);
Console.WriteLine($"[TEST 5] {text}");

// test 6: call IPC service method returning GUID
Guid generatedId = await systemClient.InvokeAsync(x => x.GenerateId(), cancellationToken);
Console.WriteLine($"[TEST 6] generated ID is: {generatedId}");

// test 7: call IPC service method with byte array
byte[] input = Encoding.UTF8.GetBytes("Test");
byte[] reversed = await systemClient.InvokeAsync(x => x.ReverseBytes(input), cancellationToken);
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");

// test 8: call IPC service method with generic parameter
string print = await systemClient.InvokeAsync(x => x.Printout(DateTime.UtcNow), cancellationToken);
Console.WriteLine($"[TEST 8] print out value: {print}");

// test 9: call slow IPC service method
await systemClient.InvokeAsync(x => x.SlowOperation(), cancellationToken);
Console.WriteLine($"[TEST 9] Called slow operation");
}
}
}
6 changes: 6 additions & 0 deletions src/IpcServiceSample.ConsoleServer/SystemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Threading;

namespace IpcServiceSample.ConsoleServer
{
Expand Down Expand Up @@ -37,5 +38,10 @@ public byte[] ReverseBytes(byte[] input)
{
return input.Reverse().ToArray();
}

public void SlowOperation()
{
Thread.Sleep(10000);
}
}
}
1 change: 1 addition & 0 deletions src/IpcServiceSample.ServiceContracts/ISystemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface ISystemService
Guid GenerateId();
byte[] ReverseBytes(byte[] input);
string Printout<T>(T value);
void SlowOperation();
}
}
12 changes: 8 additions & 4 deletions src/JKang.IpcServiceFramework.Client/Tcp/TcpIpcServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ protected override async Task<Stream> ConnectToServerAsync(CancellationToken can

await Task.Run(() =>
{
// poll every 1 second to check cancellation request
while (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(1000), false))
// poll every 100ms to check cancellation request
while (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(100), false))
{
if (cancellationToken.IsCancellationRequested)
{
client.Close();
client.EndConnect(result);
cancellationToken.ThrowIfCancellationRequested();
}
}
client.EndConnect(result);
});

cancellationToken.Register(() =>
{
client.Close();
});

return client.GetStream();
Expand Down

0 comments on commit b7fbb0f

Please sign in to comment.