Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加上传输使用的方法 #24

Merged
merged 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions demo/dotnetCampus.Ipc.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,6 @@

namespace dotnetCampus.Ipc.Demo
{

public class IpcProxy<T> : DispatchProxy
{
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
var actualReturnType = GetAndCheckActualReturnType(targetMethod.ReturnType);

return default!;
}

private Type GetAndCheckActualReturnType(Type returnType)
{
if (returnType == typeof(Task))
{
return typeof(void);
}
else if (returnType.BaseType == typeof(Task))
{
if (returnType.Name == "Task`1")
{
if (returnType.GenericTypeArguments.Length == 1)
{
return returnType.GenericTypeArguments[0];
}
}
}

throw new ArgumentException($"方法返回值只能是 Task 或 Task 泛形");
}
}

public interface IF1
{
Task<int> F2();
Expand All @@ -48,13 +17,6 @@ public interface IF1
void Fx();
}

class IpcClientProvider
{
public T GetObject<T>()
{
return DispatchProxy.Create<T, IpcProxy<T>>();
}
}

internal class Program
{
Expand Down Expand Up @@ -91,8 +53,7 @@ private static void Main(string[] args)
//Task.Run(LibearlafeCilinoballnelnall),
//Task.Run(LibearlafeCilinoballnelnall),
//Task.Run(WhejeewukawBalbejelnewearfe),
Task.Run(WheehakawlucearHalwahewurlaiwhair),
Task.Run(BaiqealawbawKeqakeyawaw)
Task.Run(WheehakawlucearHalwahewurlaiwhair), Task.Run(BaiqealawbawKeqakeyawaw)
};

Task.WaitAll(jalejekemNereyararli.ToArray());
Expand Down Expand Up @@ -121,7 +82,6 @@ private static void LerlocunaihukeajerJinenenay()
var ipcProvider = new IpcProvider();
ipcProvider.PeerConnected += (sender, proxy) =>
{

};
var peer = await ipcProvider.ConnectToPeerAsync(IpcContext.DefaultPipeName);
await peer.IpcMessageWriter.WriteMessageAsync("林德熙是逗比");
Expand All @@ -135,7 +95,6 @@ private static void LerlocunaihukeajerJinenenay()
var ipcProvider = new IpcProvider();
ipcProvider.PeerConnected += (sender, proxy) =>
{

};
var peer = await ipcProvider.ConnectToPeerAsync(IpcContext.DefaultPipeName);
Console.WriteLine($"连接上{peer.PeerName}");
Expand Down
1 change: 1 addition & 0 deletions demo/dotnetCampus.Ipc.Demo/dotnetCampus.Ipc.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\dotnetCampus.Ipc.PipeCore\dotnetCampus.Ipc.PipeCore.csproj" />
<ProjectReference Include="..\..\src\dotnetCampus.Ipc\dotnetCampus.Ipc.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/dotnetCampus.Ipc/IpcClientProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Diagnostics;
using System.Reflection;

namespace dotnetCampus.Ipc
{
/// <summary>
/// 提供客户端使用的方法,可以拿到服务器端的对象
/// </summary>
/// 在服务器端将会维护一个对象池,可以选的是一个对象可以作为单例或者作为每次访问返回新的对象
/// 如果是每次访问都返回的新的对象,就需要将这个对象发生过去的时候加上对象的标识,之后客户端调用的时候,就可以
/// 使用这个标识拿到对应的对象
public class IpcClientProvider
{
public T GetObject<T>()
{
#if NETCOREAPP
var obj= DispatchProxy.Create<T, IpcProxy<T>>();
var ipcProxy = obj as IpcProxy<T>;
Debug.Assert(ipcProxy!=null);
ipcProxy!.IpcClientProvider = this;
return obj;
#endif
// 还需要加上 NET45 使用的透明代理
throw new NotImplementedException();
}
}
}
45 changes: 45 additions & 0 deletions src/dotnetCampus.Ipc/IpcProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace dotnetCampus.Ipc
{
#if !NETCOREAPP
public abstract class DispatchProxy
{
protected abstract object Invoke(MethodInfo targetMethod, object[] args);
}
#endif

public class IpcProxy<T> : DispatchProxy
{
public IpcClientProvider IpcClientProvider { set; get; } = null!;

protected override object Invoke(MethodInfo targetMethod, object[] args)
{
var actualReturnType = GetAndCheckActualReturnType(targetMethod.ReturnType);

return default!;
}

private Type GetAndCheckActualReturnType(Type returnType)
{
if (returnType == typeof(Task))
{
return typeof(void);
}
else if (returnType.BaseType == typeof(Task))
{
if (returnType.Name == "Task`1")
{
if (returnType.GenericTypeArguments.Length == 1)
{
return returnType.GenericTypeArguments[0];
}
}
}

throw new ArgumentException($"方法返回值只能是 Task 或 Task 泛形");
}
}
}
27 changes: 27 additions & 0 deletions src/dotnetCampus.Ipc/IpcRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

namespace dotnetCampus.Ipc
{
/// <summary>
/// 发送给服务器的请求数据
/// </summary>
/// Copy From: https://github.com/jacqueskang/IpcServiceFramework.git
public class IpcRequest
{
public Type ObjectType { set; get; }

/// <summary>
/// 用来标识服务器端的对象
/// </summary>
public ulong ObjectId { set; get; }

public string MethodName { get; set; }

public List<IpcRequestParameter> ParameterList { set; get; }

public List<Type> GenericArgumentList { set; get; }

public Type ReturnType { set; get; }
}
}
11 changes: 11 additions & 0 deletions src/dotnetCampus.Ipc/IpcRequestParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace dotnetCampus.Ipc
{
public class IpcRequestParameter
{
public Type ParameterType { set; get; }

public object Value { set; get; }
}
}
59 changes: 59 additions & 0 deletions src/dotnetCampus.Ipc/IpcResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Runtime.Serialization;

namespace dotnetCampus.Ipc
{
/// <summary>
/// 从服务器返回的信息
/// </summary>
/// Copy From: https://github.com/jacqueskang/IpcServiceFramework.git
public class IpcResponse
{
public static IpcResponse Success(object data)
=> new IpcResponse(IpcStatus.Ok, data, null, null);

public static IpcResponse BadRequest()
=> new IpcResponse(IpcStatus.BadRequest, null, null, null);

public static IpcResponse BadRequest(string errorDetails)
=> new IpcResponse(IpcStatus.BadRequest, null, errorDetails, null);

public static IpcResponse BadRequest(string errorDetails, Exception innerException)
=> new IpcResponse(IpcStatus.BadRequest, null, errorDetails, innerException);

public static IpcResponse InternalServerError()
=> new IpcResponse(IpcStatus.InternalServerError, null, null, null);

public static IpcResponse InternalServerError(string errorDetails)
=> new IpcResponse(IpcStatus.InternalServerError, null, errorDetails, null);

public static IpcResponse InternalServerError(string errorDetails, Exception innerException)
=> new IpcResponse(IpcStatus.InternalServerError, null, errorDetails, innerException);

public IpcResponse(
IpcStatus status,
object data,
string errorMessage,
Exception innerException)
{
Status = status;
Data = data;
ErrorMessage = errorMessage;
InnerException = innerException;
}

[DataMember]
public IpcStatus Status { get; }

[DataMember]
public object Data { get; }

[DataMember]
public string ErrorMessage { get; set; }

[DataMember]
public Exception InnerException { get; }

public bool Succeed() => Status == IpcStatus.Ok;
}
}
14 changes: 14 additions & 0 deletions src/dotnetCampus.Ipc/IpcStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace dotnetCampus.Ipc
{
/// <summary>
/// 从服务器返回的值
/// </summary>
/// Copy From: https://github.com/jacqueskang/IpcServiceFramework.git
public enum IpcStatus : int
{
Unknown = 0,
Ok = 200,
BadRequest = 400,
InternalServerError = 500,
}
}