⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listener.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace Imps.Client.Core.P2P.BlockingTransportor
{
    using Imps.Client.Core;
    using Imps.Client.Core.P2P.ICE;
    using NCindy.Util.Logging;
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;

    public class Listener
    {
        private const int DefaultBacklog = 10;
        private PeerDetector detector;
        private readonly long localSid;
        private readonly User localUser;
        private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
        private readonly Dictionary<long, PeerFoundEventArgs> nearPeers = new Dictionary<long, PeerFoundEventArgs>();
        private readonly Dictionary<Guid, ShareContent> openingShareContent = new Dictionary<Guid, ShareContent>();
        private Socket tcpListener;

        public Listener(User user)
        {
            this.localUser = user;
            this.localSid = this.localUser.Sid;
        }

        public void AddShareContent(Guid id, ShareContent sc)
        {
            if (!this.openingShareContent.ContainsKey(id))
            {
                lock (this.openingShareContent)
                {
                    if (!this.openingShareContent.ContainsKey(id))
                    {
                        this.openingShareContent.Add(id, sc);
                    }
                }
            }
        }

        public Peer IsInSameLan(Peer remote)
        {
            lock (this.nearPeers)
            {
                if (this.nearPeers.ContainsKey(remote.Sid))
                {
                    PeerFoundEventArgs args = this.nearPeers.get_Item(remote.Sid);
                    TimeSpan span = (TimeSpan) (DateTime.Now - args.PeerFoundTime);
                    if (span.TotalMilliseconds > 90000)
                    {
                        this.nearPeers.Remove(remote.Sid);
                        return null;
                    }
                    return args.RemotePeer;
                }
            }
            return null;
        }

        private void OnPeerAccepted(object sender, PeerAcceptedEventArgs e)
        {
            try
            {
                Guid id = e.Token;
                if (this.openingShareContent.ContainsKey(id))
                {
                    this.openingShareContent.get_Item(id).TcpP2PSocket = e.TranSocket;
                    this.RemoveShareContent(id);
                }
            }
            catch
            {
            }
        }

        private void OnPeerFound(object sender, PeerFoundEventArgs e)
        {
            lock (this.nearPeers)
            {
                Contact contact = this.localUser.ContactList.FindContactBySid((int) e.RemotePeer.Sid);
                if ((contact != null) && contact.EnableSendFile)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Peer found, Peer info [" + e.RemotePeer + "]");
                    }
                    this.nearPeers.set_Item(e.RemotePeer.Sid, e);
                }
            }
        }

        public void RemoveShareContent(Guid id)
        {
            if (this.openingShareContent.ContainsKey(id))
            {
                lock (this.openingShareContent)
                {
                    if (this.openingShareContent.ContainsKey(id))
                    {
                        this.openingShareContent.Remove(id);
                    }
                }
            }
        }

        public void Start()
        {
            try
            {
                if ((this.tcpListener == null) && (((Environment.OSVersion.Platform != PlatformID.Win32NT) || (Environment.OSVersion.Version.Major != 5)) || (Environment.OSVersion.Version.Minor != 0)))
                {
                    this.tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    this.tcpListener.Bind(new IPEndPoint(IPAddress.Any, 0));
                    this.tcpListener.Listen(10);
                    this.detector = new PeerDetector(this.tcpListener, this.localSid);
                    this.detector.PeerFound += new EventHandler<PeerFoundEventArgs>(this, (IntPtr) this.OnPeerFound);
                    this.detector.PeerAccepted += new EventHandler<PeerAcceptedEventArgs>(this, (IntPtr) this.OnPeerAccepted);
                    this.detector.Register();
                }
            }
            catch
            {
            }
        }

        public void Stop()
        {
            try
            {
                if (this.detector != null)
                {
                    this.detector.Unregister();
                }
                if (this.tcpListener != null)
                {
                    this.tcpListener.Close();
                    this.tcpListener = null;
                }
            }
            catch
            {
            }
            finally
            {
                if (this.nearPeers != null)
                {
                    this.nearPeers.Clear();
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -