Skip to content

Commit

Permalink
Merge pull request #152 from jacqueskang/develop
Browse files Browse the repository at this point in the history
v3.0.4
  • Loading branch information
jacqueskang authored Jul 5, 2020
2 parents 1b99b47 + e3f830d commit 8208ef2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Named pipeline and TCP support out-of-the-box, extensible with other protocols.
.ConfigureIpcHost(builder =>
{
// configure IPC endpoints
builder.AddNamedPipeEndpoint<IInterProcessService>(pipeName: "my-pipe");
builder.AddNamedPipeEndpoint<IInterProcessService>(pipeName: "pipeinternal");
})
.ConfigureLogging(builder =>
{
Expand Down
2 changes: 1 addition & 1 deletion build/version.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
variables:
version: '3.0.3'
version: '3.0.4'
53 changes: 32 additions & 21 deletions src/JKang.IpcServiceFramework.Client/IpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Castle.DynamicProxy;
using JKang.IpcServiceFramework.IO;
using JKang.IpcServiceFramework.IO;
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -12,7 +12,6 @@ namespace JKang.IpcServiceFramework.Client
public abstract class IpcClient<TInterface> : IIpcClient<TInterface>
where TInterface : class
{
private static readonly ProxyGenerator _proxyGenerator = new ProxyGenerator();
private readonly IpcClientOptions _options;

protected IpcClient(
Expand All @@ -31,7 +30,7 @@ protected IpcClient(
public async Task InvokeAsync(Expression<Action<TInterface>> exp,
CancellationToken cancellationToken = default)
{
IpcRequest request = GetRequest(exp, new MyInterceptor());
IpcRequest request = GetRequest(exp, DispatchProxy.Create<TInterface, IpcProxy>());
IpcResponse response = await GetResponseAsync(request, cancellationToken).ConfigureAwait(false);

if (!response.Succeed())
Expand All @@ -46,7 +45,7 @@ public async Task InvokeAsync(Expression<Action<TInterface>> exp,
public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TResult>> exp,
CancellationToken cancellationToken = default)
{
IpcRequest request = GetRequest(exp, new MyInterceptor<TResult>());
IpcRequest request = GetRequest(exp, DispatchProxy.Create<TInterface, IpcProxy<TResult>>());
IpcResponse response = await GetResponseAsync(request, cancellationToken).ConfigureAwait(false);

if (!response.Succeed())
Expand All @@ -68,7 +67,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TRes
public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
CancellationToken cancellationToken = default)
{
IpcRequest request = GetRequest(exp, new MyInterceptor<Task>());
IpcRequest request = GetRequest(exp, DispatchProxy.Create<TInterface, IpcProxy<Task>>());
IpcResponse response = await GetResponseAsync(request, cancellationToken).ConfigureAwait(false);

if (!response.Succeed())
Expand All @@ -83,7 +82,7 @@ public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task<TResult>>> exp,
CancellationToken cancellationToken = default)
{
IpcRequest request = GetRequest(exp, new MyInterceptor<Task<TResult>>());
IpcRequest request = GetRequest(exp, DispatchProxy.Create<TInterface, IpcProxy<Task<TResult>>>());
IpcResponse response = await GetResponseAsync(request, cancellationToken).ConfigureAwait(false);

if (!response.Succeed())
Expand All @@ -101,7 +100,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task
}
}

private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
private static IpcRequest GetRequest(Expression exp, TInterface proxy)
{
if (!(exp is LambdaExpression lambdaExp))
{
Expand All @@ -113,21 +112,20 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
throw new ArgumentException("Only support calling method, ex: x => x.GetData(a, b)");
}

TInterface proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(interceptor);
Delegate @delegate = lambdaExp.Compile();
@delegate.DynamicInvoke(proxy);

return new IpcRequest
{
MethodName = interceptor.LastInvocation.Method.Name,
Parameters = interceptor.LastInvocation.Arguments,
MethodName = (proxy as IpcProxy).LastInvocation.Method.Name,
Parameters = (proxy as IpcProxy).LastInvocation.Arguments,

ParameterTypes = interceptor.LastInvocation.Method.GetParameters()
ParameterTypes = (proxy as IpcProxy).LastInvocation.Method.GetParameters()
.Select(p => p.ParameterType)
.ToArray(),


GenericArguments = interceptor.LastInvocation.GenericArguments,
GenericArguments = (proxy as IpcProxy).LastInvocation.Method.GetGenericArguments(),
};
}

Expand All @@ -148,22 +146,35 @@ private async Task<IpcResponse> GetResponseAsync(IpcRequest request, Cancellatio
}
}

private class MyInterceptor : IInterceptor
public class IpcProxy : DispatchProxy
{
public IInvocation LastInvocation { get; private set; }
public Invocation LastInvocation { get; protected set; }

public virtual void Intercept(IInvocation invocation)
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
LastInvocation = invocation;
LastInvocation = new Invocation(targetMethod, args);
return null;
}

public class Invocation
{
public Invocation(MethodInfo method, object[] args)
{
Method = method;
Arguments = args;
}

public MethodInfo Method { get; }
public object[] Arguments { get; }
}
}

private class MyInterceptor<TResult> : MyInterceptor
public class IpcProxy<TResult> : IpcProxy
{
public override void Intercept(IInvocation invocation)
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
base.Intercept(invocation);
invocation.ReturnValue = default(TResult);
LastInvocation = new Invocation(targetMethod, args);
return default(TResult);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.0" />
<PackageReference Include="System.Reflection.DispatchProxy" Version="4.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 8208ef2

Please sign in to comment.