/// <summary> /// Creates a new session initiated locally and prepares the invitation message. /// The next step must be Invite(). /// </summary> public P2PSession(P2PApplication app) { p2pApplication = app; version = app.P2PVersion; localContact = app.Local; remoteContact = app.Remote; localContactEndPointID = NSMessageHandler.MachineGuid; remoteContactEndPointID = app.Remote.SelectBestEndPointId(); nsMessageHandler = app.Local.NSMessageHandler; sessionId = (uint)random.Next(50000, int.MaxValue); // These 2 fields are set when optimal bridge found. localBaseIdentifier = 0; localIdentifier = 0; invitation = new SLPRequestMessage(RemoteContactEPIDString, MSNSLPRequestMethod.INVITE); invitation.Target = RemoteContactEPIDString; invitation.Source = LocalContactEPIDString; invitation.ContentType = "application/x-msnmsgr-sessionreqbody"; invitation.BodyValues["SessionID"] = SessionId.ToString(System.Globalization.CultureInfo.InvariantCulture); app.SetupInviteMessage(invitation); app.P2PSession = this; // Register events remoteContact.DirectBridgeEstablished += RemoteDirectBridgeEstablished; Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, String.Format("{0} created (Initiated locally)", SessionId), GetType().Name); status = P2PSessionStatus.WaitingForRemote; // Next step must be: Invite() }
private void DisposeApp() { if (p2pApplication != null) { p2pApplication.Dispose(); p2pApplication = null; } }
public P2PSession AddTransfer(P2PApplication app) { P2PSession session = new P2PSession(app); session.Closed += P2PSessionClosed; if (app.P2PVersion == P2PVersion.P2PV2) { p2pV2Sessions.Add(session); } else { p2pV1Sessions.Add(session); } session.Invite(); return(session); }
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> /// Creates a new session initiated remotely. If the application cannot be handled, decline message will be sent. /// The application will handle all messages automatically and no user interaction is required if AutoAccept is true. /// If AutoAccept is false, <see cref="P2PHandler.InvitationReceived"/> event will be fired. /// </summary> public P2PSession(SLPRequestMessage slp, P2PMessage msg, NSMessageHandler ns, P2PBridge bridge) { nsMessageHandler = ns; invitation = slp; version = slp.P2PVersion; if (version == P2PVersion.P2PV1) { localContact = (slp.ToEmailAccount == ns.Owner.Account) ? ns.Owner : ns.ContactList.GetContactWithCreate(slp.ToEmailAccount, IMAddressInfoType.WindowsLive); remoteContact = ns.ContactList.GetContactWithCreate(slp.FromEmailAccount, IMAddressInfoType.WindowsLive); } else { localContact = (slp.ToEmailAccount == ns.Owner.Account) ? ns.Owner : ns.ContactList.GetContactWithCreate(slp.ToEmailAccount, IMAddressInfoType.WindowsLive); localContactEndPointID = slp.ToEndPoint; remoteContact = ns.ContactList.GetContactWithCreate(slp.FromEmailAccount, IMAddressInfoType.WindowsLive); remoteContactEndPointID = slp.FromEndPoint; } if (!uint.TryParse(slp.BodyValues["SessionID"].Value, out sessionId)) { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, "Can't parse SessionID: " + SessionId, GetType().Name); } p2pBridge = bridge; localBaseIdentifier = bridge.SequenceId; localIdentifier = localBaseIdentifier; status = P2PSessionStatus.WaitingForLocal; Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, String.Format("{0} created (Initiated remotely)", SessionId), GetType().Name); remoteContact.DirectBridgeEstablished += RemoteDirectBridgeEstablished; if (msg != null) { // Set remote baseID remoteBaseIdentifier = msg.Header.Identifier; remoteIdentifier = remoteBaseIdentifier; if (version == P2PVersion.P2PV2) { remoteIdentifier += msg.V2Header.MessageSize; } } // Create application based on invitation 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; p2pApplication = P2PApplication.CreateInstance(eufGuid, appId, this); if (p2pApplication != null && p2pApplication.ValidateInvitation(invitation)) { if (p2pApplication.AutoAccept) { Accept(); } else { ns.P2PHandler.OnInvitationReceived(new P2PSessionEventArgs(this)); } } else { Decline(); } }