/// <summary>
        /// TODO: Documentation StartTurnPeer
        /// </summary>
        /// <param name="sid"></param>
        private void StartTurnPeer(String sid)
        {
            JingleSession jingleSession = this.JingleManager.FindSession(sid);

            if (jingleSession != null &&
                jingleSession.Remote.Action == ActionType.session_initiate)
            {
                JingleContent jingleContent = jingleSession.Remote.GetContent(0);
                JingleIce     jingleIce     = jingleContent.GetElement <JingleIce>(0);

                foreach (JingleIceCandidate candidate in jingleIce.GetCandidates())
                {
                    if (candidate.Type == IceCandidateType.relay)
                    {
                        if (this.OnTurnStart != null)
                        {
                            this.OnTurnStart(this, candidate.EndPoint, sid);
                        }

                        if (this.OnConnectionTryEnded != null)
                        {
                            this.OnConnectionTryEnded(this, sid);
                        }

                        this.CancelStartingSession();
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// TODO: Documentation CheckConnectivity
        /// </summary>
        /// <param name="sid"></param>
        private void CheckConnectivity(String sid)
        {
            if (this.turnSessions.ContainsKey(sid))
            {
                this.holePuncher = new HolePuncher(this.turnSessions[sid].TurnManager.HostEP, sid);

                JingleSession jingleSession = this.JingleManager.FindSession(sid);

                JingleContent jingleContent = jingleSession.Remote.GetContent(0);

                JingleIce jingleIce = jingleContent.GetElement <JingleIce>(0);

                foreach (JingleIceCandidate remoteCandidate in jingleIce.GetCandidates())
                {
                    switch (remoteCandidate.Type)
                    {
                    case IceCandidateType.host:
                    case IceCandidateType.prflx:
                    case IceCandidateType.srflx:
                        foreach (JingleIceCandidate localCandidate in this.localCandidates[sid])
                        {
                            if (localCandidate.Type == remoteCandidate.Type)
                            {
                                this.holePuncher.AddEP(remoteCandidate.Priority, remoteCandidate.EndPoint);
                                break;
                            }
                        }
                        break;

                    case IceCandidateType.relay:
                        if (this.TurnSupported &&
                            jingleSession.Remote.Action == ActionType.session_accept)
                        {
                            this.turnSessions[sid].TurnManager.CreatePermission(new XorMappedAddress(remoteCandidate.RelatedEndPoint),
                                                                                this.turnSessions[sid].TurnAllocation);
                        }
                        break;
                    }
                }

                if (!this.holePuncher.CanStart && this.TurnSupported)
                {
                    this.StartTurnPeer(sid);
                }
                else
                {
                    this.holePuncher.StartTcpPunch(this.HolePunchSuccess, this.HolePunchFailure);
                }
            }
        }
        /// <summary>
        /// TODO: Documentation HolePunchSuccess
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connectedSocket"></param>
        /// <param name="punchData"></param>
        private void HolePunchSuccess(object sender, Socket connectedSocket, Object punchData)
        {
            JingleSession jingleSession = this.JingleManager.FindSession(punchData as String);

            if (this.OnConnectionTryEnded != null)
            {
                this.OnConnectionTryEnded(this, punchData as String);
            }

            if (this.OnHolePunchSucceed != null)
            {
                this.OnHolePunchSucceed(this, connectedSocket, jingleSession.Remote);
            }


            this.DestroyTurnSession(punchData as String);
            this.CancelStartingSession();
        }
Beispiel #4
0
        /// <summary>
        /// Analyses an IQ paquet and chooses to handle it or not
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="iq"></param>
        private void GotIQ(object sender, IQ iq)
        {
            Jingle jingle = iq.Query as Jingle;

            if (jingle != null)
            {
                if (jingle.Action == ActionType.session_terminate)
                {
                    this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                    if (this.OnReceivedSessionTerminate != null)
                    {
                        this.OnReceivedSessionTerminate(this, iq);
                    }

                    if (this.Sessions.ContainsKey(jingle.Sid))
                    {
                        this.Sessions.Remove(jingle.Sid);
                    }

                    iq.Handled = true;
                }
                else
                {
                    if (!this.AllowSessionInitiateFrom(iq.From))
                    {
                        this.SessionTerminate(iq.From, jingle.Sid, ReasonType.security_error);
                        iq.Handled = true;
                    }
                    else
                    {
                        switch (jingle.Action)
                        {
                        case ActionType.session_initiate:
                            this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                            if (!this.Sessions.ContainsKey(jingle.Sid))
                            {
                                JingleSession jingleSession = new JingleSession();
                                jingleSession.SID    = jingle.Sid;
                                jingleSession.Remote = jingle;

                                this.Sessions.Add(jingle.Sid, jingleSession);
                            }

                            if (this.OnReceivedSessionInitiate != null)
                            {
                                this.OnReceivedSessionInitiate(this, iq);
                            }

                            if (!iq.Handled)
                            {
                                this.SessionTerminate(iq.From, jingle.Sid, ReasonType.decline);
                                iq.Handled = true;
                            }

                            break;

                        case ActionType.session_accept:
                            this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                            if (!this.Sessions.ContainsKey(jingle.Sid))
                            {
                                JingleSession jingleSession = new JingleSession();
                                jingleSession.SID    = jingle.Sid;
                                jingleSession.Remote = jingle;

                                this.Sessions.Add(jingle.Sid, jingleSession);
                            }

                            if (this.OnReceivedSessionAccept != null)
                            {
                                this.OnReceivedSessionAccept(this, iq);
                            }

                            if (!iq.Handled)
                            {
                                this.SessionTerminate(iq.From, jingle.Sid, ReasonType.cancel);
                                iq.Handled = true;
                            }

                            break;

                        case ActionType.session_info:
                            if (this.Sessions.ContainsKey(jingle.Sid))
                            {
                                this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));
                            }
                            else
                            {
                                this.Stream.Write(new UnknownSession(this.Stream.Document));
                            }

                            if (this.OnReceivedSessionInfo != null)
                            {
                                this.OnReceivedSessionInfo(this, iq);
                            }

                            break;

                        case ActionType.transport_info:
                            if (this.Sessions.ContainsKey(jingle.Sid))
                            {
                                this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));
                            }
                            else
                            {
                                this.Stream.Write(new UnknownSession(this.Stream.Document));
                            }

                            if (this.OnReceivedTransportInfo != null)
                            {
                                this.OnReceivedTransportInfo(this, iq);
                            }

                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Analyses an IQ paquet and chooses to handle it or not
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="iq"></param>
        private void GotIQ(object sender, IQ iq)
        {
            Jingle jingle = iq.Query as Jingle;

            if (jingle != null)
            {
                if (jingle.Action == ActionType.session_terminate)
                {
                    this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                    if (this.OnReceivedSessionTerminate != null)
                        this.OnReceivedSessionTerminate(this, iq);

                    if (this.Sessions.ContainsKey(jingle.Sid))
                        this.Sessions.Remove(jingle.Sid);

                    iq.Handled = true;
                }
                else
                {
                    if (!this.AllowSessionInitiateFrom(iq.From))
                    {
                        this.SessionTerminate(iq.From, jingle.Sid, ReasonType.security_error);
                        iq.Handled = true;
                    }
                    else
                    {
                        switch (jingle.Action)
                        {
                            case ActionType.session_initiate:
                                this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                                if (!this.Sessions.ContainsKey(jingle.Sid))
                                {
                                    JingleSession jingleSession = new JingleSession();
                                    jingleSession.SID = jingle.Sid;
                                    jingleSession.Remote = jingle;

                                    this.Sessions.Add(jingle.Sid, jingleSession);
                                }

                                if (this.OnReceivedSessionInitiate != null)
                                    this.OnReceivedSessionInitiate(this, iq);

                                if (!iq.Handled)
                                {
                                    this.SessionTerminate(iq.From, jingle.Sid, ReasonType.decline);
                                    iq.Handled = true;
                                }

                                break;

                            case ActionType.session_accept:
                                this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));

                                if (!this.Sessions.ContainsKey(jingle.Sid))
                                {
                                    JingleSession jingleSession = new JingleSession();
                                    jingleSession.SID = jingle.Sid;
                                    jingleSession.Remote = jingle;

                                    this.Sessions.Add(jingle.Sid, jingleSession);
                                }

                                if (this.OnReceivedSessionAccept != null)
                                    this.OnReceivedSessionAccept(this, iq);

                                if (!iq.Handled)
                                {
                                    this.SessionTerminate(iq.From, jingle.Sid, ReasonType.cancel);
                                    iq.Handled = true;
                                }

                                break;

                            case ActionType.session_info:
                                if (this.Sessions.ContainsKey(jingle.Sid))
                                    this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));
                                else
                                    this.Stream.Write(new UnknownSession(this.Stream.Document));

                                if (this.OnReceivedSessionInfo != null)
                                    this.OnReceivedSessionInfo(this, iq);

                                break;

                            case ActionType.transport_info:
                                if (this.Sessions.ContainsKey(jingle.Sid))
                                    this.Stream.Write(iq.GetAcknowledge(this.Stream.Document));
                                else
                                    this.Stream.Write(new UnknownSession(this.Stream.Document));

                                if (this.OnReceivedTransportInfo != null)
                                    this.OnReceivedTransportInfo(this, iq);

                                break;
                        }
                    }
                }
            }
        }