zoukankan      html  css  js  c++  java
  • cad.net 跨进程通讯Remoting

    cad跨进程通讯Remoting

    正在研究cad的跨进程通讯,
    难点在于cad是服务端的话,让客户端直接调用暴露的接口,而不会暴露实现,
    因此写了一个Remoting的案例,如下代码就可以进行了.

    至于WCF工程,由于它的App.config处理有些地方不是很理解,之后再仔细研究.

    动图演示

    工程结构

    1. 服务端工程 --> cad的类库,透过cad调用 --> 引用端口工程
    2. 客户端工程 --> 控制台工程 --> 引用端口工程
    3. 端口工程 --> 类库

    1.服务端工程

    #if !HC2020
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
    #else
    using GrxCAD.ApplicationServices;
    using GrxCAD.DatabaseServices;
    using GrxCAD.EditorInput;
    using Acap = GrxCAD.ApplicationServices.Application;
    #endif
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System;
    using JoinBox.IAdapter;
    
    namespace JoinBox.Service
    {
        public class Remoting
        {
            /// <summary>
            /// 注册服务管道 Remoting,实现占用端口
            /// </summary>
            [JoinBoxInitialize]
            public void RegisterServerChannel()
            {
                var channel = new TcpServerChannel(AdapterHelper.Port);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(BaseService),            //服务端实现(通讯地址接口)的函数
                    AdapterHelper.TcpPortAdapter,   //通讯地址接口
                    WellKnownObjectMode.Singleton);
            }
        }
    
        // 服务端实现接口工程的方法,在cad命令栏打印
        public class BaseService : MarshalByRefObject, IJoinBoxAdapter
        {
            public void PostAcedString(string str)
            {
                //阻塞线程并切换上下文,另见 https://www.cnblogs.com/JJBox/p/14179921.html
                AutoGo.SyncManager.SyncContext.Post(() => {
                    Document doc = Acap.DocumentManager.MdiActiveDocument;
                    Database db = doc.Database;
                    Editor ed = doc.Editor;
                    ed.WriteMessage(str);
                });
            }
        }
    }
    

    2.客户端工程 JoinBox.Client

    using System;
    using System.Threading;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using JoinBox.IAdapter;
    
    namespace JoinBox.Client
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                ChannelServices.RegisterChannel(new TcpClientChannel(), false);
                var adapter = (IJoinBoxAdapter)Activator.GetObject(typeof(IJoinBoxAdapter), AdapterHelper.TcpPortFullName);
                while (true)
                {
                    var str = DateTime.Now.ToString() + "
    ";
                    Console.WriteLine(str);
                    adapter.PostAcedString(str);//调用接口工程的方法.
                    Thread.Sleep(1000);
                }
            }
        }
    }
    

    3.接口工程 JoinBox.IAdapter

    此工程同时暴露给服务端和客户端利用

    namespace JoinBox.IAdapter
    {
        //公共接口和方法
        public interface IJoinBoxAdapter
        {
            public void PostAcedString(string str);
        }
    
        //公共的属性
        public class AdapterHelper
        {
            /// <summary>
            /// 端口
            /// </summary>
            public static int Port               = 15341;
            /// <summary>
            /// 通讯地址(接口)--服务端用
            /// </summary>
            public static string TcpPortAdapter  = "jingjing/" + nameof(IJoinBoxAdapter);
            /// <summary>
            /// 通讯地址(完整)--客户端用
            /// </summary>
            public static string TcpPortFullName = $"tcp://localhost:{Port}/" + TcpPortAdapter;
        }
    }
    

    (完)

  • 相关阅读:
    Atitit.Java exe bat  作为windows系统服务程序运行
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结
  • 原文地址:https://www.cnblogs.com/JJBox/p/14992966.html
Copyright © 2011-2022 走看看