GithubHelp home page GithubHelp logo

handyorg / handyipc Goto Github PK

View Code? Open in Web Editor NEW
28.0 5.0 9.0 341 KB

An out-of-the-box inter-process communication (IPC) library, which can also be seen as a remote Ioc container.

License: MIT License

C# 100.00%
ipc remote-method-invocation msbuild msbuild-task csharp handy source-generator dotnet ioc ioc-container

handyipc's People

Contributors

dingpingzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

handyipc's Issues

是否可以尝试去掉System.ValueTuple?

我自己试了一下更改源代码发现工作量太大了。.....
我知道 (key, interfaceType, factory) 这种写法很酷,但是有一个致命的问题。
我在net4.6.2里面生成的文件会造成非常多的依赖项...导致我项目非常的不干净。
其实你可以采用元祖Tuple<int, int>也是一样的。

image

长时间运行会出现 attempted to read or write protected memory

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Threading.ThreadPoolWorkQueue.TryDequeueTimeSensitiveWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dequeue(System.Threading.ThreadPoolWorkQueueThreadLocals, Boolean ByRef)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()

语法分析无法识别所有形式的 [IpcContractAttribute]

由语法分析得到的 AttributeSyntax.Name 只能获得用户输入的原样字符串,故一个 Attribute 可能有以下的多种形式:

  • IpcContract
  • IpcContractAttribute
  • HandyIpc.IpcContract
  • HandyIpc.IpcContractAttribute

甚至可能无法去除末尾的空格、换行符、tab 等不影响语法的无意义字符:

  • IpcContract \t\r\n

迁移到语义化模型进行分析可以解决此问题。

ArgumentException: Could not cast or convert from System.String to System.Type.

Newtonsoft.Json.JsonSerializationException
  HResult=0x80131500
  Message=Error converting value "System.NotSupportedException, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" to type 'System.Type'. Path '', line 1, position 121.
  Source=Newtonsoft.Json
  StackTrace:
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at HandyIpc.Serializer.Json.Extensions.ToObject(Byte[] bytes, Type type)
   at HandyIpc.Serializer.Json.JsonSerializer.Deserialize(Byte[] bytes, Type type)
   at HandyIpc.Core.Response.TryParse(Byte[] bytes, Type valueType, ISerializer serializer, Object& value, Exception& exception)
   at HandyIpc.Core.GeneratorHelper.UnpackResponse[T](Byte[] bytes, ISerializer serializer)
   at IpcServer.ClientProxyIDriver.global::IpcServer.IDriver.AddFloat(Single x_, Single y_) in D:\heart\Git\IpcDemo\IpcClientDll\HandyIpc.Generator\HandyIpc.Generator.SourceGenerator\IDriver.ClientProxy.g.cs:line 33
   at IpcClientDll.IpcDriver.AddFloat(Single x, Single y) in D:\heart\Git\IpcDemo\IpcClientDll\IpcDriver.cs:line 91
   at IpcClient.Program.Main(String[] args) in D:\heart\Git\IpcDemo\IpcClient\Program.cs:line 23

  此异常最初是在此调用堆栈中引发的: 
    [外部代码]

内部异常 1:
ArgumentException: Could not cast or convert from System.String to System.Type.

image

    [IpcContract]
    public interface IDriver
    {
        float AddFloat(float x, float y);

        void WriteMemery(uint addr, byte[] data);

        byte[] ReadMemery(uint addr, uint lens);
    }

IpcContract 接口支持 IDisposable 接口

设计行为:当检测到 [IpcContract] 接口派生自 IDisposable 时,会在客户端侧生成的代码(ClientProxy)里自动实现 Dispose() 方法,其行为是:调用该方法时,释放 IpcClientHub 内部的 ClientProxy 实例缓存。

合同接口(IpcContract)支持事件

背景:

当前的合同接口仅支持方法成员,按目前设定:Server 在同一个域内应该是唯一的,不允许启动第二个 Server 实例,以避免 Client 无法区分服务的提供者;而 Client 方则允许创建多个实例与唯一的 Server 交互,但交互方式仅为:Client 调用 Server,Server 在本次调用中应答一次 Client,是单对单的交互方式,还缺乏一种单对多的通知方式,即多播事件。

方案:

使当前的合同接口可以定义事件,事件的行为应当是:Server 发布事件后,所有 Client 实例(可能有多个)中订阅了该事件的 Handler 都应该被触发。Client 之间若想交互,仅可以通过调用 Server 方法以触发某个 Server Event 的方式进行。

实现:

该 Feature 本质上是:

  1. Server 和 Client 的角色互换,
  2. 且 Server 还需要储存多个连接到它的 Client 实例,以做到多播。

考虑到以下问题,我们并不能简单地将目前的 SenderBaseReceiverBase 进行互换:Tcp 中,同一台机器中的同一个端口仅允许被一个实例监听,如果 Client 侧也创建 ReceiverBase,那么端口号将不方便选取,选取后也还需要通知 Server 侧,实现起来比较麻烦;NamedPipe 虽然允许创建多个同名 Pipe,但多个进程中包含的同名 ServerStream 也会导致 Client 无法区分正确的连接目标。
所以,应当借助 Tcp 和 NamedPipe 的全双工能力实现反向的通知。具体的:

  1. 当 Client 第一次订阅事件时(可以在 ClientProxy 中拦截到),Client 向 Server 发送一个订阅请求;
  2. Server 处理该请求,并缓存本次连接。当 Server 中该事件被触发后,将向所有缓存中的连接推送消息;
  3. 当 Client 取消事件订阅后,并且 handlerCounter 计数器归零时,Client 向 Server 发送一个订阅请求;
  4. Server 释放相应的连接资源。

内存是否哪里没有释放干净?

服务端代码

        static void Main(string[] args)
        {
            // 创建一个容器服务器 Builder。
            ContainerServerBuilder serverBuilder = new ContainerServerBuilder();
            serverBuilder.UseNamedPipe("JgkljServe").UseJsonSerializer();
            serverBuilder.Register<IHansLaserServe, HansLaserServe>();
            using (var server = serverBuilder.Build())
            {
                server.Start();

                //检查状态进行阻塞,降低CPU使用率
                while (server.IsRunning)
                    Task.Delay(1000).Wait();
            }
        }

启动时内存(2.8M)
image
执行了50次方法后(4.8M)(我的方法是调用别人的dll,不存在自身的内存问题)
image

The bytes is not valid response data.

发生异常: CLR/System.ArgumentException
“System.ArgumentException”类型的未经处理的异常在 HandyIpc.dll 中发生 : 'The bytes is not valid response data.'
在 HandyIpc.Core.Response.TryParse(Byte[] bytes, Type valueType, ISerializer serializer, Object& value, Exception& exception)
在 HandyIpc.Core.GeneratorHelper.UnpackResponse[T](Byte[] bytes, ISerializer serializer)
在Driver.ClientProxyIDriver.global::Driver.IDriver.WriteMemory(UInt32 addr_, UInt32 value_, Nullable`1 channel_)

uint ReadMemory(uint addr, int? channel = null); // 这个正常,

void WriteMemory(uint addr, uint value, int? channel = null); 这个报错

我的接口如上,我通过IPC ,显示错误。
image

完善测试用例

针对该库所提供的 feature,编写对应的测试用例。本库中的测试用例应面向接口编写,因为具体的通讯协议有很多,如目前的 Named Pipe、Tcp 等,但它们应该用同一套测试用例。

测试用例 TODO List:

  • 【代码生成器】应对非合同接口所支持的成员进行报错提示
  • 【代码生成器】应对方法中的可变字段(尤其是 CancellationToken)报以 Not Supported 警告
  • 【代码生成器】应对方法中的回调函数报以 Not Supported 警告 (#7)
  • 【合同接口】测试对事件的支持 (#12)

Error running example from README.md

I'm getting:
System.InvalidOperationException: 'IDemo doesn't look like a Ipc interface.'

When running:

[IpcContract]
public interface IDemo
{
    double Add(double x, double y);
}

public class Demo : IDemo
{
    public double Add(double x, double y)
    {
        return x + y;
    }
}
ContainerServerBuilder serverBuilder = new ContainerServerBuilder();
serverBuilder.UseNamedPipe("ec57043f-465c-4766-ae49-b9b1ee9ac571");

serverBuilder.Register<IDemo, Demo>();

using (var server = serverBuilder.Build())
{
    server.Start();
}

What am I doing wrong?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.