Beispiel #1
0
        /// <summary>
        /// Removes a JID from the recognized set of JIDs.
        /// </summary>
        /// <param name="FullJID">Full JID.</param>
        public void RemovePeerAddresses(string FullJID)
        {
            string ThisExternalIp = this.p2pNetwork.ExternalAddress is null ? string.Empty : this.p2pNetwork.ExternalAddress.ToString();

            lock (this.addressesByFullJid)
            {
                if (this.addressesByFullJid.TryGetValue(FullJID, out AddressInfo Info))
                {
                    this.addressesByFullJid.Remove(FullJID);

                    if (this.addressesByExternalIPPort.TryGetValue(Info.ExternalIp, out Dictionary <int, AddressInfo> Infos))
                    {
                        if (Infos.Remove(Info.ExternalPort) && Infos.Count == 0)
                        {
                            this.addressesByExternalIPPort.Remove(Info.ExternalIp);
                        }
                    }

                    if (Info.ExternalIp == ThisExternalIp)
                    {
                        if (this.addressesByLocalIPPort.TryGetValue(Info.LocalIp, out Infos))
                        {
                            if (Infos.Remove(Info.LocalPort) && Infos.Count == 0)
                            {
                                this.addressesByLocalIPPort.Remove(Info.LocalIp);
                            }
                        }
                    }
                }
                else
                {
                    return;
                }
            }

            this.Information("Removing JID from set of recognized JIDs: " + FullJID);

            PeerAddressEventHandler h = this.PeerAddressRemoved;

            if (h != null)
            {
                try
                {
                    h(this, new PeerAddressEventArgs(FullJID, string.Empty, 0, string.Empty, 0));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Reports recognized peer addresses.
        /// </summary>
        /// <param name="FullJID">XMPP Address (full JID).</param>
        /// <param name="ExternalIp">External IP address.</param>
        /// <param name="ExternalPort">External Port number.</param>
        /// <param name="LocalIp">Local IP address.</param>
        /// <param name="LocalPort">Local Port number.</param>
        public void ReportPeerAddresses(string FullJID, string ExternalIp, int ExternalPort, string LocalIp, int LocalPort)
        {
            Dictionary <int, AddressInfo> Infos;
            string ThisExternalIp;

            if (this.p2pNetwork.ExternalAddress is null)
            {
                ThisExternalIp = string.Empty;
            }
            else
            {
                ThisExternalIp = this.p2pNetwork.ExternalAddress.ToString();
            }

            lock (this.addressesByFullJid)
            {
                if (this.addressesByFullJid.TryGetValue(FullJID, out AddressInfo Info))
                {
                    if (Info.ExternalIp == ExternalIp && Info.ExternalPort == ExternalPort &&
                        Info.LocalIp == LocalIp && Info.LocalPort == LocalPort)
                    {
                        return;
                    }

                    if (Info.ExternalIp != ExternalIp)
                    {
                        if (this.addressesByExternalIPPort.TryGetValue(Info.ExternalIp, out Infos))
                        {
                            if (Infos.Remove(Info.ExternalPort) && Infos.Count == 0)
                            {
                                this.addressesByExternalIPPort.Remove(Info.ExternalIp);
                            }
                        }
                    }

                    if (ExternalIp == ThisExternalIp && Info.LocalIp != LocalIp)
                    {
                        if (this.addressesByLocalIPPort.TryGetValue(Info.LocalIp, out Infos))
                        {
                            if (Infos.Remove(Info.LocalPort) && Infos.Count == 0)
                            {
                                this.addressesByLocalIPPort.Remove(Info.LocalIp);
                            }
                        }
                    }
                }

                Info = new AddressInfo(FullJID, ExternalIp, ExternalPort, LocalIp, LocalPort);
                this.addressesByFullJid[FullJID] = Info;

                if (!this.addressesByExternalIPPort.TryGetValue(ExternalIp, out Infos))
                {
                    Infos = new Dictionary <int, AddressInfo>();
                    this.addressesByExternalIPPort[ExternalIp] = Infos;
                }

                Infos[ExternalPort] = Info;

                if (ExternalIp == ThisExternalIp)
                {
                    if (!this.addressesByLocalIPPort.TryGetValue(LocalIp, out Infos))
                    {
                        Infos = new Dictionary <int, AddressInfo>();
                        this.addressesByLocalIPPort[LocalIp] = Infos;
                    }

                    Infos[LocalPort] = Info;
                }
            }

            this.Information("P2P information available for " + FullJID + ". External: " + ExternalIp + ":" + ExternalPort.ToString() +
                             ", Local: " + LocalIp + ":" + LocalPort.ToString());

            PeerAddressEventHandler h = this.PeerAddressReceived;

            if (h != null)
            {
                try
                {
                    h(this, new PeerAddressEventArgs(FullJID, ExternalIp, ExternalPort, LocalIp, LocalPort));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
        }