Exemplo n.º 1
0
        internal static PeerObject ConvertPEER_OBJECTToPeerObject(PEER_OBJECT po)
        {
            byte[] data = null;

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

            return(new PeerObject(ConvertGUIDToGuid(po.guid), data, (PeerScope)po.dwPublicationScope));
        }
        public static void SetObject(PeerObject peerObject)
        {
            PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand();

            if (peerObject == null){
                throw new ArgumentNullException("peerObject");
            }

            CollaborationHelperFunctions.Initialize();

            if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information)){
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "SetObject() is called with the following info");
                peerObject.TracePeerObject();
            }

            PEER_OBJECT po = new PEER_OBJECT();
            int errorCode = 0;

            //
            // Create the native PEER_OBJECT struct
            //

            po.guid = CollaborationHelperFunctions.ConvertGuidToGUID(peerObject.Id);
            po.dwPublicationScope = (uint)peerObject.PeerScope;
            SafeCollabMemory data = null;

            try{
                if ((peerObject.Data != null) && (peerObject.Data.Length > 0)){
                    data = new SafeCollabMemory(peerObject.Data.Length);
                    po.data.pbData = data.DangerousGetHandle();
                    po.data.cbData = (UInt32)peerObject.Data.Length;

                    Marshal.Copy(peerObject.Data, 0, po.data.pbData, peerObject.Data.Length);
                }
                else{
                    po.data.pbData = IntPtr.Zero;
                    po.data.cbData = 0;
                }

                errorCode = UnsafeCollabNativeMethods.PeerCollabSetObject(ref po);
            }
            finally{
                if (data != null) data.Dispose();
            }

            if (errorCode == UnsafeCollabReturnCodes.PEER_E_ALREADY_EXISTS){
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabSetObject returned with errorcode {0}. Object already set.", errorCode);
                throw new ArgumentException(SR.GetString(SR.Collab_ObjectSetFailed) + " " + SR.GetString(SR.Collab_ObjectExists));
            }
            else if (errorCode != 0){
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabSetObject returned with errorcode {0}", errorCode);
                throw (PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ObjectSetFailed), errorCode));
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Set Object successful.");
        }
        internal static PeerObject ConvertPEER_OBJECTToPeerObject(PEER_OBJECT po)
        {
            byte[] data = null;

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

            return new PeerObject(ConvertGUIDToGuid(po.guid), data, (PeerScope)po.dwPublicationScope);
        }
 internal extern static int PeerCollabSetObject(ref PEER_OBJECT pcObject);
 internal extern static int PeerCollabSetObject(ref PEER_OBJECT pcObject);
        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().");
        }