Skip to content

Commit

Permalink
Merge pull request #22 from jacqueskang/develop
Browse files Browse the repository at this point in the history
v1.0.4
  • Loading branch information
jacqueskang authored Jul 13, 2018
2 parents 39d4dd7 + c7628fd commit 19a20e7
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 70 deletions.
14 changes: 14 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project>

<PropertyGroup>
<Authors>Jacques Kang and other GitHub contributors</Authors>
<Company />
<PackageLicenseUrl>https://github.com/jacqueskang/IpcServiceFramework/blob/develop/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<!-- this version will be overwritten during build -->
<Version>0.0.0</Version>
</PropertyGroup>

</Project>
5 changes: 3 additions & 2 deletions src/IpcServiceFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{20913218-C740-42E9-9D17-CAD973B676D0}"
ProjectSection(SolutionItems) = preProject
..\.travis.yml = ..\.travis.yml
Directory.Build.props = Directory.Build.props
..\README.md = ..\README.md
EndProjectSection
EndProject
Expand All @@ -23,9 +24,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.Server", "JKang.IpcServiceFramework.Server\JKang.IpcServiceFramework.Server.csproj", "{069B416A-B2C6-40D1-80B8-AC9ACA2217E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JKang.IpcServiceFramework.Core.Tests", "JKang.IpcServiceFramework.Core.Tests\JKang.IpcServiceFramework.Core.Tests.csproj", "{1EC81913-883B-487C-A3FD-98A80EDE3225}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.IpcServiceFramework.Core.Tests", "JKang.IpcServiceFramework.Core.Tests\JKang.IpcServiceFramework.Core.Tests.csproj", "{1EC81913-883B-487C-A3FD-98A80EDE3225}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpcServiceSample.WebServer", "IpcServiceSample.WebServer\IpcServiceSample.WebServer.csproj", "{D57727B9-81F1-439A-AD17-0DB26C8F0523}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IpcServiceSample.WebServer", "IpcServiceSample.WebServer\IpcServiceSample.WebServer.csproj", "{D57727B9-81F1-439A-AD17-0DB26C8F0523}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
23 changes: 19 additions & 4 deletions src/IpcServiceSample.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IpcServiceSample.ServiceContracts;
using JKang.IpcServiceFramework;
using System;
using System.Text;
using System.Threading.Tasks;

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

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

// test 2: call IPC service method with complex types
ComplexNumber result2 = await client.InvokeAsync(x => x.AddComplexNumber(
new ComplexNumber(0.1f, 0.3f),
new ComplexNumber(0.2f, 0.6f)));
Console.WriteLine($"sum of 2 complexe number is: {result2.A}+{result2.B}i");
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 client.InvokeAsync(x => x.AddComplexNumbers(new[]
Expand All @@ -35,11 +36,25 @@ private static async Task MainAsync(string[] args)
new ComplexNumber(0.2f, 0.1f),
new ComplexNumber(0.3f, 0.5f),
}));
Console.WriteLine($"sum of 3 complexe number is: {result3.A}+{result3.B}i");
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i");

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

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

// test 6: call IPC service method returning GUID
Guid generatedId = await client.InvokeAsync(x => x.GenerateId());
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 client.InvokeAsync(x => x.ReverseBytes(input));
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");

}
catch (Exception ex)
{
Expand Down
28 changes: 27 additions & 1 deletion src/IpcServiceSample.ConsoleServer/ComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using IpcServiceSample.ServiceContracts;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -36,7 +39,30 @@ public float AddFloat(float x, float y)
return x + y;
}

public string ConvertText(string text, TextStyle style)
{
switch (style)
{
case TextStyle.TitleCase:
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(text);
case TextStyle.Upper:
return CultureInfo.InvariantCulture.TextInfo.ToUpper(text);
default:
return text;
}
}

public void DoNothing()
{ }

public Guid GenerateId()
{
return Guid.NewGuid();
}

public byte[] ReverseBytes(byte[] input)
{
return input.Reverse().ToArray();
}
}
}
12 changes: 11 additions & 1 deletion src/IpcServiceSample.ServiceContracts/IComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace IpcServiceSample.ServiceContracts
{
Expand All @@ -8,6 +9,9 @@ public interface IComputingService
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
void DoNothing();
string ConvertText(string text, TextStyle style);
Guid GenerateId();
byte[] ReverseBytes(byte[] input);
}

public class ComplexNumber
Expand All @@ -21,4 +25,10 @@ public ComplexNumber(float a, float b)
B = b;
}
}

public enum TextStyle
{
TitleCase,
Upper
}
}
8 changes: 4 additions & 4 deletions src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace JKang.IpcServiceFramework
public class IpcServiceClient<TInterface>
where TInterface : class
{
private static readonly ProxyGenerator _proxyGenerator = new ProxyGenerator();
private readonly string _pipeName;
private readonly IIpcMessageSerializer _serializer;
private readonly IValueConverter _converter;
Expand Down Expand Up @@ -77,8 +78,7 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
throw new ArgumentException("Only support calling method, ex: x => x.GetData(a, b)");
}

var proxyGenerator = new ProxyGenerator();
TInterface proxy = proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor);
TInterface proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor);
Delegate @delegate = lamdaExp.Compile();
@delegate.DynamicInvoke(proxy);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>JKang.IpcServiceFramework</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Jacques Kang</Authors>
<Company />
<Copyright>Jacques Kang</Copyright>
<PackageLicenseUrl>https://github.com/jacqueskang/IpcServiceFramework/blob/develop/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<PackageReleaseNotes>1.0.2
- support implicitly converting input parameters from derived type to base type
1.0.1
- support passing array parameters</PackageReleaseNotes>
<Version>1.0.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ public void TryConvert_DerivedTypeToBaseType()
Assert.IsInstanceOfType(actual, typeof(ComplexType));
}

[TestMethod]
public void TryConvert_StringToEnum()
{
EnumType expected = EnumType.SecondOption;

bool succeed = _sut.TryConvert(expected.ToString(), typeof(EnumType), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(EnumType));
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void TryConvert_Int32ToEnum()
{
EnumType expected = EnumType.SecondOption;

bool succeed = _sut.TryConvert((int)expected, typeof(EnumType), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(EnumType));
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void TryConvert_StringToGuid()
{
var expected = Guid.NewGuid();

bool succeed = _sut.TryConvert(expected.ToString(), typeof(Guid), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(Guid));
Assert.AreEqual(expected, actual);
}

interface IComplexType
{
int Int32Value { get; }
Expand All @@ -151,5 +187,11 @@ class ComplexType : IComplexType
public int Int32Value { get; set; }
public string StringValue { get; set; }
}

enum EnumType
{
FirstOption,
SecondOption
}
}
}
7 changes: 6 additions & 1 deletion src/JKang.IpcServiceFramework.Core/IO/IpcReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;

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

public IpcReader(Stream stream, IIpcMessageSerializer serializer)
: this(stream, serializer, leaveOpen: false)
{ }

public IpcReader(Stream stream, IIpcMessageSerializer serializer, bool leaveOpen)
{
_reader = new BinaryReader(stream);
_reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen);
_serializer = serializer;
}

Expand Down
9 changes: 7 additions & 2 deletions src/JKang.IpcServiceFramework.Core/IO/IpcWriter.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.IO;
using System.Text;

namespace JKang.IpcServiceFramework.IO
{
public class IpcWriter: IDisposable
public class IpcWriter : IDisposable
{
private readonly BinaryWriter _writer;
private readonly IIpcMessageSerializer _serializer;

public IpcWriter(Stream stream, IIpcMessageSerializer serializer)
: this(stream, serializer, leaveOpen: false)
{ }

public IpcWriter(Stream stream, IIpcMessageSerializer serializer, bool leaveOpen)
{
_writer = new BinaryWriter(stream);
_writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen);
_serializer = serializer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>JKang.IpcServiceFramework</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Jacques Kang</Authors>
<Company />
<Description>A lightweight .NET Core inter-process communication framework allowing invoking a service via named pipeline in a similar way as WCF.</Description>
<Copyright>Jacques Kang</Copyright>
<PackageLicenseUrl>https://github.com/jacqueskang/IpcServiceFramework/blob/develop/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<PackageReleaseNotes>1.0.2
- support implicitly converting input parameters from derived type to base type
1.0.1
- support passing array parameters</PackageReleaseNotes>
<Version>1.0.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DefaultIpcMessageSerializer : IIpcMessageSerializer
{
private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.None
};

public IpcRequest DeserializeRequest(byte[] binary)
Expand Down
Loading

0 comments on commit 19a20e7

Please sign in to comment.