Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#

Peer Graph - Peers and Connections

Rate me:
Please Sign up or sign in to vote.
3.19/5 (7 votes)
17 Nov 20055 min read 51.2K   1K   20  
Peer Graph - Peers and Connections using Microsoft's Peer-to-Peer technology.
using System;
using System.Runtime.InteropServices;
using System.Collections;

namespace Peer.Graph
{
	public class PeerCollection : IEnumerable	
	{
		private PeerEnumerator Peers;

		internal PeerCollection(IntPtr hGraph, string PeerName)
		{
			Peers = new PeerEnumerator(hGraph, PeerName);
		}

		internal PeerCollection(IntPtr hGraph)
		{
			Peers = new PeerEnumerator(hGraph, string.Empty);
		}

		private class PeerEnumerator : IEnumerator
		{
			private string peerName;
			private IntPtr hGraph;
			private IntPtr hPeerEnum;
			private int index;
			private int count;

			public PeerEnumerator(IntPtr Graph, string PeerName)
			{
				hGraph = Graph;
				peerName = PeerName;
				Reset();
			}

			#region IEnumerator Members

			public void Reset()
			{
				uint hr;
				if (hPeerEnum != IntPtr.Zero)
				{
					hr = PeerGraphNative.PeerGraphEndEnumeration(hPeerEnum);
					if (hr != 0) throw new PeerGraphException(hr);
					hPeerEnum = IntPtr.Zero;
				}

				IntPtr nameptr = IntPtr.Zero;
				if (peerName != string.Empty) nameptr = Marshal.StringToHGlobalUni(peerName);
				hr = PeerGraphNative.PeerGraphEnumNodes(hGraph, nameptr, out hPeerEnum);
				if (hr != 0) throw new PeerGraphException(hr);

				uint count;
				hr = PeerGraphNative.PeerGraphGetItemCount(hPeerEnum, out count);
				if (hr != 0) throw new PeerGraphException(hr);

				this.count = (int)count;
				index = -1;
			}

			public object Current
			{
				get
				{
					uint ulItem = 1;
					IntPtr Peerptr;
					uint hr = PeerGraphNative.PeerGraphGetNextItem(hPeerEnum, ref ulItem, out Peerptr);
					if (hr != 0) throw new PeerGraphException(hr);

					IntPtr itemptr = Marshal.ReadIntPtr(Peerptr);
					PeerNode peer = new PeerNode((PEER_NODE_INFO)Marshal.PtrToStructure(itemptr, typeof(PEER_NODE_INFO)));

					PeerGraphNative.PeerGraphFreeData(Peerptr);
					return peer;
				}
			}

			public bool MoveNext()
			{
				if (index < count) index++;
				return index == count ? false : true;
			}

			#endregion
		}

		public IEnumerator GetEnumerator()
		{
			return Peers;
		}

		IEnumerator IEnumerable.GetEnumerator() 
		{
			return GetEnumerator();
		}
	}


	public class PeerConnectionCollection : IEnumerable	
	{
		private ConnectionEnumerator connections;

		internal PeerConnectionCollection(IntPtr hGraph, PeerConnectionType Type)
		{
			connections = new ConnectionEnumerator(hGraph, Type);
		}

		internal PeerConnectionCollection(IntPtr hGraph)
		{
			connections = new ConnectionEnumerator(hGraph, PeerConnectionType.All);
		}

		private class ConnectionEnumerator : IEnumerator
		{
			private PeerConnectionType type;
			private IntPtr hGraph;
			private IntPtr hPeerEnum;
			private int index;
			private int count;

			public ConnectionEnumerator(IntPtr Graph, PeerConnectionType Type)
			{
				hGraph = Graph;
				type = Type;
				Reset();
			}

			#region IEnumerator Members

			public void Reset()
			{
				uint hr;
				if (hPeerEnum != IntPtr.Zero)
				{
					hr = PeerGraphNative.PeerGraphEndEnumeration(hPeerEnum);
					if (hr != 0) throw new PeerGraphException(hr);
					hPeerEnum = IntPtr.Zero;
				}

				hr = PeerGraphNative.PeerGraphEnumConnections(hGraph, type, out hPeerEnum);
				if (hr != 0) throw new PeerGraphException(hr);

				uint count;
				hr = PeerGraphNative.PeerGraphGetItemCount(hPeerEnum, out count);
				if (hr != 0) throw new PeerGraphException(hr);

				this.count = (int)count;
				index = -1;
			}

			public object Current
			{
				get
				{
					uint ulItem = 1;
					IntPtr connectionptr;
					uint hr = PeerGraphNative.PeerGraphGetNextItem(hPeerEnum, ref ulItem, out connectionptr);
					if (hr != 0) throw new PeerGraphException(hr);

					IntPtr itemptr = Marshal.ReadIntPtr(connectionptr);
					PeerConnection connection = new PeerConnection((PEER_CONNECTION_INFO)Marshal.PtrToStructure(itemptr, typeof(PEER_CONNECTION_INFO)));

					PeerGraphNative.PeerGraphFreeData(connectionptr);
					return connection;
				}
			}

			public bool MoveNext()
			{
				if (index < count) index++;
				return index == count ? false : true;
			}

			#endregion
		}

		public IEnumerator GetEnumerator()
		{
			return connections;
		}

		IEnumerator IEnumerable.GetEnumerator() 
		{
			return GetEnumerator();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Adrian Moore is the Development Manager for the SCADA Vision system developed by ABB Inc in Calgary, Alberta.

He has been interested in compilers, parsers, real-time database systems and peer-to-peer solutions since the early 90's. In his spare time, he is currently working on a SQL parser for querying .NET DataSets (http://www.queryadataset.com).

Adrian is a Microsoft MVP for Windows Networking.

Comments and Discussions