private void Migrate(P2PBridge newBridge) { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, String.Format("P2PSession {0} migrating from bridge {1} to {2}", sessionId, (p2pBridge != null) ? p2pBridge.ToString() : "null", (newBridge != null) ? newBridge.ToString() : "null"), GetType().Name); if (p2pBridge != null) { p2pBridge.BridgeOpened -= BridgeOpened; p2pBridge.BridgeSynced -= BridgeSynced; p2pBridge.BridgeClosed -= BridgeClosed; p2pBridge.BridgeSent -= BridgeSent; p2pBridge.MigrateQueue(this, newBridge); } p2pBridge = newBridge; if (p2pBridge != null) { p2pBridge.BridgeOpened += BridgeOpened; p2pBridge.BridgeSynced += BridgeSynced; p2pBridge.BridgeClosed += BridgeClosed; p2pBridge.BridgeSent += BridgeSent; if (localIdentifier == 0) { localBaseIdentifier = p2pBridge.SequenceId; localIdentifier = localBaseIdentifier; } else { p2pBridge.SequenceId = localIdentifier; } if ((directNegotiationTimer != null) & (p2pBridge is TCPv1Bridge)) { DirectNegotiationSuccessful(); } } }
public bool ProcessP2PMessage(P2PBridge bridge, Contact source, Guid sourceGuid, P2PMessage p2pMessage) { // 1) SLP BUFFERING: Combine splitted SLP messages if (slpMessagePool.BufferMessage(ref p2pMessage)) { // * Buffering: Not completed yet, we must wait next packets -OR- // * Invalid packet received: Don't kill me, just ignore it... return(true); } Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, String.Format("Received P2PMessage from {0}\r\n{1}", bridge.ToString(), p2pMessage.ToDebugString()), GetType().Name); // 2) CHECK SLP: Check destination, source, endpoints SLPMessage slp = p2pMessage.IsSLPData ? p2pMessage.InnerMessage as SLPMessage : null; if (slp != null) { if (!slpHandler.CheckSLPMessage(bridge, source, sourceGuid, p2pMessage, slp)) { return(true); // HANDLED, This SLP is not for us. } } // 3) FIRST SLP MESSAGE: Create applications/sessions based on invitation if (slp != null && slp is SLPRequestMessage && (slp as SLPRequestMessage).Method == "INVITE" && slp.ContentType == "application/x-msnmsgr-sessionreqbody") { uint appId = slp.BodyValues.ContainsKey("AppID") ? uint.Parse(slp.BodyValues["AppID"].Value) : 0; Guid eufGuid = slp.BodyValues.ContainsKey("EUF-GUID") ? new Guid(slp.BodyValues["EUF-GUID"].Value) : Guid.Empty; P2PVersion ver = slp.P2PVersion; if (P2PApplication.IsRegistered(eufGuid, appId)) { P2PSession newSession = FindSessionByCallId(slp.CallId, ver); if (newSession == null) { newSession = new P2PSession(slp as SLPRequestMessage, p2pMessage, nsMessageHandler, bridge); newSession.Closed += P2PSessionClosed; if (newSession.Version == P2PVersion.P2PV2) { p2pV2Sessions.Add(newSession); } else { p2pV1Sessions.Add(newSession); } } else { // P2PSession exists, bridge changed... if (newSession.Bridge != bridge) { // BRIDGETODO } } return(true); } // Not registered application. Decline it without create a new session... slpHandler.SendSLPStatus(bridge, p2pMessage, source, sourceGuid, 603, "Decline"); return(true); } // 4) FIND SESSION: Search session by SessionId/ExpectedIdentifier P2PSession session = FindSession(p2pMessage, slp); if (session != null) { // ResetTimeoutTimer(); // Keep track of theremoteIdentifier // Keep track of the remote identifier session.remoteIdentifier = (p2pMessage.Version == P2PVersion.P2PV2) ? p2pMessage.Header.Identifier + p2pMessage.Header.MessageSize : p2pMessage.Header.Identifier; // Session SLP if (slp != null && slpHandler.HandleP2PSessionSignal(bridge, p2pMessage, slp, session)) { return(true); } // Session Data if (slp == null && session.ProcessP2PData(bridge, p2pMessage)) { return(true); } } return(false); }
/// <summary> /// Move the specified <see cref="P2PSession"/> to the new <see cref="P2PBridge"/> specified. /// </summary> /// <param name="session"></param> /// <param name="newBridge"></param> public virtual void MigrateQueue(P2PSession session, P2PBridge newBridge) { Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, String.Format("P2PBridge {0} migrating session {1} queue to new bridge {2}", this.ToString(), session.SessionId, (newBridge != null) ? newBridge.ToString() : "null"), GetType().Name); P2PSendQueue newQueue = new P2PSendQueue(); lock (sendingQueues) { if (sendingQueues.ContainsKey(session)) { if (newBridge != null) { lock (sendingQueues[session]) { foreach (P2PSendItem item in sendingQueues[session]) newQueue.Enqueue(item); } } sendingQueues.Remove(session); } } lock (sendQueues) { if (sendQueues.ContainsKey(session)) { if (newBridge != null) { while (sendQueues[session].Count > 0) newQueue.Enqueue(sendQueues[session].Dequeue()); } sendQueues.Remove(session); } } lock (stoppedSessions) { if (stoppedSessions.Contains(session)) { stoppedSessions.Remove(session); } } if (newBridge != null) newBridge.AddQueue(session, newQueue); }
private void Migrate(P2PBridge newBridge) { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, String.Format("P2PSession {0} migrating from bridge {1} to {2}", sessionId, (p2pBridge != null) ? p2pBridge.ToString() : "null", (newBridge != null) ? newBridge.ToString() : "null"), GetType().Name); if (p2pBridge != null) { p2pBridge.BridgeOpened -= BridgeOpened; p2pBridge.BridgeSynced -= BridgeSynced; p2pBridge.BridgeClosed -= BridgeClosed; p2pBridge.BridgeSent -= BridgeSent; p2pBridge.MigrateQueue(this, newBridge); } p2pBridge = newBridge; if (p2pBridge != null) { p2pBridge.BridgeOpened += BridgeOpened; p2pBridge.BridgeSynced += BridgeSynced; p2pBridge.BridgeClosed += BridgeClosed; p2pBridge.BridgeSent += BridgeSent; if (localIdentifier == 0) { localBaseIdentifier = p2pBridge.SequenceId; localIdentifier = localBaseIdentifier; } else { p2pBridge.SequenceId = localIdentifier; } if ((directNegotiationTimer != null) & (p2pBridge is TCPv1Bridge)) DirectNegotiationSuccessful(); } }
public bool ProcessP2PMessage(P2PBridge bridge, Contact source, Guid sourceGuid, P2PMessage p2pMessage) { // 1) SLP BUFFERING: Combine splitted SLP messages if (slpMessagePool.BufferMessage(ref p2pMessage)) { // * Buffering: Not completed yet, we must wait next packets -OR- // * Invalid packet received: Don't kill me, just ignore it... return true; } Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, String.Format("Received P2PMessage from {0}\r\n{1}", bridge.ToString(), p2pMessage.ToDebugString()), GetType().Name); // 2) CHECK SLP: Check destination, source, endpoints SLPMessage slp = p2pMessage.IsSLPData ? p2pMessage.InnerMessage as SLPMessage : null; if (slp != null) { if (!slpHandler.CheckSLPMessage(bridge, source, sourceGuid, p2pMessage, slp)) return true; // HANDLED, This SLP is not for us. } // 3) FIRST SLP MESSAGE: Create applications/sessions based on invitation if (slp != null && slp is SLPRequestMessage && (slp as SLPRequestMessage).Method == "INVITE" && slp.ContentType == "application/x-msnmsgr-sessionreqbody") { uint appId = slp.BodyValues.ContainsKey("AppID") ? uint.Parse(slp.BodyValues["AppID"].Value) : 0; Guid eufGuid = slp.BodyValues.ContainsKey("EUF-GUID") ? new Guid(slp.BodyValues["EUF-GUID"].Value) : Guid.Empty; P2PVersion ver = slp.P2PVersion; if (P2PApplication.IsRegistered(eufGuid, appId)) { P2PSession newSession = FindSessionByCallId(slp.CallId, ver); if (newSession == null) { newSession = new P2PSession(slp as SLPRequestMessage, p2pMessage, nsMessageHandler, bridge); newSession.Closed += P2PSessionClosed; if (newSession.Version == P2PVersion.P2PV2) p2pV2Sessions.Add(newSession); else p2pV1Sessions.Add(newSession); } else { // P2PSession exists, bridge changed... if (newSession.Bridge != bridge) { // BRIDGETODO } } return true; } // Not registered application. Decline it without create a new session... slpHandler.SendSLPStatus(bridge, p2pMessage, source, sourceGuid, 603, "Decline"); return true; } // 4) FIND SESSION: Search session by SessionId/ExpectedIdentifier P2PSession session = FindSession(p2pMessage, slp); if (session != null) { // ResetTimeoutTimer(); // Keep track of theremoteIdentifier // Keep track of the remote identifier session.remoteIdentifier = (p2pMessage.Version == P2PVersion.P2PV2) ? p2pMessage.Header.Identifier + p2pMessage.Header.MessageSize : p2pMessage.Header.Identifier; // Session SLP if (slp != null && slpHandler.HandleP2PSessionSignal(bridge, p2pMessage, slp, session)) return true; // Session Data if (slp == null && session.ProcessP2PData(bridge, p2pMessage)) return true; } return false; }
/// <summary> /// Move the specified <see cref="P2PSession"/> to the new <see cref="P2PBridge"/> specified. /// </summary> /// <param name="session"></param> /// <param name="newBridge"></param> public virtual void MigrateQueue(P2PSession session, P2PBridge newBridge) { Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, String.Format("P2PBridge {0} migrating session {1} queue to new bridge {2}", this.ToString(), session.SessionId, (newBridge != null) ? newBridge.ToString() : "null"), GetType().Name); P2PSendQueue newQueue = new P2PSendQueue(); lock (sendingQueues) { if (sendingQueues.ContainsKey(session)) { if (newBridge != null) { lock (sendingQueues[session]) { foreach (P2PSendItem item in sendingQueues[session]) { newQueue.Enqueue(item); } } } sendingQueues.Remove(session); } } lock (sendQueues) { if (sendQueues.ContainsKey(session)) { if (newBridge != null) { while (sendQueues[session].Count > 0) { newQueue.Enqueue(sendQueues[session].Dequeue()); } } sendQueues.Remove(session); } } lock (stoppedSessions) { if (stoppedSessions.Contains(session)) { stoppedSessions.Remove(session); } } if (newBridge != null) { newBridge.AddQueue(session, newQueue); } }