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); } }
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); }
internal static PeerEndPoint ConvertPEER_ENDPOINTToPeerEndPoint(PEER_ENDPOINT pe) { byte[] addrBytes = new byte[] { pe.peerAddress.sin6.sin6_addr0, pe.peerAddress.sin6.sin6_addr1, pe.peerAddress.sin6.sin6_addr2, pe.peerAddress.sin6.sin6_addr3, pe.peerAddress.sin6.sin6_addr4, pe.peerAddress.sin6.sin6_addr5, pe.peerAddress.sin6.sin6_addr6, pe.peerAddress.sin6.sin6_addr7, pe.peerAddress.sin6.sin6_addr8, pe.peerAddress.sin6.sin6_addr9, pe.peerAddress.sin6.sin6_addr10, pe.peerAddress.sin6.sin6_addr11, pe.peerAddress.sin6.sin6_addr12, pe.peerAddress.sin6.sin6_addr13, pe.peerAddress.sin6.sin6_addr14, pe.peerAddress.sin6.sin6_addr15 }; IPAddress IPAddr = new IPAddress(addrBytes, (long)pe.peerAddress.sin6.sin6_scope_id); ushort port; unchecked { port = (ushort)IPAddress.NetworkToHostOrder((short)pe.peerAddress.sin6.sin6_port); } IPEndPoint IPEndPt = new IPEndPoint(IPAddr, port); return(new PeerEndPoint(IPEndPt, Marshal.PtrToStringUni(pe.pwzEndpointName))); }
internal static PeerEndPoint ConvertPEER_ENDPOINTToPeerEndPoint(PEER_ENDPOINT pe) { byte[] addrBytes = new byte[]{ pe.peerAddress.sin6.sin6_addr0, pe.peerAddress.sin6.sin6_addr1, pe.peerAddress.sin6.sin6_addr2, pe.peerAddress.sin6.sin6_addr3, pe.peerAddress.sin6.sin6_addr4, pe.peerAddress.sin6.sin6_addr5, pe.peerAddress.sin6.sin6_addr6, pe.peerAddress.sin6.sin6_addr7, pe.peerAddress.sin6.sin6_addr8, pe.peerAddress.sin6.sin6_addr9, pe.peerAddress.sin6.sin6_addr10, pe.peerAddress.sin6.sin6_addr11, pe.peerAddress.sin6.sin6_addr12, pe.peerAddress.sin6.sin6_addr13, pe.peerAddress.sin6.sin6_addr14, pe.peerAddress.sin6.sin6_addr15}; IPAddress IPAddr = new IPAddress(addrBytes, (long)pe.peerAddress.sin6.sin6_scope_id); ushort port; unchecked{ port = (ushort)IPAddress.NetworkToHostOrder((short)pe.peerAddress.sin6.sin6_port); } IPEndPoint IPEndPt = new IPEndPoint(IPAddr, port); return new PeerEndPoint(IPEndPt, Marshal.PtrToStringUni(pe.pwzEndpointName)); }
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()."); }
public PeerContact CreateContact(PeerNearMe peerNearMe) { if (m_Disposed) throw new ObjectDisposedException(this.GetType().FullName); PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); if (peerNearMe == null) throw new ArgumentNullException("peerNearMe"); if ((peerNearMe.PeerEndPoints == null) || (peerNearMe.PeerEndPoints.Count == 0) || (peerNearMe.PeerEndPoints[0].EndPoint == null)) throw new PeerToPeerException(SR.GetString(SR.Collab_NoEndpointFound)); Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering CreateContact() with peernearme", peerNearMe.ToString()); PeerEndPoint peerEndPoint = peerNearMe.PeerEndPoints[0]; PEER_ENDPOINT pe = new PEER_ENDPOINT(); pe.peerAddress = CollaborationHelperFunctions.ConvertIPEndpointToPEER_ADDRESS(peerEndPoint.EndPoint); // // Pin all the data to pass to native // GCHandle pepName = new GCHandle(); if (peerEndPoint.Name != null){ pepName = GCHandle.Alloc(peerEndPoint.Name, GCHandleType.Pinned); pe.pwzEndpointName = pepName.AddrOfPinnedObject(); } GCHandle peerEP = GCHandle.Alloc(pe, GCHandleType.Pinned); IntPtr ptrPeerEP = peerEP.AddrOfPinnedObject(); string contactData = null; int errorCode = 0; // // Refresh end point data if it not subscribed // peerNearMe.RefreshData(); errorCode = UnsafeCollabNativeMethods.PeerCollabQueryContactData(ptrPeerEP, ref contactData); if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetContact returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CreateContactFailed), errorCode); } SafeCollabData contact = null; PeerContact peerContact = null; try{ errorCode = UnsafeCollabNativeMethods.PeerCollabParseContact(contactData, out contact); if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetContact returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CreateContactFailed), errorCode); } PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(contact.DangerousGetHandle(), typeof(PEER_CONTACT)); peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc); // // Mark it as just created and add the xml. This is used when adding the contact or getting // contact xml when contact is not added // peerContact.JustCreated = true; peerContact.ContactXml = contactData; } finally{ if (contact != null) contact.Dispose(); pepName.Free(); peerEP.Free(); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving CreateContact()."); return peerContact; }
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."); }
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()."); }