<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using SuperSocket.Common;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase;


namespace SuperSocket.SocketEngine
{
    /// <summary>
    /// The core socket server which can run any SocketSession
    /// </summary>
    /// <typeparam name="T">The typeof the SocketSession</typeparam>
    class SyncSocketServer<TAppSession, TCommandInfo> : TcpSocketServerBase<SyncSocketSession<TAppSession, TCommandInfo>, TAppSession>
        where TAppSession : IAppSession, IAppSession<TAppSession, TCommandInfo>, new()
        where TCommandInfo : ICommandInfo
    {
        public SyncSocketServer(IAppServer<TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol<TCommandInfo> protocol)
            : base(appServer, localEndPoint)
        {
            m_Protocol = protocol;
        }

        private ICustomProtocol<TCommandInfo> m_Protocol;

        private Socket m_ListenSocket = null;

        private Thread m_ListenThread = null;

        private Semaphore m_MaxConnectionSemaphore;

        /// <summary>
        /// Starts the server
        /// </summary>
        public override bool Start()
        {
            try
            {
                if (!base.Start())
                    return false;

                if (m_ListenSocket == null)
                {
                    m_ListenThread = new Thread(StartListen);
                    m_ListenThread.Start();
                }

                WaitForStartupFinished();
                return IsRunning;
            }
            catch (Exception e)
            {
                AppServer.Logger.LogError(e);
                return false;
            }
        }        

        /// <summary>
        /// Starts to listen
        /// </summary>
        private void StartListen()
        {
            m_ListenSocket = new Socket(this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                m_ListenSocket.Bind(this.EndPoint);
                m_ListenSocket.Listen(100);

                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
            }
            catch (Exception e)
            {
                AppServer.Logger.LogError(e);
                OnStartupFinished();
                return;
            }

            m_MaxConnectionSemaphore = new Semaphore(AppServer.Config.MaxConnectionNumber, AppServer.Config.MaxConnectionNumber);

            IsRunning = true;

            OnStartupFinished();

            while (!IsStopped)
            {
                Socket client = null;

                try
                {
                    m_MaxConnectionSemaphore.WaitOne();

                    if (IsStopped)
                        break;

                    client = m_ListenSocket.Accept();
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (NullReferenceException)
                {
                    break;
                }
                catch (Exception e)
                {
                    SocketException se = e as SocketException;
                    if (se != null)
                    {
                        //A blocking operation was interrupted by a call to WSACancelBlockingCall
                        //SocketListener has been stopped normally
                        if (se.ErrorCode == 10004 || se.ErrorCode == 10038)
                            break;
                    }

                    AppServer.Logger.LogError("Socket Listener stopped unexpectly, Socket Address:" + EndPoint.Address.ToString() + ":" + EndPoint.Port, e);
                    break;
                }

                SyncSocketSession<TAppSession, TCommandInfo> session;

                try
                {
                    session = RegisterSession(client, new SyncSocketSession<TAppSession, TCommandInfo>(client, m_Protocol.CreateCommandReader(AppServer)));
                }
                catch (Exception e)
                {
                    AppServer.Logger.LogError(e);
                    continue;
                }

                if (session == null)
                {
                    this.ExecuteAsync(w => client.SafeCloseClientSocket(AppServer.Logger));
                    continue;
                }

                session.Closed += new EventHandler<SocketSessionClosedEventArgs>(session_Closed);

                Thread thUser = new Thread(session.Start);
                thUser.IsBackground = true;
                thUser.Start();
            }

            IsRunning = false;
        }

        void session_Closed(object sender, SocketSessionClosedEventArgs e)
        {
            m_MaxConnectionSemaphore.Release();
        }

        /// <summary>
        /// Stops this server.
        /// </summary>
        public override void Stop()
        {
            base.Stop();

            if (IsRunning)
            {
                if (m_ListenSocket != null)
                {
                    m_ListenSocket.Close();
                    m_ListenSocket = null;
                }<commit_msg>mainline: run connection session in thread from thread pool<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using SuperSocket.Common;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase;


namespace SuperSocket.SocketEngine
{
    /// <summary>
    /// The core socket server which can run any SocketSession
    /// </summary>
    /// <typeparam name="T">The typeof the SocketSession</typeparam>
    class SyncSocketServer<TAppSession, TCommandInfo> : TcpSocketServerBase<SyncSocketSession<TAppSession, TCommandInfo>, TAppSession>
        where TAppSession : IAppSession, IAppSession<TAppSession, TCommandInfo>, new()
        where TCommandInfo : ICommandInfo
    {
        public SyncSocketServer(IAppServer<TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol<TCommandInfo> protocol)
            : base(appServer, localEndPoint)
        {
            m_Protocol = protocol;
        }

        private ICustomProtocol<TCommandInfo> m_Protocol;

        private Socket m_ListenSocket = null;

        private Thread m_ListenThread = null;

        private Semaphore m_MaxConnectionSemaphore;

        /// <summary>
        /// Starts the server
        /// </summary>
        public override bool Start()
        {
            try
            {
                if (!base.Start())
                    return false;

                if (m_ListenSocket == null)
                {
                    m_ListenThread = new Thread(StartListen);
                    m_ListenThread.Start();
                }

                WaitForStartupFinished();
                return IsRunning;
            }
            catch (Exception e)
            {
                AppServer.Logger.LogError(e);
                return false;
            }
        }        

        /// <summary>
        /// Starts to listen
        /// </summary>
        private void StartListen()
        {
            m_ListenSocket = new Socket(this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                m_ListenSocket.Bind(this.EndPoint);
                m_ListenSocket.Listen(100);

                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
            }
            catch (Exception e)
            {
                AppServer.Logger.LogError(e);
                OnStartupFinished();
                return;
            }

            m_MaxConnectionSemaphore = new Semaphore(AppServer.Config.MaxConnectionNumber, AppServer.Config.MaxConnectionNumber);

            IsRunning = true;

            OnStartupFinished();

            while (!IsStopped)
            {
                Socket client = null;

                try
                {
                    m_MaxConnectionSemaphore.WaitOne();

                    if (IsStopped)
                        break;

                    client = m_ListenSocket.Accept();
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (NullReferenceException)
                {
                    break;
                }
                catch (Exception e)
                {
                    SocketException se = e as SocketException;
                    if (se != null)
                    {
                        //A blocking operation was interrupted by a call to WSACancelBlockingCall
                        //SocketListener has been stopped normally
                        if (se.ErrorCode == 10004 || se.ErrorCode == 10038)
                            break;
                    }

                    AppServer.Logger.LogError("Socket Listener stopped unexpectly, Socket Address:" + EndPoint.Address.ToString() + ":" + EndPoint.Port, e);
                    break;
                }

                SyncSocketSession<TAppSession, TCommandInfo> session;

                try
                {
                    session = RegisterSession(client, new SyncSocketSession<TAppSession, TCommandInfo>(client, m_Protocol.CreateCommandReader(AppServer)));
                }
                catch (Exception e)
                {
                    AppServer.Logger.LogError(e);
                    continue;
                }

                if (session == null)
                {
                    this.ExecuteAsync(w => client.SafeCloseClientSocket(AppServer.Logger));
                    continue;
                }

                session.Closed += new EventHandler<SocketSessionClosedEventArgs>(session_Closed);
    
                this.ExecuteAsync(w => session.Start());
            }

            IsRunning = false;
        }

        void session_Closed(object sender, SocketSessionClosedEventArgs e)
        {
            m_MaxConnectionSemaphore.Release();
        }

        /// <summary>
        /// Stops this server.
        /// </summary>
        public override void Stop()
        {
            base.Stop();

            if (IsRunning)
            {
                if (m_ListenSocket != null)
                {
                    m_ListenSocket.Close();
                    m_ListenSocket = null;
                }
{'fraction_non_alphanumeric': 0.07879656160458452, 'fraction_numerical': 0.002328080229226361, 'mean_word_length': 1.3998710786420283, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 46, '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': '5888186', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2564, 'n_words': 543}