gpt4 book ai didi

c# - WCF 点对点,那里有节点吗?

转载 作者:太空狗 更新时间:2023-10-29 21:42:25 27 4
gpt4 key购买 nike

我在 .NET 3.5 中使用 WCF 来实现对等网络应用程序。我使用 PNRP 解析对等节点。

IGlobalStoreServiceContract 是我的契约(Contract),如下所示,

[ServiceContract(Namespace = "http://GlobalStoreEventDriven.API", CallbackContract = typeof(IGlobalStoreServiceContract))]
internal interface IGlobalStoreServiceContract
{
[OperationContract(IsOneWay = true)]
void NotifyGlobalStoreDataInserted(string globalGroup, DateTime maxDateTime);

[OperationContract(IsOneWay = true)]
void RegisterNode();

[OperationContract(IsOneWay = true)]
void SynchronizeMemberList(Guid clientId);
}

我正在使用这样的代码将每个节点加入对等网络。

DuplexChannelFactory<IGlobalStoreChannel> channelFactory = new DuplexChannelFactory<IGlobalStoreChannel>(instance, "GlobalStoreAPIEndPoint");
IGlobalStoreChannel globalStoreChannel = channelFactory.CreateChannel();

globalStoreChannel.Open();

我的问题是,一旦我打开 channel ,我如何才能最好地判断网络上是否有其他对等节点?

例如,我可以调用我的合约 RegisterNode 中的方法之一,网络中的每个节点都可以使用回调来调用 SynchronizeMemberList。然后我会知道是否有其他节点。

问题在于它都是异步的。如果我调用 RegisterNode 并且没有人回复,这并不意味着真的没有人在那里,这可能只是意味着我没有等待足够长的时间。

你怎么看?有什么建议吗?

最佳答案

参见 Peer-to-Peer Programming with WCF and .NET Framework 3.5: Peer Name作者:Amit Bahree 和 Chris Peiris:

The final logical step after creating and publishing a peer is resolving a peer. What good is it to publish something to the cloud if another peer cannot find you? We use the PeerNameResolver class to resolve for a specific peer in a given cloud. The PeerNameResolver can resolve a peer to either a PeerRecord or a cloud, depending on the parameters that are passed. The resolution process finishes either when the maximum number of record entries for the PeerRecordCollection is reached or when it has reached the end of various clouds.

The PeerNameResolver class exposes an overloaded method that is called Resolve and is used to resolve a given peer synchronously.

Listing 17 shows us how to try to resolve for a peer that is called MySecurePeer. The Resolve method returns a collection of type PeerNameRecordCollection through which we iterate. Listing 18 shows the result of this when running on a computer that has three network cards.

PeerName myPeer = new PeerName("MySecurePeer", PeerNameType.Secured);
PeerNameResolver resolver = new PeerNameResolver();

PeerNameRecordCollection results = resolver.Resolve(myPeer);

Console.WriteLine("{0} Peers Found:", results.Count.ToString());
int i = 1;

foreach (PeerNameRecord peer in results)
{
Console.WriteLine("{0} Peer:{1}", i++, peer.PeerName.ToString());
foreach (IPEndPoint ip in peer.EndPointCollection)
{
Console.WriteLine("\t Endpoint: {0}", ip.ToString());
}
}

所以,我想你应该看看 PeerNameResolver.Resolve Method :

This method associates peer names to clouds. Calling the PeerNameResolver method is similar to calling the Resolve method for each peer name record in the PeerNameRecordCollection. Note that using the Resolve method on an individual peer name record does not invalidate resolving multiple peer names.

For every Resolve method, there is an equivalent ResolveAsync method. They are identical in the parameters they are passed, except that the ResolveAsync method includes a system token in its parameter list for asynchronous event handling.

关于c# - WCF 点对点,那里有节点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1085132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com