Esempio n. 1
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 is null))
            {
                try
                {
                    h(this, new PeerAddressEventArgs(FullJID, ExternalIp, ExternalPort, LocalIp, LocalPort));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
        }