15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Building P2P applications ❘ 1349<br />

Event h<strong>and</strong>lers for the ResolveProgressChanged event use the ResolveProgressChangedEventArgs<br />

event arguments parameter, which derives from the st<strong>and</strong>ard System.ComponentModel.<br />

ProgressChangedEventArgs class. You can use the PeerNameRecord property of the event argument<br />

object you receive in the event h<strong>and</strong>ler to get a reference to the peer name record that was found.<br />

Similarly, the ResolveCompleted event requires an event h<strong>and</strong>ler that uses a parameter of type ResolveComplete<br />

dEventArgs, which derives from AsyncCompletedEventArgs. This type includes a PeerNameRecordCollection<br />

parameter you can use to obtain a complete list of the peer name records that were found.<br />

The following code shows an implementation of event h<strong>and</strong>lers for these events:<br />

private pnres_ResolveProgressChanged(object sender,<br />

ResolveProgressChangedEventArgs e)<br />

{<br />

// Use e.ProgressPercentage (inherited from base event args)<br />

// Process PeerNameRecord from e.PeerNameRecord<br />

}<br />

private pnres_ResolveCompleted(object sender,<br />

ResolveCompletedEventArgs e)<br />

{<br />

// Test for e.IsCancelled <strong>and</strong> e.Error (inherited from base event args)<br />

// Process PeerNameRecordCollection from e.PeerNameRecordCollection<br />

}<br />

After you have one or more PeerNameRecord objects you can proceed to process them. This<br />

PeerNameRecord class exposes Comment <strong>and</strong> Data properties to examine the comment <strong>and</strong> data set in the<br />

peer name registration (if any), a PeerName property to get the PeerName object for the peer name record,<br />

<strong>and</strong>, most importantly, an EndPointCollection property. As with PeerNameRegistration, this property<br />

is a System.Net.IPEndPointCollection collection of System.Net.IPEndPoint objects. You can use<br />

these objects to connect to endpoints exposed by the peer in any way you want.<br />

Code access security in system.net.PeerToPeer<br />

The System.Net.PeerToPeer namespace also includes the following two classes that you can use with<br />

Code Access Security (CAS). See Chapter 21, “Security” for more details.<br />

➤<br />

➤<br />

PnrpPermission, which inherits from CodeAccessPermission<br />

PnrpPermissionAttribute, which inherits from CodeAccessSecurityAttribute<br />

You can use these classes to provide permissions functionality for PNRP access in the usual CAS way.<br />

sample application<br />

The downloadable code for this chapter includes a sample P2P application (P2PSample) that uses the<br />

concepts <strong>and</strong> namespace introduced in this section. It is a WPF application that uses a WCF service for a<br />

peer endpoint.<br />

The application is configured with an application configuration file, in which you can specify the name of<br />

the peer <strong>and</strong> a port to listen on as follows:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet App.config<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!