예제 #1
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.");
        }
예제 #2
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.");
        }
        private static void AddNameChanged(EventHandler<NameChangedEventArgs> callback)
        {
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering AddNameChanged().");

            //
            // Register a wait handle if one has not been registered already
            //
            lock (LockNameChangedEvent)
            {
                if (s_nameChanged == null){
                    s_nameChangedEvent = new AutoResetEvent(false);

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

                    s_regNameChangedWaitHandle = ThreadPool.RegisterWaitForSingleObject(s_nameChangedEvent, //Event that triggers the callback
                                            new WaitOrTimerCallback(NameChangedCallback), //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.MyEndPointChanged;
                    pcer.pInstance = IntPtr.Zero;

                    //
                    // Register event with collab
                    //

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

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddNameChanged() successful.");
        }
 internal extern static int PeerCollabRegisterEvent(SafeWaitHandle hEvent, UInt32 cEventRegistration,
                                                    ref PEER_COLLAB_EVENT_REGISTRATION pEventRegistrations,
                                                    out SafeCollabEvent phPeerEvent);
예제 #5
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 extern static int PeerCollabRegisterEvent(SafeWaitHandle hEvent, UInt32 cEventRegistration,
                                                     ref PEER_COLLAB_EVENT_REGISTRATION pEventRegistrations,
                                                     out SafeCollabEvent phPeerEvent);
예제 #7
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.");
        }