基于IPC-CFX的点对点通信C#

        IPC-CFX有两种主要的通信方式,可以通过RabbitMQ发布和订阅,也可以通过request和response进行点对点的通信,本文主要讲的是点对点的通信方式。

        在vscode里建立新的dotnet项目,可以通过终端输入dotnet new console来建立,文件目录为CFXDemo->machine1和CFXDemo->machine2。

        通过nuget插件分别为两个项目都安装CFX.CFXSDK、AMQPNetLite.Core和Newtonsoft.Json这几个metapackage。

         我们将machine1作为发送端(sendRequest),machine2作为接收端(Receive),则Machine1的代码如下所示:

using System.Threading;
using CFX;
using CFX.Transport;
using System;
using System.Security.Principal;namespace machine1
{class Program{static void Main(string[] args){OpenRequest();Console.ReadLine();for(int i = 0;i<5;i++){SendRequest();Thread.Sleep(2000);}}static string sendCFXHandle = "a.b.001";     static string receiveCFXHandle = "a.b.002";static string sendRequestUri = string.Format("amqp://127.0.0.1:1235");static string receivRequestUri = string.Format("amqp://127.0.0.1:1234");#region send requeststatic AmqpCFXEndpoint endpointSendRequest;static void OpenRequest(){if (endpointSendRequest != null){endpointSendRequest.Close();endpointSendRequest = null;}endpointSendRequest = new AmqpCFXEndpoint();if (!endpointSendRequest.IsOpen){Console.WriteLine("endpointSendRequest.IsOpen:" + endpointSendRequest.IsOpen.ToString());endpointSendRequest.Open(sendCFXHandle);    //这一步会绑定endpointSendRequest里的CFXHandle,即sendCFXHandle的值Console.WriteLine("Request.Source is : {0}",endpointSendRequest.CFXHandle);Console.WriteLine("endpointSendRequest.IsOpen:" + endpointSendRequest.IsOpen.ToString());}// Set a timeout of 20 seconds.  If the target endpoint does not// respond in this time, the request will time out.AmqpCFXEndpoint.RequestTimeout = TimeSpan.FromSeconds(20);}static void SendRequest(){// Build a GetEndpointInfomation RequestCFXEnvelope request = CFXEnvelope.FromCFXMessage(new GetEndpointInformationRequest(){CFXHandle = receiveCFXHandle});request.Source = endpointSendRequest.CFXHandle;request.Target = receiveCFXHandle;try{CFXEnvelope response = endpointSendRequest.ExecuteRequest(receivRequestUri, request);Console.WriteLine($"response:\n{response.ToJson(true)}");}catch (Exception ex){Console.WriteLine(ex.Message);}}#endregion send request#region receive requeststatic AmqpCFXEndpoint endpointReceiveRequest;static void OpenReceive(){if (endpointReceiveRequest != null){endpointReceiveRequest.Close();endpointReceiveRequest = null;}endpointReceiveRequest = new AmqpCFXEndpoint();endpointReceiveRequest.OnRequestReceived -= Endpoint_OnRequestReceived;endpointReceiveRequest.OnRequestReceived += Endpoint_OnRequestReceived;if (!endpointReceiveRequest.IsOpen){Console.WriteLine("endpointSendRequest.IsOpen:" + endpointReceiveRequest.IsOpen.ToString());endpointReceiveRequest.Open(receiveCFXHandle, new Uri(receivRequestUri));Console.WriteLine("endpointSendRequest.IsOpen:" + endpointReceiveRequest.IsOpen.ToString());}}static CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request){Console.WriteLine($"Endpoint_OnRequestReceived:  { request.ToString()}");// Process request.  Return Result.if (request.MessageBody is WhoIsThereRequest){CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse(){ CFXHandle = receiveCFXHandle, RequestNetworkUri = receivRequestUri, RequestTargetAddress = "" });result.Source = receiveCFXHandle;result.Target = request.Source;result.TimeStamp = DateTime.Now;return result;}if (request.MessageBody is GetEndpointInformationRequest){CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse(){ CFXHandle = receiveCFXHandle, RequestNetworkUri = receivRequestUri, RequestTargetAddress = "...." });result.Source = receiveCFXHandle;result.Target = request.Source;result.TimeStamp = DateTime.Now;return result;}return null;}#endregion receive request}
}

        作为接收端,machine2的代码如下所示:

using CFX;
using CFX.Transport;
using System;namespace machine2
{class Program{static void Main(string[] args){Console.WriteLine("ReceivEndPoint is waiting Request......");OpenReceive();Console.WriteLine("Press Enter Key to end the App");Console.ReadKey();}static string sendCFXHandle = "a.b.001";static string receiveCFXHandle = "a.b.002";static string sendRequestUri = string.Format("amqp://127.0.0.1:1235");static string receivRequestUri = string.Format("amqp://127.0.0.1:1234");#region send requeststatic AmqpCFXEndpoint endpointSendRequest;static void OpenRequest(){if (endpointSendRequest != null){endpointSendRequest.Close();endpointSendRequest = null;}endpointSendRequest = new AmqpCFXEndpoint();if (!endpointSendRequest.IsOpen){Console.WriteLine("endpointSendRequest.IsOpen:" + endpointSendRequest.IsOpen.ToString());endpointSendRequest.Open(receiveCFXHandle, new Uri(receivRequestUri));Console.WriteLine("endpointSendRequest.IsOpen:" + endpointSendRequest.IsOpen.ToString());}// Set a timeout of 20 seconds.  If the target endpoint does not// respond in this time, the request will time out.AmqpCFXEndpoint.RequestTimeout = TimeSpan.FromSeconds(20);}static void SendRequest(){// Build a GetEndpointInfomation RequestCFXEnvelope request = CFXEnvelope.FromCFXMessage(new GetEndpointInformationRequest(){CFXHandle = receiveCFXHandle});request.Source = endpointSendRequest.CFXHandle;request.Target = receiveCFXHandle;try{CFXEnvelope response = endpointSendRequest.ExecuteRequest(receivRequestUri, request);Console.WriteLine($"response:\n{response.ToJson(true)}");}catch (Exception ex){Console.WriteLine(ex.Message);}}#endregion send request#region receive requeststatic AmqpCFXEndpoint endpointReceiveRequest;static void OpenReceive(){if (endpointReceiveRequest != null){endpointReceiveRequest.Close();endpointReceiveRequest = null;}endpointReceiveRequest = new AmqpCFXEndpoint();endpointReceiveRequest.OnRequestReceived -= Endpoint_OnRequestReceived;endpointReceiveRequest.OnRequestReceived += Endpoint_OnRequestReceived;if (!endpointReceiveRequest.IsOpen){Console.WriteLine("endpointSendRequest.IsOpen:" + endpointReceiveRequest.IsOpen.ToString());endpointReceiveRequest.Open(receiveCFXHandle, new Uri(receivRequestUri));//endpointReceiveRequest.Open(receiveCFXHandle);Console.WriteLine("endpointSendRequest.IsOpen:" + endpointReceiveRequest.IsOpen.ToString());}}static CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request){Console.WriteLine($"Endpoint_OnRequestReceived:  { request.ToString()}");Console.WriteLine($"request:\n{request.ToJson(true)}");// Process request.  Return Result.if (request.MessageBody is WhoIsThereRequest){CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse(){ CFXHandle = receiveCFXHandle, RequestNetworkUri = receivRequestUri, RequestTargetAddress = "" });result.Source = receiveCFXHandle;result.Target = request.Source;result.TimeStamp = DateTime.Now;return result;}if (request.MessageBody is GetEndpointInformationRequest){CFXEnvelope result = CFXEnvelope.FromCFXMessage(new WhoIsThereResponse(){ CFXHandle = receiveCFXHandle, RequestNetworkUri = receivRequestUri, RequestTargetAddress = "..." });result.Source = receiveCFXHandle;result.Target = request.Source;result.TimeStamp = DateTime.Now;return result;}return null;}#endregion receive request}
}

         运行结果,可以用json格式对response和request的内容进行解析。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/2728.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【拿来就能用】C#指定打印机打印的类

之前写过一个“C#WinForm程序中选择打印机打印”的文章&#xff0c;但在使用过程中&#xff0c;尤其是生成Word文档时&#xff0c;会感觉系统响应较慢。如果不需要留存打印文档的电子版&#xff0c;可以使用下面的类直接打印。相比之前的方法&#xff0c;这种方法更简单&#x…

MySQL常用语句大全

语句 DDL MySQL的DDL&#xff08;数据定义语言&#xff09;是一组用于创建、修改和删除数据库、表、索引、视图、存储过程和触发器等数据库对象的语句。下面是一些常用的MySQL DDL语句和它们的详细说明&#xff1a; alter 在MySQL中&#xff0c;DDL&#xff08;数据定义语言…

LCD—STM32液晶显示(2.使用FSMC模拟8080时序)

目录 使用STM32的FSMC模拟8080接口时序 FSMC简介 FSMC NOR/PSRAM中的模式B时序图 用FSMC模拟8080时序 重点&#xff1a;HADDR内部地址与FSMC地址信号线的转换&#xff08;实现地址对齐&#xff09; 使用STM32的FSMC模拟8080接口时序 ILI9341的8080通讯接口时序可以由STM32使…

北邮国院物联网 Microprocessor 微处理器笔记

Introduction-随便聊 嵌入式系统是什么&#xff1f;专用的计算机系统。为专门功能可能对计算机架构&#xff0c;外设等做出一些取舍。 通常的限制&#xff1a;Cost&#xff08;比如大量部署传感器节点&#xff09;&#xff0c;Size and weight limits&#xff08;特定应用场景…

配置Hadoop_0

配置Hadoop_0 1配置Hadoop100模板虚拟机1.1配置Hadoop100模板虚拟机硬件1.2配置Hadoop100模板虚拟机软件1.3配置Hadoop100模板虚拟机IP地址1.4配置Hadoop100模板虚拟机主机名称/主机名称映射1.5配置Hadoop100模板虚拟机远程操作工具 1配置Hadoop100模板虚拟机 Hadoop100 内存…

TRT4-trt-integrate - 1 YOLOV5导出、编译、推理

模型导出 修改Image的Input动态维度 首先可以看到这个模型导出的时候Input有三个维度都是动态&#xff0c;而我们之前说过只需要一个batch维度是动态&#xff0c;所以要在export的export onnx 进行修改&#xff0c;将 torch.onnx.export(model, im, f, verboseFalse, opset_ver…

华为云子网路由表作用及价值

子网路由表 子网路由表作用云专线、VPN的配置与子网路由表强关联&#xff0c;本质是在相应的子网路由表中添加了一条路由Nat路由表问题地址变更问题snat和dnat 子网路由表作用 子网内部作为一个二层网络&#xff0c;通过mac地址互通&#xff0c;不通过路由互通。跨子网&#x…

实时网络更改检测

未经授权的配置更改可能会对业务连续性造成严重破坏&#xff0c;这就是为什么使用实时更改检测来检测和跟踪更改是网络管理员的一项关键任务。尽管可以手动跟踪更改&#xff0c;但此方法往往非常耗时&#xff0c;并且通常会导致人为错误&#xff0c;例如在跟踪时错过关键网络设…

企业需要一个数字体验平台(DXP)吗?

数字体验平台是一个软件框架&#xff0c;通过与不同的业务系统喝解决方案集成&#xff0c;帮助企业和机构建立、管理和优化跨渠道的数字体验。帮助企业实现跨网站、电子邮件、移动应用、社交平台、电子商务站点、物联网设备、数字标牌、POS系统等传播内容&#xff0c;除了为其中…

文心一言 VS 讯飞星火 VS chatgpt (58)-- 算法导论6.4 2题

文心一言 VS 讯飞星火 VS chatgpt &#xff08;58&#xff09;-- 算法导论6.4 2题 二、试分析在使用下列循环不变量时&#xff0c;HEAPSORT 的正确性&#xff1a;在算法的第 2~5行 for 循环每次迭代开始时&#xff0c;子数组 A[1…i]是一个包含了数组A[1…n]中第i小元素的最大…

IntelliJ IDEA 2023.1 更新内容总结

IntelliJ IDEA 2023.1 更新内容总结 * 主要更新内容 * UI 大改版 * 性能改进项 * 其它更新内容IntelliJ IDEA 2023.1 更新内容总结 主要更新内容 IntelliJ IDEA 2023.1 针对新的用户界面进行了大量重构,这些改进都是基于收到的宝贵反馈而实现的。官方还实施了性能增强措施, …

如果微信消息显示“已读”的话......

近日&#xff0c;一则 #如果微信显示已读的话# 话题冲上了微博热搜榜单。 “已读”是很多社交软件拥有的功能&#xff0c;如果对方接收并查看了消息&#xff0c;就会在消息上显示“已读”&#xff0c;但目前微信还没有推出这项功能。 对于“已读”功能&#xff0c;不少网友纷纷…

自动化用例编写思路 (使用pytest编写一个测试脚本)

目录 一&#xff0c;明确测试对象 二&#xff0c;编写测试用例 构造请求数据 封装测试代码 断言设置 三&#xff0c;执行脚本获取测试结果 四&#xff0c;总结 经过之前的学习铺垫&#xff0c;我们尝试着利用pytest框架编写一条接口自动化测试用例&#xff0c;来厘清接口…

Unity Hub下载中文一直验证中怎么办

Unity Hub是Unity官方提供的一款管理Unity引擎和项目的工具。然而&#xff0c;有时在下载中文版的Unity Hub时可能会遇到“验证中”的情况&#xff0c;这可能会导致下载进程无法继续。本文将介绍一些可能的解决方法&#xff0c;帮助您处理Unity Hub下载中文版本出现“验证中”问…

TypeScript面试题汇总

1、面试官&#xff1a;说说你对 TypeScript 的理解&#xff1f;与 JavaScript 的区别&#xff1f; Typescript 是 JavaScript 的超集&#xff0c;可以被编译成 JavaScript 代码。 用 JavaScript 编写的合法代码&#xff0c;在 TypeScript 中依然有效。它给JavaScript添加了可选…

【CNN记录】pytorch中BatchNorm2d

torch.nn.BatchNorm2d(num_features, eps1e-05, momentum0.1, affineTrue, track_running_statsTrue, deviceNone, dtypeNone) 功能&#xff1a;对输入的四维数组进行批量标准化处理&#xff08;归一化&#xff09; 计算公式如下&#xff1a; 对于所有的batch中样本的同一个ch…

商城-学习整理-基础-环境搭建(二)

目录 一、环境搭建1、安装linux虚拟机1&#xff09;下载&安装 VirtualBox https://www.virtualbox.org/&#xff0c;要开启 CPU 虚拟化2&#xff09;虚拟机的网络设置3&#xff09;虚拟机允许使用账号密码登录4&#xff09;VirtualBox冲突5&#xff09;修改 linux 的 yum 源…

PyCharm 常用快捷键

目录 1、代码编辑快捷键 2、搜索/替换快捷键 3、代码运行快捷键 4、代码调试快捷键 5、应用搜索快捷键 6、代码重构快捷键 7、动态模块快捷键 8、导航快捷键 9、通用快捷键 1、代码编辑快捷键 序号快捷键作用1CTRLALTSPACE快速导入任意类2CTRLSHIFTENTER代码补全3SHI…

[杂谈]反义词汇总

在看英文资料的时候&#xff0c;有意的总结了一下反义词&#xff1a; https://blog.csdn.net/qq_36428903/article/details/124958575 有些反义词总是能在一个句子里同时出现。就像中文里的经典成语&#xff1a;自相矛盾&#xff0c;左右逢源&#xff0c;七上八下……。这些相…

$.getScript()方法获取js文件

通过$.getScript(‘xxxx.js’)获取xxxx.js文件&#xff0c;这时的ajax是一个get请求的状态&#xff0c;如果进行了入参data的赋值那么他就会跟在url后面,同理获取json文件&#xff0c;css文件。 一开始没想起这茬。。。