internal static PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet MarshalWsaQuerySetNativeToWsaQuerySet(IntPtr pNativeData, uint scopeId)
 {
     if (pNativeData == IntPtr.Zero)
     {
         return null;
     }
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet set = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet();
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetNative native = (PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetNative) Marshal.PtrToStructure(pNativeData, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetNative));
     int num = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfoNative));
     set.Context = Marshal.PtrToStringUni(native.lpszContext);
     set.NameSpace = native.dwNameSpace;
     set.ServiceInstanceName = Marshal.PtrToStringUni(native.lpszServiceInstanceName);
     set.Comment = Marshal.PtrToStringUni(native.lpszComment);
     set.CsAddrInfos = new PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfo[native.dwNumberOfCsAddrs];
     for (int i = 0; i < native.dwNumberOfCsAddrs; i++)
     {
         IntPtr ptr = (IntPtr) (native.lpcsaBuffer.ToInt64() + (i * num));
         PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfoNative native2 = (PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfoNative) Marshal.PtrToStructure(ptr, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfoNative));
         set.CsAddrInfos[i].iProtocol = native2.iProtocol;
         set.CsAddrInfos[i].iSocketType = native2.iSocketType;
         set.CsAddrInfos[i].LocalAddr = IPEndPointFromSocketAddress(native2.LocalAddr, scopeId);
         set.CsAddrInfos[i].RemoteAddr = IPEndPointFromSocketAddress(native2.RemoteAddr, scopeId);
     }
     if (native.lpNSProviderId != IntPtr.Zero)
     {
         set.NSProviderId = (Guid) Marshal.PtrToStructure(native.lpNSProviderId, typeof(Guid));
     }
     if (native.lpServiceClassId != IntPtr.Zero)
     {
         set.ServiceClassId = (Guid) Marshal.PtrToStructure(native.lpServiceClassId, typeof(Guid));
     }
     if (set.NameSpace == PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Cloud)
     {
         if (native.lpBlob != IntPtr.Zero)
         {
             set.Blob = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo();
             PnrpPeerResolver.UnsafePnrpNativeMethods.BlobNative native3 = (PnrpPeerResolver.UnsafePnrpNativeMethods.BlobNative) Marshal.PtrToStructure(native.lpBlob, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.BlobNative));
             if (native3.pBlobData != IntPtr.Zero)
             {
                 set.Blob = (PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo) Marshal.PtrToStructure(native3.pBlobData, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo));
             }
         }
         return set;
     }
     if ((set.NameSpace == PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Name) && (native.lpBlob != IntPtr.Zero))
     {
         set.Blob = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo();
         PnrpPeerResolver.UnsafePnrpNativeMethods.BlobSafe safe = (PnrpPeerResolver.UnsafePnrpNativeMethods.BlobSafe) Marshal.PtrToStructure(native.lpBlob, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.BlobSafe));
         if (safe.pBlobData != IntPtr.Zero)
         {
             PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo info = (PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo) Marshal.PtrToStructure((IntPtr) safe.pBlobData, typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo));
             set.Blob = info;
         }
     }
     return set;
 }
                public void SyncEnumeration(object state)
                {
                    PnrpPeerResolver.UnsafePnrpNativeMethods.CriticalLookupHandle handle;
                    int num = 0;
                    PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetSafe safeQuerySet = PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet.ToWsaQuerySetSafe(this.resolveQuery);
                    using (safeQuerySet)
                    {
                        num = PnrpPeerResolver.UnsafePnrpNativeMethods.WSALookupServiceBegin(PnrpPeerResolver.UnsafePnrpNativeMethods.CriticalAllocHandleWsaQuerySetSafe.FromWsaQuerySetSafe(safeQuerySet), PnrpPeerResolver.UnsafePnrpNativeMethods.WsaNspControlFlags.ReturnAll, out handle);
                    }
                    if (num != 0)
                    {
                        this.lastException = new PnrpPeerResolver.PnrpException(PnrpPeerResolver.UnsafePnrpNativeMethods.WSAGetLastError(), this.resolveQuery.Context);
                        Utility.CloseInvalidOutCriticalHandle(handle);
                        base.Complete(false, this.lastException);
                    }
                    else
                    {
                        PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet set = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet();
                        int size = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetSafe)) + 400;
                        CriticalAllocHandle handle3 = CriticalAllocHandle.FromSize(size);
                        try
                        {
                            using (handle)
                            {
                            Label_0090:
                                if (this.timeoutHelper.RemainingTime() != TimeSpan.Zero)
                                {
                                    if (PnrpPeerResolver.UnsafePnrpNativeMethods.WSALookupServiceNext(handle, 0, ref size, (IntPtr) handle3) != 0)
                                    {
                                        int errorCode = PnrpPeerResolver.UnsafePnrpNativeMethods.WSAGetLastError();
                                        switch (errorCode)
                                        {
                                            case 0x2776:
                                            case 0x277e:
                                                return;

                                            case 0x271e:
                                                handle3 = CriticalAllocHandle.FromSize(size);
                                                goto Label_0090;
                                        }
                                        PeerExceptionHelper.ThrowPnrpError(errorCode, set.Context);
                                        goto Label_0090;
                                    }
                                    if (handle3 == IntPtr.Zero)
                                    {
                                        goto Label_0090;
                                    }
                                    set = MarshalWsaQuerySetNativeToWsaQuerySet((IntPtr) handle3, this.scopeId);
                                    PnrpPeerResolver.PnrpRegistration item = new PnrpPeerResolver.PnrpRegistration {
                                        CloudName = set.Context,
                                        Comment = set.Comment,
                                        PeerName = set.ServiceInstanceName,
                                        Addresses = new IPEndPoint[set.CsAddrInfos.Length]
                                    };
                                    for (int i = 0; i < set.CsAddrInfos.Length; i++)
                                    {
                                        item.Addresses[i] = set.CsAddrInfos[i].LocalAddr;
                                    }
                                    lock (this.results)
                                    {
                                        this.results.Add(item);
                                        goto Label_0090;
                                    }
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            if (Fx.IsFatal(exception))
                            {
                                throw;
                            }
                            DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                            if (DiagnosticUtility.ShouldTraceInformation)
                            {
                                PnrpResolveExceptionTraceRecord extendedData = new PnrpResolveExceptionTraceRecord(this.resolveQuery.ServiceInstanceName, this.resolveQuery.Context, exception);
                                if (DiagnosticUtility.ShouldTraceError)
                                {
                                    TraceUtility.TraceEvent(TraceEventType.Error, 0x4004b, System.ServiceModel.SR.GetString("TraceCodePnrpResolveException"), extendedData, this, null);
                                }
                            }
                            this.lastException = exception;
                        }
                        finally
                        {
                            base.Complete(false, this.lastException);
                        }
                    }
                }
 public PeerNameResolver(string peerName, int numberOfResultsRequested, PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpResolveCriteria resolveCriteria, uint scopeId, string cloudName, TimeSpan timeout, List<PnrpPeerResolver.PnrpRegistration> results) : base(null, null)
 {
     if (timeout > PnrpPeerResolver.MaxTimeout)
     {
         timeout = PnrpPeerResolver.MaxTimeout;
     }
     this.timeoutHelper = new TimeoutHelper(timeout);
     PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo info = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo {
         dwSize = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo)),
         nMaxResolve = numberOfResultsRequested,
         dwTimeout = (int) timeout.TotalSeconds,
         dwLifetime = 0,
         enNameState = (PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpRegisteredIdState) 0,
         lpwszIdentity = null,
         dwFlags = 1
     };
     IPEndPoint hint = PnrpPeerResolver.GetHint();
     info.enResolveCriteria = resolveCriteria;
     info.saHint = PnrpPeerResolver.UnsafePnrpNativeMethods.SOCKET_ADDRESS_SAFE.SocketAddressFromIPEndPoint(hint);
     this.resolveQuery = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet();
     this.resolveQuery.ServiceInstanceName = peerName;
     this.resolveQuery.ServiceClassId = PnrpPeerResolver.UnsafePnrpNativeMethods.SvcIdNameV1;
     this.resolveQuery.NameSpace = PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Name;
     this.resolveQuery.NSProviderId = PnrpPeerResolver.UnsafePnrpNativeMethods.NsProviderName;
     this.resolveQuery.Context = cloudName;
     this.resolveQuery.Blob = info;
     this.results = results;
     this.scopeId = scopeId;
     ActionItem.Schedule(new Action<object>(this.SyncEnumeration), null);
 }
 public void Unregister(string peerName, string cloudName, TimeSpan timeout)
 {
     PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo info = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo {
         lpwszIdentity = null,
         dwSize = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo))
     };
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet registerQuery = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet {
         NameSpace = PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Name,
         NSProviderId = PnrpPeerResolver.UnsafePnrpNativeMethods.NsProviderName,
         ServiceClassId = PnrpPeerResolver.UnsafePnrpNativeMethods.SvcIdNameV1,
         ServiceInstanceName = peerName,
         Context = cloudName,
         Blob = info
     };
     this.DeleteService(registerQuery);
 }
 public void Register(PnrpPeerResolver.PnrpRegistration registration, TimeSpan timeout)
 {
     PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo info;
     info = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpInfo {
         dwLifetime = 0xe10,
         lpwszIdentity = null,
         dwSize = Marshal.SizeOf(info),
         dwFlags = 1
     };
     IPEndPoint hint = PnrpPeerResolver.GetHint();
     info.saHint = PnrpPeerResolver.UnsafePnrpNativeMethods.SOCKET_ADDRESS_SAFE.SocketAddressFromIPEndPoint(hint);
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet registerQuery = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet {
         NameSpace = PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Name,
         NSProviderId = PnrpPeerResolver.UnsafePnrpNativeMethods.NsProviderName,
         ServiceClassId = PnrpPeerResolver.UnsafePnrpNativeMethods.SvcIdNameV1,
         ServiceInstanceName = registration.PeerName,
         Comment = registration.Comment,
         Context = registration.CloudName
     };
     if (registration.Addresses != null)
     {
         registerQuery.CsAddrInfos = new PnrpPeerResolver.UnsafePnrpNativeMethods.CsAddrInfo[registration.Addresses.Length];
         for (int i = 0; i < registration.Addresses.Length; i++)
         {
             registerQuery.CsAddrInfos[i].LocalAddr = registration.Addresses[i];
             registerQuery.CsAddrInfos[i].iProtocol = 6;
             registerQuery.CsAddrInfos[i].iSocketType = 1;
         }
     }
     registerQuery.Blob = info;
     this.RegisterService(registerQuery);
 }
 public static PnrpPeerResolver.UnsafePnrpNativeMethods.CloudInfo[] GetClouds()
 {
     PnrpPeerResolver.UnsafePnrpNativeMethods.CriticalLookupHandle handle;
     int num = 0;
     ArrayList list = new ArrayList();
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet input = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet();
     PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo info = new PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo {
         dwSize = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo))
     };
     info.Cloud.Scope = PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpScope.Any;
     info.dwCloudState = PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudState.Virtual;
     info.Flags = PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudFlags.None;
     input.NameSpace = PnrpPeerResolver.UnsafePnrpNativeMethods.NspNamespaces.Cloud;
     input.NSProviderId = PnrpPeerResolver.UnsafePnrpNativeMethods.NsProviderCloud;
     input.ServiceClassId = PnrpPeerResolver.UnsafePnrpNativeMethods.SvcIdCloud;
     input.Blob = info;
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetSafe safeQuerySet = PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet.ToWsaQuerySetSafe(input);
     using (safeQuerySet)
     {
         num = PnrpPeerResolver.UnsafePnrpNativeMethods.WSALookupServiceBegin(PnrpPeerResolver.UnsafePnrpNativeMethods.CriticalAllocHandleWsaQuerySetSafe.FromWsaQuerySetSafe(safeQuerySet), PnrpPeerResolver.UnsafePnrpNativeMethods.WsaNspControlFlags.ReturnAll, out handle);
     }
     if (num != 0)
     {
         SocketException exception = new SocketException(PnrpPeerResolver.UnsafePnrpNativeMethods.WSAGetLastError());
         Utility.CloseInvalidOutCriticalHandle(handle);
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
     }
     int size = Marshal.SizeOf(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetSafe)) + 200;
     CriticalAllocHandle handle3 = CriticalAllocHandle.FromSize(size);
     using (handle)
     {
         while (true)
         {
             while (PnrpPeerResolver.UnsafePnrpNativeMethods.WSALookupServiceNext(handle, 0, ref size, (IntPtr) handle3) != 0)
             {
                 int errorCode = PnrpPeerResolver.UnsafePnrpNativeMethods.WSAGetLastError();
                 switch (errorCode)
                 {
                     case 0x2776:
                     case 0x277e:
                         goto Label_01F5;
                 }
                 if (errorCode != 0x271e)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SocketException(errorCode));
                 }
                 if (handle3 != null)
                 {
                     handle3.Dispose();
                     handle3 = null;
                 }
                 handle3 = CriticalAllocHandle.FromSize(size);
             }
             if (handle3 != IntPtr.Zero)
             {
                 PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet set2 = PnrpPeerResolver.UnsafePnrpNativeMethods.PeerNameResolver.MarshalWsaQuerySetNativeToWsaQuerySet((IntPtr) handle3, 0);
                 PnrpPeerResolver.UnsafePnrpNativeMethods.CloudInfo info2 = new PnrpPeerResolver.UnsafePnrpNativeMethods.CloudInfo();
                 PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo blob = (PnrpPeerResolver.UnsafePnrpNativeMethods.PnrpCloudInfo) set2.Blob;
                 info2.Name = set2.ServiceInstanceName;
                 info2.Scope = blob.Cloud.Scope;
                 info2.ScopeId = blob.Cloud.ScopeId;
                 info2.State = blob.dwCloudState;
                 info2.Flags = blob.Flags;
                 list.Add(info2);
             }
         }
     }
 Label_01F5:
     return (PnrpPeerResolver.UnsafePnrpNativeMethods.CloudInfo[]) list.ToArray(typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.CloudInfo));
 }
 public bool IsPnrpAvailable(TimeSpan waitForService)
 {
     if (!this.IsPnrpInstalled())
     {
         return false;
     }
     if (!this.IsPnrpServiceRunning(waitForService))
     {
         return false;
     }
     PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet registerQuery = new PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet {
         NSProviderId = PnrpPeerResolver.UnsafePnrpNativeMethods.NsProviderName,
         ServiceClassId = PnrpPeerResolver.UnsafePnrpNativeMethods.SvcIdNameV1
     };
     int num = this.InvokeService(registerQuery, PnrpPeerResolver.UnsafePnrpNativeMethods.WsaSetServiceOp.Register, 0);
     if ((num != 0x2726) && (num != 0x2afc))
     {
         return false;
     }
     return true;
 }