peerdetector.cs

来自「破解的飞信源代码」· CS 代码 · 共 420 行 · 第 1/2 页

CS
420
字号
            catch
            {
            }
        }

        private void FirePeerAccepted(PeerAcceptedEventArgs e)
        {
            if (this.PeerAccepted != null)
            {
                try
                {
                    this.PeerAccepted.Invoke(this, e);
                }
                catch
                {
                }
            }
        }

        private void FirePeerFound(PeerFoundEventArgs e)
        {
            if (this.PeerFound != null)
            {
                try
                {
                    if (e.RemotePeer.FirstEndPoint.Port > 0)
                    {
                        this.PeerFound.Invoke(this, e);
                    }
                }
                catch
                {
                }
            }
        }

        private static void GenerateKey(SymmetricAlgorithm sa, byte[] a)
        {
            using (MD5 md = MD5.Create())
            {
                byte[] src = md.ComputeHash(a);
                byte[] dst = new byte[sa.Key.Length];
                Buffer.BlockCopy(src, 1, dst, 0, dst.Length);
                sa.Key = dst;
            }
        }

        private void InitMcastSockets()
        {
            this.CloseAllMCastSockets();
            this.mcastEelements.Clear();
            foreach (NetworkInterface interface2 in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (((interface2.get_NetworkInterfaceType() != 0x18) && (interface2.get_OperationalStatus() == 1)) && (interface2.get_SupportsMulticast() && interface2.Supports(0)))
                {
                    using (IEnumerator<UnicastIPAddressInformation> enumerator = interface2.GetIPProperties().get_UnicastAddresses().GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            UnicastIPAddressInformation information = enumerator.get_Current();
                            if (IsLanAddress(information.get_Address()))
                            {
                                try
                                {
                                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                                    socket.Bind(new IPEndPoint(information.get_Address(), 0x2af8));
                                    MulticastOption optionValue = new MulticastOption(mcastEndPoint.Address, information.get_Address());
                                    socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, optionValue);
                                    socket.set_MulticastLoopback(false);
                                    this.mcastEelements.Add(socket, this.ComputeRegData(information.get_Address(), this.tcpPort));
                                    continue;
                                }
                                catch (Exception exception)
                                {
                                    ClientLogger.WriteGeneral("加入多播组失败", exception.ToString());
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }

        private static bool IsLanAddress(IPAddress address)
        {
            byte[] addressBytes = address.GetAddressBytes();
            return ((addressBytes[0] == 10) || ((((addressBytes[0] == 0xac) && (addressBytes[1] >= 0x10)) && (addressBytes[1] <= 0x1f)) || ((addressBytes[0] == 0xc0) && (addressBytes[1] == 0xa8))));
        }

        private void OnNetworkAddressChanged(object sender, EventArgs e)
        {
            try
            {
                lock (this.mcastEelements)
                {
                    this.InitMcastSockets();
                }
            }
            catch (Exception exception)
            {
                ClientLogger.WriteException("网络地址变化时重新初始化多播失败", exception);
            }
        }

        private void ProcessReadySockets(byte[] buffer, IEnumerable<Socket> reads)
        {
            using (IEnumerator<Socket> enumerator = reads.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Socket socket = enumerator.get_Current();
                    try
                    {
                        if (socket == this.tcpListener)
                        {
                            this.DoAccept();
                        }
                        else
                        {
                            int length = socket.Receive(buffer);
                            Peer peer = Decrypt(buffer, length);
                            if (peer != null)
                            {
                                this.FirePeerFound(new PeerFoundEventArgs(peer, ((IPEndPoint) socket.LocalEndPoint).Address));
                            }
                        }
                        continue;
                    }
                    catch (Exception exception)
                    {
                        ClientLogger.WriteGeneral("ProcessReadySockets failed.", exception.ToString(), 10);
                        continue;
                    }
                }
            }
        }

        public void Register()
        {
            this.InitMcastSockets();
            this.SendRegMessage();
            NetworkChange.add_NetworkAddressChanged(new NetworkAddressChangedEventHandler(this, (IntPtr) this.OnNetworkAddressChanged));
            this.detectThread.IsBackground = true;
            this.detectThread.Start();
        }

        private void SendRegMessage()
        {
            if (this.lastSendTime.get_IsRunning())
            {
                if (this.lastSendTime.get_ElapsedMilliseconds() < 0x7530)
                {
                    return;
                }
                this.lastSendTime.Reset();
            }
            else
            {
                this.lastSendTime.Start();
            }
            lock (this.mcastEelements)
            {
                Dictionary<Socket, byte[]>.Enumerator enumerator = this.mcastEelements.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        KeyValuePair<Socket, byte[]> pair = enumerator.get_Current();
                        Socket socket = pair.get_Key();
                        try
                        {
                            socket.SendTo(pair.get_Value(), mcastEndPoint);
                            continue;
                        }
                        catch (Exception exception)
                        {
                            ClientLogger.WriteGeneral("发送多播数据包失败.", exception.ToString(), 10);
                            continue;
                        }
                    }
                }
                finally
                {
                    enumerator.Dispose();
                }
            }
        }

        public void Unregister()
        {
            try
            {
                this.PeerFound = null;
                this.stop = true;
                NetworkChange.remove_NetworkAddressChanged(new NetworkAddressChangedEventHandler(this, (IntPtr) this.OnNetworkAddressChanged));
                this.encryptor.Dispose();
                this.detectThread.Join(0x3d0900);
                this.CloseAllMCastSockets();
            }
            catch (Exception exception)
            {
                ClientLogger.WriteGeneral("Unregister failed.", exception.ToString(), 10);
            }
        }
    }
}

⌨️ 快捷键说明

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