protected override bool ReleaseHandle()
 {
     UnsafeCollabNativeMethods.PeerCollabUnregisterEvent(handle);
     SetHandleAsInvalid();   //Mark it closed - This does not change the value of the handle it self
     SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
     return(true);
 }
Beispiel #2
0
        internal static void InternalRefreshData(PeerEndPoint peerEndPoint)
        {
            int           errorCode;
            PEER_ENDPOINT pep = new PEER_ENDPOINT();

            pep.peerAddress = CollaborationHelperFunctions.ConvertIPEndpointToPEER_ADDRESS(peerEndPoint.EndPoint);

            GCHandle pepName = GCHandle.Alloc(peerEndPoint.Name, GCHandleType.Pinned);

            pep.pwzEndpointName = pepName.AddrOfPinnedObject();

            GCHandle peerEP    = GCHandle.Alloc(pep, GCHandleType.Pinned);
            IntPtr   ptrPeerEP = peerEP.AddrOfPinnedObject();

            try{
                errorCode = UnsafeCollabNativeMethods.PeerCollabRefreshEndpointData(ptrPeerEP);
            }
            finally{
                if (pepName.IsAllocated)
                {
                    pepName.Free();
                }
                if (peerEP.IsAllocated)
                {
                    peerEP.Free();
                }
            }

            if (errorCode != 0)
            {
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRefreshEndpointData returned with errorcode {0}", errorCode);
                throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_RefreshDataFailed), errorCode);
            }
        }
 protected override bool ReleaseHandle()
 {
     if (!IsInvalid)
     {
         UnsafeCollabNativeMethods.PeerEndEnumeration(handle);
     }
     SetHandleAsInvalid();   //Mark it closed - This does not change the value of the handle it self
     SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
     return(true);
 }
Beispiel #4
0
        internal static void AddMyObjectChanged(EventHandler <ObjectChangedEventArgs> callback,
                                                ref EventHandler <ObjectChangedEventArgs> objectChanged,
                                                object lockObjChangedEvent,
                                                ref RegisteredWaitHandle regObjChangedWaitHandle,
                                                ref AutoResetEvent objChangedEvent,
                                                ref SafeCollabEvent safeObjChangedEvent,
                                                WaitOrTimerCallback ObjectChangedCallback)
        {
            //
            // Register a wait handle if one has not been registered already
            //
            lock (lockObjChangedEvent){
                if (objectChanged == null)
                {
                    objChangedEvent = new AutoResetEvent(false);

                    //
                    // Register callback with a wait handle
                    //

                    regObjChangedWaitHandle = ThreadPool.RegisterWaitForSingleObject(objChangedEvent,       //Event that triggers the callback
                                                                                     ObjectChangedCallback, //callback to be called
                                                                                     null,                  //state to be passed
                                                                                     -1,                    //Timeout - aplicable only for timers
                                                                                     false                  //call us everytime the event is set
                                                                                     );

                    PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();
                    pcer.eventType = PeerCollabEventType.MyObjectChanged;
                    pcer.pInstance = IntPtr.Zero;


                    //
                    // Register event with collab
                    //

                    int errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                        objChangedEvent.SafeWaitHandle,
                        1,
                        ref pcer,
                        out safeObjChangedEvent);
                    if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ObjectChangedRegFailed), errorCode);
                    }
                }
                objectChanged += callback;
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddObjectChanged() successful.");
        }
Beispiel #5
0
        internal static PeerContact ConvertPEER_CONTACTToPeerContact(PEER_CONTACT pc, bool isMyContact)
        {
            PeerContact peerContact = (isMyContact ? new MyContact(): new PeerContact());

            peerContact.PeerName     = new PeerName(pc.pwzPeerName);
            peerContact.DisplayName  = pc.pwzDisplayName;
            peerContact.Nickname     = pc.pwzNickname;
            peerContact.EmailAddress = (pc.pwzEmailAddress != null) ? new MailAddress(pc.pwzEmailAddress) : null;
            if (!isMyContact)
            {
                peerContact.SubscribeAllowed = pc.WatcherPermissions;
            }
            peerContact.IsSubscribed = (isMyContact ? true : pc.fWatch);
            byte[] data = null;

            if (pc.credentials.cbData != 0)
            {
                data = new byte[pc.credentials.cbData];
                Marshal.Copy(pc.credentials.pbData, data, 0, (int)pc.credentials.cbData);
            }

            if (data != null)
            {
                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_PKCS7*/ 5),
                                                                                   0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                                   IntPtr.Zero,
                                                                                   0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                                                                   ref pc.credentials);

                if (certHandle == null || certHandle.IsInvalid)
                {
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }
                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    peerContact.Credentials = new X509Certificate2(certStore.Certificates[0]);
                }
                finally{
                    if (certHandle != null)
                    {
                        certHandle.Dispose();
                    }
                }
            }

            return(peerContact);
        }
Beispiel #6
0
        private static void AddPeerNearMeChanged(EventHandler <PeerNearMeChangedEventArgs> cb)
        {
            //
            // Register a wait handle if one has not been registered already
            //

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering AddPeerNearMeChanged().");

            lock (LockPNMChangedEvent){
                if (s_peerNearMeChanged == null)
                {
                    s_peerNearMeChangedEvent = new AutoResetEvent(false);

                    //
                    // Register callback with a wait handle
                    //

                    s_registeredPNMWaitHandle = ThreadPool.RegisterWaitForSingleObject(s_peerNearMeChangedEvent,                           //Event that triggers the callback
                                                                                       new WaitOrTimerCallback(PeerNearMeChangedCallback), //callback to be called
                                                                                       null,                                               //state to be passed
                                                                                       -1,                                                 //Timeout - aplicable only for timers
                                                                                       false                                               //call us everytime the event is set
                                                                                       );
                    PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();
                    pcer.eventType = PeerCollabEventType.PeopleNearMeChanged;
                    pcer.pInstance = IntPtr.Zero;

                    //
                    // Register event with collab
                    //

                    int errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                        s_peerNearMeChangedEvent.SafeWaitHandle,
                        1,
                        ref pcer,
                        out s_safePeerNearMeChangedEvent);
                    if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_PeerNearMeChangedRegFailed), errorCode);
                    }
                }
                s_peerNearMeChanged += cb;
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddPeerNearMeChanged() successful.");
        }
Beispiel #7
0
 internal static void Initialize()
 {
     if (!s_Initialized)
     {
         lock (s_LockInitialized){
             if (!s_Initialized)
             {
                 if (!PeerToPeerOSHelper.SupportsP2P)
                 {
                     throw new PlatformNotSupportedException(SR.GetString(SR.P2P_NotAvailable));
                 }
                 int errorCode = UnsafeCollabNativeMethods.PeerCollabStartup(c_CollabVersion);
                 if (errorCode != 0)
                 {
                     Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabStartup returned with errorcode {0}", errorCode);
                     throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_StartupFailed), errorCode);
                 }
                 s_Initialized = true;
             }
         }
     }
 }
Beispiel #8
0
        private void ApplicationChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "ApplicationChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                ApplicationChangedEventArgs appChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockAppChangedEvent)
                    {
                        if (m_safeAppChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safeAppChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetApplicationChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.EndPointApplicationChanged)
                    {
                        PEER_EVENT_APPLICATION_CHANGED_DATA appData = ped.applicationChangedData;
                        PEER_APPLICATION pa = (PEER_APPLICATION)Marshal.PtrToStructure(appData.pApplication, typeof(PEER_APPLICATION));

                        PeerApplication peerApplication = CollaborationHelperFunctions.ConvertPEER_APPLICATIONToPeerApplication(pa);;

                        //
                        // Check if the Guid of the fired app is indeed our guid
                        //

                        if (Guid.Equals(m_id, peerApplication.Id))
                        {
                            PeerContact  peerContact  = null;
                            PeerEndPoint peerEndPoint = null;

                            if (appData.pContact != IntPtr.Zero)
                            {
                                PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(appData.pContact, typeof(PEER_CONTACT));
                                peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc);
                            }

                            if (appData.pEndPoint != IntPtr.Zero)
                            {
                                PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(appData.pEndPoint, typeof(PEER_ENDPOINT));
                                peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                            }

                            appChangedArgs = new ApplicationChangedEventArgs(peerEndPoint,
                                                                             peerContact,
                                                                             appData.changeType,
                                                                             peerApplication);
                        }
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //

                if (appChangedArgs != null)
                {
                    OnApplicationChanged(appChangedArgs);
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving ApplicationChangedCallback().");
        }
Beispiel #9
0
        private void AddApplicationChanged(EventHandler <ApplicationChangedEventArgs> callback)
        {
            //
            // Register a wait handle if one has not been registered already
            //

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddApplicationChanged() called.");

            lock (LockAppChangedEvent){
                if (m_applicationChanged == null)
                {
                    if (m_id.Equals(Guid.Empty))
                    {
                        throw new PeerToPeerException("No application guid defined");
                    }

                    m_appChangedEvent = new AutoResetEvent(false);

                    //
                    // Register callback with a wait handle
                    //

                    m_regAppChangedWaitHandle = ThreadPool.RegisterWaitForSingleObject(m_appChangedEvent,                                   //Event that triggers the callback
                                                                                       new WaitOrTimerCallback(ApplicationChangedCallback), //callback to be called
                                                                                       null,                                                //state to be passed
                                                                                       -1,                                                  //Timeout - aplicable only for timers
                                                                                       false                                                //call us everytime the event is set
                                                                                       );
                    PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();
                    pcer.eventType = PeerCollabEventType.EndPointApplicationChanged;

                    GUID     guid       = CollaborationHelperFunctions.ConvertGuidToGUID(m_id);
                    GCHandle guidHandle = GCHandle.Alloc(guid, GCHandleType.Pinned);

                    pcer.pInstance = guidHandle.AddrOfPinnedObject();

                    //
                    // Register event with collab
                    //

                    try{
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Registering event with App ID {0}", m_id.ToString());

                        int errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                            m_appChangedEvent.SafeWaitHandle,
                            1,
                            ref pcer,
                            out m_safeAppChangedEvent);
                        if (errorCode != 0)
                        {
                            Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                            throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ApplicationChangedRegFailed), errorCode);
                        }
                    }
                    finally{
                        if (guidHandle.IsAllocated)
                        {
                            guidHandle.Free();
                        }
                    }
                }
                m_applicationChanged += callback;
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddApplicationChanged() successful.");
        }
        internal override void ObjectChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "ObjectChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                ObjectChangedEventArgs objChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockObjChangedEvent){
                        if (m_safeObjChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safeObjChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetObjectChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.MyObjectChanged)
                    {
                        PEER_EVENT_OBJECT_CHANGED_DATA objData = ped.objectChangedData;

                        PEER_OBJECT po = (PEER_OBJECT)Marshal.PtrToStructure(objData.pObject, typeof(PEER_OBJECT));

                        PeerObject peerObject = CollaborationHelperFunctions.ConvertPEER_OBJECTToPeerObject(po);;

                        PeerEndPoint peerEndPoint = null;
                        if (objData.pEndPoint != IntPtr.Zero)
                        {
                            PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(objData.pEndPoint, typeof(PEER_ENDPOINT));
                            peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                        }

                        objChangedArgs = new ObjectChangedEventArgs(peerEndPoint,
                                                                    null,
                                                                    objData.changeType,
                                                                    peerObject);
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //
                if (objChangedArgs != null)
                {
                    OnObjectChanged(objChangedArgs);
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving ObjectChangedCallback().");
        }
        internal override void PresenceChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "PresenceChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                PresenceChangedEventArgs presenceChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockPresenceChangedEvent){
                        if (m_safePresenceChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safePresenceChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetPresenceChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.MyPresenceChanged)
                    {
                        PEER_EVENT_PRESENCE_CHANGED_DATA presenceData = ped.presenceChangedData;

                        PeerPresenceInfo peerPresenceInfo = null;
                        if (presenceData.pPresenceInfo != IntPtr.Zero)
                        {
                            PEER_PRESENCE_INFO ppi = (PEER_PRESENCE_INFO)Marshal.PtrToStructure(presenceData.pPresenceInfo, typeof(PEER_PRESENCE_INFO));
                            peerPresenceInfo = new PeerPresenceInfo();
                            peerPresenceInfo.PresenceStatus  = ppi.status;
                            peerPresenceInfo.DescriptiveText = ppi.descText;
                        }

                        PeerEndPoint peerEndPoint = null;
                        if (presenceData.pEndPoint != IntPtr.Zero)
                        {
                            PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(presenceData.pEndPoint, typeof(PEER_ENDPOINT));
                            peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                        }

                        presenceChangedArgs = new PresenceChangedEventArgs(peerEndPoint,
                                                                           null,
                                                                           presenceData.changeType,
                                                                           peerPresenceInfo);
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //
                if (presenceChangedArgs != null)
                {
                    OnPresenceChanged(presenceChangedArgs);
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving PresenceChangedCallback().");
        }
Beispiel #12
0
        private static void PeerNearMeChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "PeerNearMeChangedCallback() called.");

            while (true)
            {
                PeerNearMeChangedEventArgs peerNearMeChangedArgs = null;

                //
                // Get the event data for the fired event
                //

                try{
                    lock (LockPNMChangedEvent){
                        if (s_safePeerNearMeChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(s_safePeerNearMeChangedEvent,
                                                                                     out eventData);
                    }
                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetPeerNearMeChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.PeopleNearMeChanged)
                    {
                        PEER_EVENT_PEOPLE_NEAR_ME_CHANGED_DATA pnmData = ped.peopleNearMeChangedData;
                        PeerNearMe peerNearMe = null;
                        if (pnmData.pPeopleNearMe != IntPtr.Zero)
                        {
                            PEER_PEOPLE_NEAR_ME pnm = (PEER_PEOPLE_NEAR_ME)Marshal.PtrToStructure(pnmData.pPeopleNearMe, typeof(PEER_PEOPLE_NEAR_ME));
                            peerNearMe = CollaborationHelperFunctions.PEER_PEOPLE_NEAR_METoPeerNearMe(pnm);
                        }

                        peerNearMeChangedArgs = new PeerNearMeChangedEventArgs(peerNearMe, pnmData.changeType);
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //

                EventHandler <PeerNearMeChangedEventArgs> handlerCopy = s_peerNearMeChanged;

                if ((peerNearMeChangedArgs != null) && (handlerCopy != null))
                {
                    handlerCopy(null, peerNearMeChangedArgs);
                    Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Fired the peer near me changed event callback.");
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving PeerNearMeChangedCallback().");
        }
Beispiel #13
0
        internal protected void InternalRefreshData(object state)
        {
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "InternalRefreshEndpointData called.");

            int       errorCode = 0;
            bool      isAsync   = (bool)state;
            Exception exception = null;

            AutoResetEvent  refreshedEPDataEvent = new AutoResetEvent(false);
            SafeCollabEvent safeRefreshedEPDataEvent;

            PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();

            pcer.eventType = PeerCollabEventType.RequestStatusChanged;
            pcer.pInstance = IntPtr.Zero;

            //
            // Register to receive status changed event from collab
            //
            errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                refreshedEPDataEvent.SafeWaitHandle,
                1,
                ref pcer,
                out safeRefreshedEPDataEvent);
            if (errorCode != 0)
            {
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                exception = PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ReqStatusChangedRegFailed), errorCode);
                if (!isAsync)
                {
                    throw exception;
                }
            }

            PeerEndPointCollection peerEndPoints = PeerEndPoints;

            if (peerEndPoints.Count == 0)
            {
                return;
            }

            try{
                InternalRefreshData(peerEndPoints[0]);
            }
            catch (Exception e) {
                if (!isAsync)
                {
                    throw;
                }
                else
                {
                    exception = e;
                }
            }

            //
            // Wait till all the endpoints are refreshed
            //
            while (exception == null)
            {
                refreshedEPDataEvent.WaitOne();

                SafeCollabData eventData;

                errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(safeRefreshedEPDataEvent,
                                                                             out eventData);
                if (errorCode != 0)
                {
                    Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                    exception = PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_RefreshDataFailed), errorCode);
                    if (!isAsync)
                    {
                        throw exception;
                    }
                    else
                    {
                        break;
                    }
                }

                PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                            typeof(PEER_COLLAB_EVENT_DATA));

                if (ped.eventType == PeerCollabEventType.RequestStatusChanged)
                {
                    PEER_EVENT_REQUEST_STATUS_CHANGED_DATA statusData = ped.requestStatusChangedData;

                    PeerEndPoint peerEndPoint = null;

                    if (statusData.pEndPoint != IntPtr.Zero)
                    {
                        PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(statusData.pEndPoint, typeof(PEER_ENDPOINT));
                        peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                    }

                    if (statusData.hrChange < 0)
                    {
                        exception = PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_RefreshDataFailed), statusData.hrChange);
                    }

                    if (exception != null)
                    {
                        //
                        // Throw exception for sync but call callback for async with exception
                        //
                        if (!isAsync)
                        {
                            throw exception;
                        }
                    }

                    //
                    // Check if this is our endpoint
                    //
                    if (PeerEndPoints[0].Equals(peerEndPoint))
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Found endpoint match in Request status changed.");

                        //
                        // For async call the callback and for sync just return
                        //
                        if (isAsync)
                        {
                            RefreshDataCompletedEventArgs args = new
                                                                 RefreshDataCompletedEventArgs(peerEndPoint,
                                                                                               null,
                                                                                               false,
                                                                                               m_refreshDataAsyncOp.UserSuppliedState);

                            if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information))
                            {
                                Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Firing RefreshDataCompleted event with folloding peer endpoint.");
                                peerEndPoint.TracePeerEndPoint();
                            }

                            this.PrepareToRaiseRefreshDataCompletedEvent(m_refreshDataAsyncOp, args);
                        }

                        break;
                    }
                }
            }

            //
            // Async case with exception fire callback here
            // Sync would have already thrown this by now
            //
            if (exception != null)
            {
                RefreshDataCompletedEventArgs args = new
                                                     RefreshDataCompletedEventArgs(null,
                                                                                   exception,
                                                                                   false,
                                                                                   m_refreshDataAsyncOp.UserSuppliedState);
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Firing RefreshDataCompleted event with exception {0}.", exception);
                this.PrepareToRaiseRefreshDataCompletedEvent(m_refreshDataAsyncOp, args);
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving InternalRefreshEndpointData.");
        }
Beispiel #14
0
        internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials)
        {
            PEER_CONTACT pc = new PEER_CONTACT();

            pc.pwzDisplayName     = peerContact.DisplayName;
            pc.pwzEmailAddress    = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString();
            pc.pwzNickname        = peerContact.Nickname;
            pc.pwzPeerName        = peerContact.PeerName.ToString();
            pc.fWatch             = peerContact.IsSubscribed;
            pc.WatcherPermissions = peerContact.SubscribeAllowed;
            PEER_DATA pd = new PEER_DATA();

            if (peerContact.Credentials != null)
            {
                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2),
                                                                                   0,
                                                                                   IntPtr.Zero,
                                                                                   0x00002000 /*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                                                                   IntPtr.Zero);

                if (certHandle == null || certHandle.IsInvalid)
                {
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }

                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    certStore.Add(peerContact.Credentials as X509Certificate2);
                    bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                                              0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                              2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                                              2, /*CERT_STORE_SAVE_TO_MEMORY*/
                                                                              ref pd,
                                                                              0);

                    if ((pd.cbData != 0) && (returnCode))
                    {
                        safeCredentials = new SafeCollabMemory((int)pd.cbData);
                        pd.pbData       = safeCredentials.DangerousGetHandle();
                        returnCode      = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                                                  0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                                  2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                                                  2,      /*CERT_STORE_SAVE_TO_MEMORY*/
                                                                                  ref pd, // Clean up memory from here;
                                                                                  0);
                    }
                    else
                    {
                        pd.cbData = 0;
                        pd.pbData = IntPtr.Zero;
                    }
                }
                finally{
                    if (certHandle != null)
                    {
                        certHandle.Dispose();
                    }
                }
            }
            else
            {
                pd.cbData = 0;
                pd.pbData = IntPtr.Zero;
            }
            pc.credentials = pd;

            return(pc);
        }
Beispiel #15
0
        private void NameChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "NameChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                NameChangedEventArgs nameChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockNameChangedEvent)
                    {
                        if (m_safeNameChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safeNameChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetNameChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.EndPointChanged)
                    {
                        PEER_EVENT_ENDPOINT_CHANGED_DATA epData = ped.endpointChangedData;
                        PeerEndPoint peerEndPoint = null;

                        if (epData.pEndPoint != IntPtr.Zero)
                        {
                            PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(epData.pEndPoint, typeof(PEER_ENDPOINT));
                            peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                        }

                        if ((peerEndPoint != null) && Equals(peerEndPoint))
                        {
                            PeerContact peerContact = null;

                            if (epData.pContact != IntPtr.Zero)
                            {
                                PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(epData.pContact, typeof(PEER_CONTACT));
                                peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc);
                            }


                            nameChangedArgs = new NameChangedEventArgs(peerEndPoint,
                                                                       peerContact,
                                                                       peerEndPoint.Name);
                        }
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //

                if (nameChangedArgs != null)
                {
                    OnNameChanged(nameChangedArgs);

                    //
                    // Change the name with the new name
                    //
                    Name = nameChangedArgs.PeerEndPoint.Name;
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving NameChangedCallback().");
        }