// TODO onDropped!
        #endregion


        #region Methods

        internal void OnTick()
        {
            if (!Manager.Settings.AllowPeerExchange)
            {
                return;
            }

            int len = (addedPeers.Count <= MAX_PEERS) ? addedPeers.Count : MAX_PEERS;

            byte[] added     = new byte[len * 6];
            byte[] addedDotF = new byte[len];
            for (int i = 0; i < len; i++)
            {
                addedPeers[i].CompactPeer(added.AsSpan(i * 6, 6));
                if (EncryptionTypes.SupportsRC4(addedPeers[i].AllowedEncryption))
                {
                    addedDotF[i] = 0x01;
                }
                else
                {
                    addedDotF[i] = 0x00;
                }

                addedDotF[i] |= (byte)(addedPeers[i].IsSeeder ? 0x02 : 0x00);
            }
            addedPeers.RemoveRange(0, len);

            len = Math.Min(MAX_PEERS - len, droppedPeers.Count);

            byte[] dropped = new byte[len * 6];
            for (int i = 0; i < len; i++)
            {
                droppedPeers[i].CompactPeer(dropped.AsSpan(i * 6, 6));
            }

            droppedPeers.RemoveRange(0, len);

            (var message, var releaser) = PeerMessage.Rent <PeerExchangeMessage> ();
            message.Initialize(id.ExtensionSupports, new ReadOnlyMemory <byte> (added), new ReadOnlyMemory <byte> (addedDotF), new ReadOnlyMemory <byte> (dropped));
            id.MessageQueue.Enqueue(message, releaser);
        }
Example #2
0
        // TODO onDropped!
        #endregion


        #region Methods

        internal void OnTick()
        {
            if (!Manager.Settings.AllowPeerExchange)
            {
                return;
            }

            int len = (addedPeers.Count <= MAX_PEERS) ? addedPeers.Count : MAX_PEERS;

            byte[] added     = new byte[len * 6];
            byte[] addedDotF = new byte[len];
            for (int i = 0; i < len; i++)
            {
                addedPeers[i].CompactPeer(added, i * 6);
                if (EncryptionTypes.SupportsRC4(addedPeers[i].AllowedEncryption))
                {
                    addedDotF[i] = 0x01;
                }
                else
                {
                    addedDotF[i] = 0x00;
                }

                addedDotF[i] |= (byte)(addedPeers[i].IsSeeder ? 0x02 : 0x00);
            }
            addedPeers.RemoveRange(0, len);

            len = Math.Min(MAX_PEERS - len, droppedPeers.Count);

            byte[] dropped = new byte[len * 6];
            for (int i = 0; i < len; i++)
            {
                droppedPeers[i].CompactPeer(dropped, i * 6);
            }

            droppedPeers.RemoveRange(0, len);
            id.MessageQueue.Enqueue(new PeerExchangeMessage(id, added, addedDotF, dropped));
        }