Esempio n. 1
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().");
        }
Esempio n. 2
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().");
        }