Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET

Peer Collaboration - Inviting

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
1 May 20067 min read 27.2K   441   28  
Inviting People Near Me using Microsoft's Peer-to-Peer Collaboration technology in Windows Vista.
using System;
using System.Runtime.InteropServices;
using System.Collections;

namespace Peer.Collaboration
{
	public class PeopleNearMeCollection : IEnumerable	
	{
		private PeopleNearMeEnumerator apps;

		internal PeopleNearMeCollection()
		{
			apps = new PeopleNearMeEnumerator();
		}

		private class PeopleNearMeEnumerator : IEnumerator
		{
			private IntPtr hPeerEnum;
			private int index;
			private int count;

			public PeopleNearMeEnumerator()
			{
				Reset();
			}

			#region IEnumerator Members

			public void Reset()
			{
				uint hr;
				if (hPeerEnum != IntPtr.Zero)
				{
					hr = PeerCollabNative.PeerEndEnumeration(hPeerEnum);
					if (hr != 0) throw new PeerCollabException(hr);
					hPeerEnum = IntPtr.Zero;
				}

				hr = PeerCollabNative.PeerCollabEnumPeopleNearMe(out hPeerEnum);
				if (hr != 0) throw new PeerCollabException(hr);

				uint count;
				hr = PeerCollabNative.PeerGetItemCount(hPeerEnum, out count);
				if (hr != 0) throw new PeerCollabException(hr);

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

			public object Current
			{
				get
				{
					uint ulItem = 1;
					IntPtr ptr;
					uint hr = PeerCollabNative.PeerGetNextItem(hPeerEnum, ref ulItem, out ptr);
					if (hr != 0) throw new PeerCollabException(hr);

					IntPtr itemptr = Marshal.ReadIntPtr(ptr);
					PeerPeopleNearMe pnm = new PeerPeopleNearMe((PEER_PEOPLE_NEAR_ME)Marshal.PtrToStructure(itemptr, typeof(PEER_PEOPLE_NEAR_ME)));

					PeerCollabNative.PeerFreeData(ptr);
					return pnm;
				}
			}

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

			#endregion
		}

		public IEnumerator GetEnumerator()
		{
			return apps;
		}

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

    public class PeerApplicationCollection : IEnumerable
    {
        private ApplicationEnumerator apps;

        internal PeerApplicationCollection(PEER_ENDPOINT EndPoint)
        {
            apps = new ApplicationEnumerator(EndPoint);
        }

        private class ApplicationEnumerator : IEnumerator
        {
            private IntPtr hPeerEnum;
            private int index;
            private int count;
            private PEER_ENDPOINT endpoint;

            public ApplicationEnumerator(PEER_ENDPOINT EndPoint)
            {
                endpoint = EndPoint;
                Reset();
            }

            #region IEnumerator Members

            public void Reset()
            {
                uint hr;
                if (hPeerEnum != IntPtr.Zero)
                {
                    hr = PeerCollabNative.PeerEndEnumeration(hPeerEnum);
                    if (hr != 0) throw new PeerCollabException(hr);
                    hPeerEnum = IntPtr.Zero;
                }

                hr = PeerCollabNative.PeerCollabEnumApplications(ref endpoint, IntPtr.Zero, out hPeerEnum);
                if (hr != 0) throw new PeerCollabException(hr);

                uint count;
                hr = PeerCollabNative.PeerGetItemCount(hPeerEnum, out count);
                if (hr != 0) throw new PeerCollabException(hr);

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

            public object Current
            {
                get
                {
                    uint ulItem = 1;
                    IntPtr ptr;
                    uint hr = PeerCollabNative.PeerGetNextItem(hPeerEnum, ref ulItem, out ptr);
                    if (hr != 0) throw new PeerCollabException(hr);

                    IntPtr itemptr = Marshal.ReadIntPtr(ptr);
                    PeerApplication app = new PeerApplication((PEER_APPLICATION)Marshal.PtrToStructure(itemptr, typeof(PEER_APPLICATION)));

                    PeerCollabNative.PeerFreeData(ptr);
                    return app;
                }
            }

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

            #endregion
        }

        public IEnumerator GetEnumerator()
        {
            return apps;
        }

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

    public class PeerApplicationRegistrationCollection : IEnumerable
    {
        private ApplicationRegistrationInfoEnumerator apps;

        internal PeerApplicationRegistrationCollection(PeerApplicationRegistrationType RegistrationType)
        {
            apps = new ApplicationRegistrationInfoEnumerator(RegistrationType);
        }

        private class ApplicationRegistrationInfoEnumerator : IEnumerator
        {
            private IntPtr hPeerEnum;
            private int index;
            private int count;
            internal PeerApplicationRegistrationType registrationType;

            public ApplicationRegistrationInfoEnumerator(PeerApplicationRegistrationType RegistrationType)
            {
                registrationType = RegistrationType;
                Reset();
            }

            #region IEnumerator Members

            public void Reset()
            {
                uint hr;
                if (hPeerEnum != IntPtr.Zero)
                {
                    hr = PeerCollabNative.PeerEndEnumeration(hPeerEnum);
                    if (hr != 0) throw new PeerCollabException(hr);
                    hPeerEnum = IntPtr.Zero;
                }

                hr = PeerCollabNative.PeerCollabEnumApplicationRegistrationInfo(registrationType, out hPeerEnum);
                if (hr != 0) throw new PeerCollabException(hr);

                uint count;
                hr = PeerCollabNative.PeerGetItemCount(hPeerEnum, out count);
                if (hr != 0) throw new PeerCollabException(hr);

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

            public object Current
            {
                get
                {
                    uint ulItem = 1;
                    IntPtr ptr;
                    uint hr = PeerCollabNative.PeerGetNextItem(hPeerEnum, ref ulItem, out ptr);
                    if (hr != 0) throw new PeerCollabException(hr);

                    IntPtr itemptr = Marshal.ReadIntPtr(ptr);
                    PeerApplicationRegistration appinfo = new PeerApplicationRegistration((PEER_APPLICATION_REGISTRATION_INFO)Marshal.PtrToStructure(itemptr, typeof(PEER_APPLICATION_REGISTRATION_INFO)));

                    PeerCollabNative.PeerFreeData(ptr);
                    return appinfo;
                }
            }

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

            #endregion
        }

        public IEnumerator GetEnumerator()
        {
            return apps;
        }

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

        public void Register(PeerApplicationRegistration Application)
        {
            uint hr = PeerCollabNative.PeerCollabRegisterApplication(ref Application.info, apps.registrationType);
            if (hr != 0) throw new PeerCollabException(hr);
        }

        public void Unregister(PeerApplicationRegistration Application)
        {
            Guid id = Application.ID;
            uint hr = PeerCollabNative.PeerCollabUnregisterApplication(ref id, apps.registrationType);
            if (hr != 0) throw new PeerCollabException(hr);
        }
    }
}

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