Exemple #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().");
        }
        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().");
        }
Exemple #4
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().");
        }
Exemple #5
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.");
        }
Exemple #6
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().");
        }