Skip to content

Commit 19a20e7

Browse files
authored
Merge pull request #22 from jacqueskang/develop
v1.0.4
2 parents 39d4dd7 + c7628fd commit 19a20e7

File tree

15 files changed

+185
-70
lines changed

15 files changed

+185
-70
lines changed

src/Directory.Build.props

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<Authors>Jacques Kang and other GitHub contributors</Authors>
5+
<Company />
6+
<PackageLicenseUrl>https://github.com/jacqueskang/IpcServiceFramework/blob/develop/LICENSE</PackageLicenseUrl>
7+
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
8+
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
9+
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
10+
<!-- this version will be overwritten during build -->
11+
<Version>0.0.0</Version>
12+
</PropertyGroup>
13+
14+
</Project>

src/IpcServiceFramework.sln

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{20913218-C740-42E9-9D17-CAD973B676D0}"
1515
ProjectSection(SolutionItems) = preProject
1616
..\.travis.yml = ..\.travis.yml
17+
Directory.Build.props = Directory.Build.props
1718
..\README.md = ..\README.md
1819
EndProjectSection
1920
EndProject
@@ -23,9 +24,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.C
2324
EndProject
2425
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.Server", "JKang.IpcServiceFramework.Server\JKang.IpcServiceFramework.Server.csproj", "{069B416A-B2C6-40D1-80B8-AC9ACA2217E3}"
2526
EndProject
26-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JKang.IpcServiceFramework.Core.Tests", "JKang.IpcServiceFramework.Core.Tests\JKang.IpcServiceFramework.Core.Tests.csproj", "{1EC81913-883B-487C-A3FD-98A80EDE3225}"
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.Core.Tests", "JKang.IpcServiceFramework.Core.Tests\JKang.IpcServiceFramework.Core.Tests.csproj", "{1EC81913-883B-487C-A3FD-98A80EDE3225}"
2728
EndProject
28-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpcServiceSample.WebServer", "IpcServiceSample.WebServer\IpcServiceSample.WebServer.csproj", "{D57727B9-81F1-439A-AD17-0DB26C8F0523}"
29+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IpcServiceSample.WebServer", "IpcServiceSample.WebServer\IpcServiceSample.WebServer.csproj", "{D57727B9-81F1-439A-AD17-0DB26C8F0523}"
2930
EndProject
3031
Global
3132
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using IpcServiceSample.ServiceContracts;
22
using JKang.IpcServiceFramework;
33
using System;
4+
using System.Text;
45
using System.Threading.Tasks;
56

67
namespace IpcServiceSample.ConsoleClient
@@ -20,13 +21,13 @@ private static async Task MainAsync(string[] args)
2021

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

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

3132
// test 3: call IPC service method with an array of complex types
3233
ComplexNumber result3 = await client.InvokeAsync(x => x.AddComplexNumbers(new[]
@@ -35,11 +36,25 @@ private static async Task MainAsync(string[] args)
3536
new ComplexNumber(0.2f, 0.1f),
3637
new ComplexNumber(0.3f, 0.5f),
3738
}));
38-
Console.WriteLine($"sum of 3 complexe number is: {result3.A}+{result3.B}i");
39+
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i");
3940

4041
// test 4: call IPC service method without parameter or return
4142
await client.InvokeAsync(x => x.DoNothing());
42-
Console.WriteLine($"invoked DoNothing()");
43+
Console.WriteLine($"[TEST 4] invoked DoNothing()");
44+
45+
// test 5: call IPC service method with enum parameter
46+
string text = await client.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper));
47+
Console.WriteLine($"[TEST 5] {text}");
48+
49+
// test 6: call IPC service method returning GUID
50+
Guid generatedId = await client.InvokeAsync(x => x.GenerateId());
51+
Console.WriteLine($"[TEST 6] generated ID is: {generatedId}");
52+
53+
// test 7: call IPC service method with byte array
54+
byte[] input = Encoding.UTF8.GetBytes("Test");
55+
byte[] reversed = await client.InvokeAsync(x => x.ReverseBytes(input));
56+
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");
57+
4358
}
4459
catch (Exception ex)
4560
{

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
25
using IpcServiceSample.ServiceContracts;
36
using Microsoft.Extensions.Logging;
47

@@ -36,7 +39,30 @@ public float AddFloat(float x, float y)
3639
return x + y;
3740
}
3841

42+
public string ConvertText(string text, TextStyle style)
43+
{
44+
switch (style)
45+
{
46+
case TextStyle.TitleCase:
47+
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(text);
48+
case TextStyle.Upper:
49+
return CultureInfo.InvariantCulture.TextInfo.ToUpper(text);
50+
default:
51+
return text;
52+
}
53+
}
54+
3955
public void DoNothing()
4056
{ }
57+
58+
public Guid GenerateId()
59+
{
60+
return Guid.NewGuid();
61+
}
62+
63+
public byte[] ReverseBytes(byte[] input)
64+
{
65+
return input.Reverse().ToArray();
66+
}
4167
}
4268
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace IpcServiceSample.ServiceContracts
45
{
@@ -8,6 +9,9 @@ public interface IComputingService
89
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
910
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
1011
void DoNothing();
12+
string ConvertText(string text, TextStyle style);
13+
Guid GenerateId();
14+
byte[] ReverseBytes(byte[] input);
1115
}
1216

1317
public class ComplexNumber
@@ -21,4 +25,10 @@ public ComplexNumber(float a, float b)
2125
B = b;
2226
}
2327
}
28+
29+
public enum TextStyle
30+
{
31+
TitleCase,
32+
Upper
33+
}
2434
}

src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace JKang.IpcServiceFramework
1111
public class IpcServiceClient<TInterface>
1212
where TInterface : class
1313
{
14+
private static readonly ProxyGenerator _proxyGenerator = new ProxyGenerator();
1415
private readonly string _pipeName;
1516
private readonly IIpcMessageSerializer _serializer;
1617
private readonly IValueConverter _converter;
@@ -77,8 +78,7 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
7778
throw new ArgumentException("Only support calling method, ex: x => x.GetData(a, b)");
7879
}
7980

80-
var proxyGenerator = new ProxyGenerator();
81-
TInterface proxy = proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor);
81+
TInterface proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor);
8282
Delegate @delegate = lamdaExp.Compile();
8383
@delegate.DynamicInvoke(proxy);
8484

@@ -93,8 +93,8 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
9393
private async Task<IpcResponse> GetResponseAsync(IpcRequest request)
9494
{
9595
using (var client = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut, PipeOptions.None))
96-
using (var writer = new IpcWriter(client, _serializer))
97-
using (var reader = new IpcReader(client, _serializer))
96+
using (var writer = new IpcWriter(client, _serializer, leaveOpen: true))
97+
using (var reader = new IpcReader(client, _serializer, leaveOpen: true))
9898
{
9999
await client.ConnectAsync();
100100

src/JKang.IpcServiceFramework.Client/JKang.IpcServiceFramework.Client.csproj

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>JKang.IpcServiceFramework</RootNamespace>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
7-
<Authors>Jacques Kang</Authors>
8-
<Company />
9-
<Copyright>Jacques Kang</Copyright>
10-
<PackageLicenseUrl>https://github.com/jacqueskang/IpcServiceFramework/blob/develop/LICENSE</PackageLicenseUrl>
11-
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
12-
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
13-
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
14-
<PackageReleaseNotes>1.0.2
15-
- support implicitly converting input parameters from derived type to base type
16-
1.0.1
17-
- support passing array parameters</PackageReleaseNotes>
18-
<Version>1.0.2</Version>
197
</PropertyGroup>
208

219
<ItemGroup>

src/JKang.IpcServiceFramework.Core.Tests/DefaultValueConverterTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,42 @@ public void TryConvert_DerivedTypeToBaseType()
140140
Assert.IsInstanceOfType(actual, typeof(ComplexType));
141141
}
142142

143+
[TestMethod]
144+
public void TryConvert_StringToEnum()
145+
{
146+
EnumType expected = EnumType.SecondOption;
147+
148+
bool succeed = _sut.TryConvert(expected.ToString(), typeof(EnumType), out object actual);
149+
150+
Assert.IsTrue(succeed);
151+
Assert.IsInstanceOfType(actual, typeof(EnumType));
152+
Assert.AreEqual(expected, actual);
153+
}
154+
155+
[TestMethod]
156+
public void TryConvert_Int32ToEnum()
157+
{
158+
EnumType expected = EnumType.SecondOption;
159+
160+
bool succeed = _sut.TryConvert((int)expected, typeof(EnumType), out object actual);
161+
162+
Assert.IsTrue(succeed);
163+
Assert.IsInstanceOfType(actual, typeof(EnumType));
164+
Assert.AreEqual(expected, actual);
165+
}
166+
167+
[TestMethod]
168+
public void TryConvert_StringToGuid()
169+
{
170+
var expected = Guid.NewGuid();
171+
172+
bool succeed = _sut.TryConvert(expected.ToString(), typeof(Guid), out object actual);
173+
174+
Assert.IsTrue(succeed);
175+
Assert.IsInstanceOfType(actual, typeof(Guid));
176+
Assert.AreEqual(expected, actual);
177+
}
178+
143179
interface IComplexType
144180
{
145181
int Int32Value { get; }
@@ -151,5 +187,11 @@ class ComplexType : IComplexType
151187
public int Int32Value { get; set; }
152188
public string StringValue { get; set; }
153189
}
190+
191+
enum EnumType
192+
{
193+
FirstOption,
194+
SecondOption
195+
}
154196
}
155197
}

src/JKang.IpcServiceFramework.Core/IO/IpcReader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34

45
namespace JKang.IpcServiceFramework.IO
56
{
@@ -9,8 +10,12 @@ public class IpcReader : IDisposable
910
private readonly IIpcMessageSerializer _serializer;
1011

1112
public IpcReader(Stream stream, IIpcMessageSerializer serializer)
13+
: this(stream, serializer, leaveOpen: false)
14+
{ }
15+
16+
public IpcReader(Stream stream, IIpcMessageSerializer serializer, bool leaveOpen)
1217
{
13-
_reader = new BinaryReader(stream);
18+
_reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen);
1419
_serializer = serializer;
1520
}
1621

src/JKang.IpcServiceFramework.Core/IO/IpcWriter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34

45
namespace JKang.IpcServiceFramework.IO
56
{
6-
public class IpcWriter: IDisposable
7+
public class IpcWriter : IDisposable
78
{
89
private readonly BinaryWriter _writer;
910
private readonly IIpcMessageSerializer _serializer;
1011

1112
public IpcWriter(Stream stream, IIpcMessageSerializer serializer)
13+
: this(stream, serializer, leaveOpen: false)
14+
{ }
15+
16+
public IpcWriter(Stream stream, IIpcMessageSerializer serializer, bool leaveOpen)
1217
{
13-
_writer = new BinaryWriter(stream);
18+
_writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen);
1419
_serializer = serializer;
1520
}
1621

0 commit comments

Comments
 (0)