Пример #1
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);
            }
        }
Пример #2
0
        public override PeerInvitationResponse Invite()
        {
            if (m_Disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand();

            Guid appGuid = CurrentApplicationGuid;

            if (appGuid.Equals(Guid.Empty))
            {
                throw new PeerToPeerException(SR.GetString(SR.Collab_NoGuidForCurrApp));
            }

            //
            // We need at least one endpoint to send invitation to
            //
            PeerEndPointCollection peerEndPoints = PeerEndPoints;

            if ((peerEndPoints == null) || (peerEndPoints.Count == 0))
            {
                throw new PeerToPeerException(SR.GetString(SR.Collab_NoEndpointFound));
            }

            PeerEndPoint peerEndPoint = PeerEndPoints[0];

            PeerInvitationResponse response = InternalInviteEndPoint(appGuid, null, null, peerEndPoint, null);

            // throw an exception if the response type is ERROR
            CollaborationHelperFunctions.ThrowIfInvitationResponseInvalid(response);
            return(response);
        }
Пример #3
0
 internal NameChangedEventArgs(PeerEndPoint peerEndPoint, PeerContact peerContact,
                               string name)
 {
     m_peerEndPoint = peerEndPoint;
     m_peerContact  = peerContact;
     m_name         = name;
 }
Пример #4
0
        internal static PeerNearMe PEER_PEOPLE_NEAR_METoPeerNearMe(PEER_PEOPLE_NEAR_ME ppnm)
        {
            PeerNearMe peerNearMe = new PeerNearMe();

            peerNearMe.Id       = CollaborationHelperFunctions.ConvertGUIDToGuid(ppnm.id);
            peerNearMe.Nickname = Marshal.PtrToStringUni(ppnm.pwzNickname);;

            PEER_ENDPOINT pe     = ppnm.endpoint;
            PeerEndPoint  peerEP = ConvertPEER_ENDPOINTToPeerEndPoint(pe);

            peerNearMe.PeerEndPoints.Add(peerEP);

            return(peerNearMe);
        }
Пример #5
0
        public override PeerInvitationResponse Invite(PeerApplication applicationToInvite, string message,
                                                      byte[] invitationData)
        {
            if (m_Disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand();

            if (applicationToInvite == null)
            {
                throw new ArgumentNullException("applicationToInvite");
            }
            if (applicationToInvite.Id == Guid.Empty)
            {
                throw new PeerToPeerException(SR.GetString(SR.Collab_EmptyGuidError));
            }

            //
            // We need at least one endpoint to send invitation to
            //
            PeerEndPointCollection peerEndPoints = PeerEndPoints;

            if ((peerEndPoints == null) || (peerEndPoints.Count == 0))
            {
                throw new PeerToPeerException(SR.GetString(SR.Collab_NoEndpointFound));
            }

            PeerEndPoint peerEndPoint = PeerEndPoints[0];

            PeerInvitationResponse response = InternalInviteEndPoint(applicationToInvite.Id, message, invitationData,
                                                                     peerEndPoint, null);

            // throw an exception if the response type is ERROR
            CollaborationHelperFunctions.ThrowIfInvitationResponseInvalid(response);
            return(response);
        }
Пример #6
0
        public static PeerNearMe CreateFromPeerEndPoint(PeerEndPoint peerEndPoint)
        {
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering CreateFromPeerEndPoint.");
            CollaborationHelperFunctions.Initialize();

            if (peerEndPoint == null)
            {
                throw new ArgumentNullException("peerEndPoint");
            }
            if (peerEndPoint.EndPoint == null)
            {
                throw new PeerToPeerException(SR.GetString(SR.Collab_NoEndPointInPeerEndPoint));
            }

            PeerNearMeCollection peers = PeerCollaboration.GetPeersNearMe();
            PeerNearMe           peer  = null;

            foreach (PeerNearMe peerNearMe in peers)
            {
                PeerEndPointCollection peerEndPoints = peerNearMe.PeerEndPoints;
                if ((peerEndPoints != null) && (peerEndPoints.Count != 0) && (peerEndPoints[0].Equals(peerEndPoint)))
                {
                    peer = peerNearMe;
                }
            }
            if (peer == null)
            {
                //
                // No peer found, throw
                //
                throw new PeerToPeerException(SR.GetString(SR.Collab_EndPointNotAPeerNearMe));
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving CreateFromPeerEndPoint.");
            return(peer);
        }
Пример #7
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().");
        }
Пример #10
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.");
        }