<commit_before>using System;
using System.Collections.Generic;
using System.Text;
using MonoTorrent.Common;
using MonoTorrent.Client.Encryption;
using System.Net.Sockets;
using MonoTorrent.Client.Messages.Standard;
using MonoTorrent.Client.Messages;

namespace MonoTorrent.Client
{
    /// <summary>
    /// Instance methods of this class are threadsafe
    /// </summary>
    public class ListenManager : IDisposable
    {
        #region old members

        private AsyncCallback peerHandshakeReceived; // The callback to invoke when we receive a peer handshake.

        #endregion old members


        #region Member Variables

        private object locker;
        private ClientEngine engine;
        private MonoTorrentCollection<ConnectionListenerBase> listeners;
        private AsyncCallback endCheckEncryptionCallback;

        #endregion Member Variables


        #region Properties

        public MonoTorrentCollection<ConnectionListenerBase> Listeners
        {
            get { return listeners; }
        }

        internal object Locker
        {
            get { return locker; }
            private set { locker = value; }
        }

        internal ClientEngine Engine
        {
            get { return engine; }
            private set { engine = value; }
        }

        #endregion Properties


        #region Constructors

        internal ListenManager(ClientEngine engine)
        {
            Engine = engine;
            Locker = new object();
            listeners = new MonoTorrentCollection<ConnectionListenerBase>();
            listeners.IsReadOnly = true;
            peerHandshakeReceived = new AsyncCallback(onPeerHandshakeReceived);
            endCheckEncryptionCallback = EndCheckEncryption;
        }

        #endregion Constructors


        #region Public Methods

        public void Dispose()
        {
            for (int i = 0; i < listeners.Count; i++)
                listeners[i].Dispose();
        }

        public void Register(ConnectionListenerBase listener)
        {
            lock (Locker)
            {
                if (listener.Engine != null)
                    throw new ListenerException("This listener is registered to a different engine");

                if (listeners.Contains(listener))
                    throw new ListenerException("This listener has already been registered with the manager");

                listener.Engine = engine;
                listeners.Add(listener, true);
                listener.ConnectionReceived += new EventHandler<NewConnectionEventArgs>(ConnectionReceived);
            }
        }

        public void Unregister(ConnectionListenerBase listener)
        {
            lock (Locker)
            {
                if (listener.Engine != this.engine)
                    throw new ListenerException("This listener is registered to a different engine");

                if (!listeners.Contains(listener))
                    throw new ListenerException("This listener has not been registered with the manager");

                listener.Engine = null;
                listeners.Remove(listener, true);
                listener.ConnectionReceived -= new EventHandler<NewConnectionEventArgs>(ConnectionReceived);
            }
        }

        #endregion Public Methods




        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            PeerIdInternal id = new PeerIdInternal(e.Peer, e.TorrentManager);
            id.Connection = new PeerConnectionBase(0);
            id.Connection.Connection = e.Connection;

            Logger.Log(id.Connection.Connection, "ListenManager - ConnectionReceived");

            if (id.Connection.Connection.IsIncoming)
            {
                ClientEngine.BufferManager.GetBuffer(ref id.Connection.recieveBuffer, 68);
                id.Connection.BytesReceived = 0;
                id.Connection.BytesToRecieve = 68;
                EncryptorFactory.BeginCheckEncryption(id, endCheckEncryptionCallback, id);
            }
            else
                id.ConnectionManager.ProcessFreshConnection(id);
        }

        private void EndCheckEncryption(IAsyncResult result)
        {
            PeerIdInternal id = (PeerIdInternal)result.AsyncState;
            try
            {
                byte[] initialData;
                SocketError error;
                EncryptorFactory.EndCheckEncryption(result, out initialData);

                if(initialData == null)<commit_msg>Propagate sKeys from the socket listener<commit_after>using System;
using System.Collections.Generic;
using System.Text;
using MonoTorrent.Common;
using MonoTorrent.Client.Encryption;
using System.Net.Sockets;
using MonoTorrent.Client.Messages.Standard;
using MonoTorrent.Client.Messages;

namespace MonoTorrent.Client
{
    /// <summary>
    /// Instance methods of this class are threadsafe
    /// </summary>
    public class ListenManager : IDisposable
    {
        #region old members

        private AsyncCallback peerHandshakeReceived; // The callback to invoke when we receive a peer handshake.

        #endregion old members


        #region Member Variables

        private object locker;
        private ClientEngine engine;
        private MonoTorrentCollection<ConnectionListenerBase> listeners;
        private AsyncCallback endCheckEncryptionCallback;

        #endregion Member Variables


        #region Properties

        public MonoTorrentCollection<ConnectionListenerBase> Listeners
        {
            get { return listeners; }
        }

        internal object Locker
        {
            get { return locker; }
            private set { locker = value; }
        }

        internal ClientEngine Engine
        {
            get { return engine; }
            private set { engine = value; }
        }

        #endregion Properties


        #region Constructors

        internal ListenManager(ClientEngine engine)
        {
            Engine = engine;
            Locker = new object();
            listeners = new MonoTorrentCollection<ConnectionListenerBase>();
            listeners.IsReadOnly = true;
            peerHandshakeReceived = new AsyncCallback(onPeerHandshakeReceived);
            endCheckEncryptionCallback = EndCheckEncryption;
        }

        #endregion Constructors


        #region Public Methods

        public void Dispose()
        {
            for (int i = 0; i < listeners.Count; i++)
                listeners[i].Dispose();
        }

        public void Register(ConnectionListenerBase listener)
        {
            lock (Locker)
            {
                if (listener.Engine != null)
                    throw new ListenerException("This listener is registered to a different engine");

                if (listeners.Contains(listener))
                    throw new ListenerException("This listener has already been registered with the manager");

                listener.Engine = engine;
                listeners.Add(listener, true);
                listener.ConnectionReceived += new EventHandler<NewConnectionEventArgs>(ConnectionReceived);
            }
        }

        public void Unregister(ConnectionListenerBase listener)
        {
            lock (Locker)
            {
                if (listener.Engine != this.engine)
                    throw new ListenerException("This listener is registered to a different engine");

                if (!listeners.Contains(listener))
                    throw new ListenerException("This listener has not been registered with the manager");

                listener.Engine = null;
                listeners.Remove(listener, true);
                listener.ConnectionReceived -= new EventHandler<NewConnectionEventArgs>(ConnectionReceived);
            }
        }

        #endregion Public Methods




        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            PeerIdInternal id = new PeerIdInternal(e.Peer, e.TorrentManager);
            id.Connection = new PeerConnectionBase(0);
            id.Connection.Connection = e.Connection;

            Logger.Log(id.Connection.Connection, "ListenManager - ConnectionReceived");

            if (id.Connection.Connection.IsIncoming)
            {
                ClientEngine.BufferManager.GetBuffer(ref id.Connection.recieveBuffer, 68);
                id.Connection.BytesReceived = 0;
                id.Connection.BytesToRecieve = 68;
                List<byte[]> skeys = new List<byte[]>();
                using (new ReaderLock(engine.torrentsLock))
                    for (int i = 0; i < engine.Torrents.Count; i++)
                        skeys.Add(engine.Torrents[i].Torrent.InfoHash);
                EncryptorFactory.BeginCheckEncryption(id, endCheckEncryptionCallback, id, skeys.ToArray());
            }
            else
                id.ConnectionManager.ProcessFreshConnection(id);
        }

        private void EndCheckEncryption(IAsyncResult result)
        {
            PeerIdInternal id = (PeerIdInternal)result.AsyncState;
            try
            {
                byte[] initialData;
                SocketError error;
                EncryptorFactory.EndCheckEncryption(result, out initialData);

                if(initialData == null)
{'fraction_non_alphanumeric': 0.07083647324792766, 'fraction_numerical': 0.0016148132199375605, 'mean_word_length': 1.9352290679304898, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2274290', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2046, 'n_words': 550}