我的邻居,Windows 联系人以及邀请人都允许一个应用程序开始一个合作性的活动。在.NET Framework 3.5 之前,开发人员需要调用非托管APIs来使用这些特性。这意味着他们需要使用C++开发或者至少创建互操作程序集(使用P/Invoke)在.NET Framework 3.5 中使用的经历已经通过引入可以使用内建于Windows Vista中的我的邻居,Windows 联系人以及邀请人架构的托管库而取代。这些库在一个成为System.Net.PeerToPeer的新的命名空间中提供。一个开发人员需要向System.Net程序集中添加一个引用来使用这些新库。我们将查看一个叫做对等会话的简单的示例程序来了解如何使用这些新特性。图片12.14显示了对等会话应用程序。
图片12.14 对等会话示例程序
一个应用程序首先要考虑的是使用Windows Vista中的合作架构来注册它自己。如果你想要发送一个邀请来运行这个应用程序那么就需要它。列表12.9显示了对等会话程序如何使用对等架构注册自己。为了完成这个我们使用在System.Net.PeerToPeer.Collaboration命名空间找到的PeerCollaboration静态类来调用注册方法。这个方法接受一个PeerApplication类的实例。这个类的实例是对等应用程序的描述,包括一个应用程序标识符和一个描述信息。
列表12.9 注册一个对等应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.PeerToPeer.Collaboration;namespace PeerChat
{public class PeerChatApplication{private static PeerApplication PeerChatPeerApplication;private static Guid PeerChatAppId = new Guid("4BC6F59E-124E-4e75-8CD9-BB75BCA78CA8");private static string PeerChatDescription = "A sample peer networking application";static PeerChatApplication(){PeerChatPeerApplication =new PeerApplication(PeerChatAppId,PeerChatDescription,null,System.Windows.Forms.Application.ExecutablePath,null,PeerScope.All);}public static void Register(){PeerApplicationCollection peerAppsColl = PeerCollaboration.GetLocalRegisteredApplications(PeerApplicationRegistrationType.AllUsers);//You gotta love LINQ! It is so cool!IEnumerable<PeerApplication> findPeerApp =from peerApp inPeerCollaboration.GetLocalRegisteredApplications(PeerApplicationRegistrationType.AllUsers)where peerApp.Id == PeerChatAppIdselect peerApp;if (findPeerApp.Count<PeerApplication>() != 0){PeerCollaboration.UnregisterApplication(PeerChatPeerApplication,PeerApplicationRegistrationType.AllUsers);}PeerCollaboration.RegisterApplication(PeerChatPeerApplication,PeerApplicationRegistrationType.AllUsers);}public static void UnRegister(){PeerCollaboration.UnregisterApplication(PeerChatPeerApplication,PeerApplicationRegistrationType.AllUsers);}}
}

一个应用程序下一步可能要做的是显示在它的本地子网内的人员列表以便于用户可以邀请他们来参加活动。这是通过枚举加入到我的邻居架构中的人来完成的。一个用户需要被签名到我的邻居内才可以收集在他们附近的人。为了帮助这个过程,我们创建了一个帮助者类来保证用户在第一次请求之前已经签名了。列表12.10 显示了PeopleNearMeHelper类。这个类调用PeerCollaboration静态类中的SignIn方法来保证用户已经签入到我的邻居中。当用户签入以后,用户可以自由调用GetPeersNearMe静态方法,它会返回一个PeerNearMe实例集合。一个PeerNearMe集合是一个在本地子网内登陆到Peer Near Me内的人的描述。
列表12.10 People Near Me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.PeerToPeer.Collaboration;namespace PeerChat
{public class PeopleNearMeHelper{public PeopleNearMeHelper(){PeerCollaboration.SignIn(PeerScope.All);}public PeerNearMeCollection PeopleNearMe{get{return PeerCollaboration.GetPeersNearMe();}}}
}

下一步我们需要向另外一个人发送一个邀请以便于它可以参与进来。对等会话应用发送一个邀请给其他人来一起开始一次会话。为了实现这个,我们不仅仅需要发送邀请,我们也需要发送一些额外的信息来引导通信过程。记着在两个对等通信者之间实际通信是由WCF和对等信道架构处理的。这意味着我们需要额外的信息,比如网状网络的名字以及密码来继续通信。额外的信息与邀请过程一起发送。列表12.11显示了如何从Windows Vista的合作架构发送并接收邀请。一次邀请使用一个PeerNewMe实例的Invite或者InviteAsync方法来发送。强烈建议你使用InviteAsync方法;否则,用户接口被阻塞,等待用户接受邀请。这两个方法都有一个选项来以一个字节数组形式发送额外的数据。在我们的情况,我们把网状网络名字和密码打包都一个字节流中并一并发送出去。通过与邀请一起发送网状网络名字和密码更多考虑是信息可能暴露。不要担心!People Near Me架构通过一个加密连接传输信息。
列表12.11 发送并接收邀请
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.PeerToPeer.Collaboration;
using System.IO;namespace PeerChat
{public class InvitationHelper{private static PeerApplication PeerChatPeerApplication;private static Guid PeerChatAppId = new Guid("4BC6F59E-124E-4e75-8CD9-BB75BCA78CA8");private static string PeerChatDescription = "A sample peer networking application";static InvitationHelper(){PeerChatPeerApplication =new PeerApplication(PeerChatAppId,PeerChatDescription,null,System.Windows.Forms.Application.ExecutablePath,null,PeerScope.All);}public static PeerInvitationResponseType Invite(PeerNearMe personTo,Guid chatId,Guid meshPassword){byte[] data;using (MemoryStream ms = new MemoryStream()){using (StreamWriter sw = new StreamWriter(ms)){sw.Write(chatId.ToString());sw.WriteLine();sw.Write(meshPassword.ToString());}data = ms.ToArray();}PeerInvitationResponse response =personTo.Invite(PeerChatPeerApplication,"You are being invited to chat.", data);return response.PeerInvitationResponseType;}public static void InviteAsync(PeerNearMe personTo,Guid chatId, Guid meshPassword){byte[] data;using (MemoryStream ms = new MemoryStream()){using (StreamWriter sw = new StreamWriter(ms)){sw.Write(chatId.ToString());sw.WriteLine();sw.Write(meshPassword.ToString());}data = ms.ToArray();}object userToken = Guid.NewGuid();personTo.InviteAsync(PeerChatPeerApplication,"You are being invited to chat.", data, userToken);}public static bool IsLaunched{get{return (PeerCollaboration.ApplicationLaunchInfo != null) && (PeerCollaboration.ApplicationLaunchInfo.Data != null)}}public static Guid ChatId{get{Guid chatId;using (MemoryStream ms = new MemoryStream(PeerCollaboration.ApplicationLaunchInfo.Data)){using (StreamReader sr = new StreamReader(ms)){string chatIdString = sr.ReadLine();string meshPassword = sr.ReadLine();chatId = new Guid(chatIdString);}}return chatId;}}public static Guid MeshPassword{get{Guid meshPassword;using (MemoryStream ms = new MemoryStream(PeerCollaboration.ApplicationLaunchInfo.Data)){using (StreamReader sr = new StreamReader(ms)){string chatIdString = sr.ReadLine();string meshPasswordString = sr.ReadLine();meshPassword = new Guid(meshPasswordString);}}return meshPassword;}}}
}

最后要考虑的是如何确定一个应用程序是否基于一个来自于People Near Me的邀请运行。为了实现这个我们需要获得一个PeerApplicationLaunchInfo类的实例。这在PeerCollaboration静态类的ApplicationLaunchInfo属性中可用。列表12.11也显示了如何使用这个类来确定是否应用由于邀请被运行以及如何访问由邀请发送的额外数据。

转载于:https://www.cnblogs.com/danielWise/archive/2011/05/26/2057735.html

WCF 第十二章 对等网 System.Net.PeerToPeer.Collaboration相关推荐

  1. WCF 第十二章 对等网

    很多开发人员在创建分布式应用程序时会考虑客户端-服务端或者n-层结构模型.另外一个通常会被忽略的创建分布式应用程序的方案是点到点(P2P)模型.大多数流行的互联网应用程序中的一部分,包含即时通信,游戏 ...

  2. WCF 第十二章 对等网 使用自定义绑定实现消息定向

    当使用对等传输信道时会有一个常见的错误,就是认为它支持在一个对等网状网络间定向通信.消息定向意味着一条消息可以在一个对等网状网络中通过跨越一个网状网络将其传播到目的端(这就是路由的原理)来发送到特定节 ...

  3. css层叠样式表基础学习笔记--第十二章 我要自学网首页实战

    第十二章 我要自学网首页实战 12-01 页面分析 12-02 工作准备 12-03 搜索区块页面结构 12-04 导航条布局 12-05 幻灯片布局 12-06 公告栏布局 12-07 远程培训班布 ...

  4. 【信息系统项目管理师】第二十二章 信息系统安全管理(考点汇总篇)

    [信息系统项目管理师]第二十二章 信息系统安全管理(考点汇总篇) 考点分析与预测 信息安全为高级科目独有的章节,在第三版教材中有66页的内容.需要掌握的知识点非常多,且知识点非常散,在考试中上午一般考 ...

  5. 第十二章_网络搭建及训练

    文章目录 第十二章 网络搭建及训练 CNN训练注意事项 第十二章 TensorFlow.pytorch和caffe介绍 12.1 TensorFlow 12.1.1 TensorFlow是什么? 12 ...

  6. Linux云计算【第一阶段】第十二章:网络管理、进制及SSH管理与攻防

    第十二章:网络管理及SSH管理与攻防 [重难点] 一.网络发展概述 局域网 城域网 广域网 基本网络协议 客户端与服务器的概念 从客户端到服务器的经过 No.1 客户端与服务器的概念 客户端: 即表示 ...

  7. 鸟哥的Linux私房菜(服务器)- 第二十二章、邮件服务器: Postfix

    第二十二章.邮件服务器: Postfix 最近更新日期:2011/08/10 在这个邮件服务器的架设中,我们首先谈论 Mail 与 DNS 的重要相关性,然后依序介绍 Mail Server 的相关名 ...

  8. 第十二章-硬盘介绍和磁盘管理 随堂笔记

    第十二章-硬盘介绍和磁盘管理 本节所讲内容: 12.1 SAS-SATA-SSD-SCSI-IDE硬盘讲解 12.2 磁盘分区工具和挂载 12.3 实战扩展swap分区 12.1 SAS-SATA-S ...

  9. 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条

    http://blog.csdn.net/terryzero/article/details/3797782 疯狂JAVA讲义---第十二章:Swing编程(五)进度条和滑动条 标签: swing编程 ...

最新文章

  1. 剑指offer:面试题28. 对称的二叉树
  2. MySQL 8.0 正式版 8.0.11 发布:比 MySQL 5.7 快 2 倍
  3. 【spring boot2】第2篇:配置文件YAML语法
  4. 计算机函数select,请问关于select函数的操作程序?
  5. 华为交换机linux版本号,Cisco和华为交换机常用配置命令总结
  6. java properties 保存_Java 读写Properties配置文件
  7. 免费mysql空间_php+mysql免费空间
  8. 设置线程当天十二点执行_这份JAVA多线程笔记真的是细节满满,几乎全是你工作能用到的干货...
  9. java.sql.SQLException: Value'0000-00-00'异常解决办法
  10. VMware虚拟机与Windows文件共享
  11. Doris之Rollup 与查询
  12. Localdatetime的坑
  13. 【LeetCode】【字符串】题号:*657. 机器人能否返回原点
  14. 盒马销量预测核心算法的技术演进
  15. bootstrap 检验 法 原理_三种中介效应检验方法及操作步骤 - spssau
  16. Vue 使用Excel表格导入导出
  17. IT行业都有哪些职位,初学者(0基础,新人)该如何选择,才能够快速进入这个行业?... 1
  18. 笔记本电脑计计算机硬盘分区,笔记本电脑如何分区,小编教你笔记本电脑如何分区...
  19. ORB-SLAM2学习笔记——BundleAdjustment函数
  20. JS 对象直接量方法创建对象

热门文章

  1. Vue中的join(),reverse()与 split()函数
  2. 如何选择分布式事务形态
  3. Lock应用之 读写锁
  4. Android设置无title报错
  5. LNMP3.0一键安装
  6. http://zhcsmx22.blog.51cto.com
  7. 02 | 纵览全局:把握 Netty 整体架构脉络
  8. 分子模拟的理论与实践_基于分子模拟的数据驱动发现流体力学宏观方程
  9. https://akaedu.github.io/book/ch32s02.html
  10. java 多线程 关键字_Java多线程常用的几个关键字