Пример #1
0
        public virtual bool Init(ServerExecuteType executeType, string version, string name = null)
        {
            Running = new AtomicBool();
            Stopped = new AtomicBool();

            ExecuteType = executeType;
            Name = string.IsNullOrEmpty(name) == true ? Process.GetCurrentProcess().ProcessName : name;
            Version = version;
            ModuleName = Name;

            CreateLogger();

            Logger.Info("Init");

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            if (LoadServerConfig() == false)
            {
                if (ExecuteType == ServerExecuteType.Console)
                {
                    Console.ReadKey();
                }
                return false;
            }

            TheadPoolEx.SetMinMaxThreads(
                m_ServerConfig.MinWorkerThreads == 0 ? Environment.ProcessorCount : m_ServerConfig.MinWorkerThreads,
                m_ServerConfig.MaxWorkerThreads == 0 ? Environment.ProcessorCount * 2: m_ServerConfig.MaxWorkerThreads,
                m_ServerConfig.MinCompletionPortThreads == 0 ? Environment.ProcessorCount : m_ServerConfig.MinCompletionPortThreads,
                m_ServerConfig.MaxCompletionPortThreads == 0 ? Environment.ProcessorCount * 2 : m_ServerConfig.MaxCompletionPortThreads);

            m_Sessions = new ConcurrentDictionary<long, Session>();
            m_Listeners = new List<Listener>();

            try
            {
                int bufferSize = m_ServerConfig.ReceiveBufferSize;

                if (bufferSize <= 0)
                    bufferSize = 1024 * 4;

                // Send, Recv
                m_BufferManager = new BufferManager(bufferSize * m_ServerConfig.MaxConnectionNumber * 2, bufferSize);

                try
                {
                    m_BufferManager.InitBuffer();

                    int[] poolSizes = new int[] { 4096, 16, 128, 256, 1024 };
                    m_PooledBufferManager = new PooledBufferManager(poolSizes);

                    {
                        SocketAsyncEventArgs socketEventArg;
                        var socketArgsList = new List<SocketAsyncEventArgs>(m_ServerConfig.MaxConnectionNumber);

                        for (int i = 0; i < m_ServerConfig.MaxConnectionNumber; i++)
                        {
                            socketEventArg = new SocketAsyncEventArgs();
                            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(CompletedReceive);
                            m_BufferManager.SetBuffer(socketEventArg);
                            socketArgsList.Add(socketEventArg);
                        }
                        m_RecvSAEAPool = new ConcurrentStack<SocketAsyncEventArgs>(socketArgsList);
                    }

                    {
                        SocketAsyncEventArgs socketEventArg;
                        var socketArgsList = new List<SocketAsyncEventArgs>(m_ServerConfig.MaxConnectionNumber);
                        for (int i = 0; i < m_ServerConfig.MaxConnectionNumber; i++)
                        {
                            socketEventArg = new SocketAsyncEventArgs();
                            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(CompletedSend);
                            //m_BufferManager.SetBuffer(socketEventArg);
                            // Send할때 별도의 풀을 사용할거라서
                            socketEventArg.SetBuffer(null, 0, 0);
                            socketArgsList.Add(socketEventArg);
                        }
                        m_SendSAEAPool = new ConcurrentStack<SocketAsyncEventArgs>(socketArgsList);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("메모리 부족! 최대 접속 허용 인원 설정을 낮추세요", e);
                    if (ExecuteType == ServerExecuteType.Console)
                    {
                        Console.ReadKey();
                    }
                    return false;
                }

                m_Updater = new TimerThread(Update, null, 1);

                return true;
            }
            catch (Exception e)
            {
                Logger.Error("Server.Init", e);
                if (ExecuteType == ServerExecuteType.Console)
                {
                    Console.ReadKey();
                }
                return false;
            }
        }
Пример #2
0
        public virtual void ServerStop()
        {
            if (Stopped.SetTrue() == false)
                return;

            Logger.Info("ServerStop");

            for (var i = 0; i < m_Listeners.Count; i++)
            {
                var listener = m_Listeners[i];

                listener.Stop();
            }

            m_Listeners.Clear();

            SocketAsyncEventArgs eventArgs;
            while (m_RecvSAEAPool.Count > 0)
            {
                if (m_RecvSAEAPool.TryPop(out eventArgs))
                    eventArgs.Dispose();
            }
            while (m_SendSAEAPool.Count > 0)
            {
                if (m_SendSAEAPool.TryPop(out eventArgs))
                    eventArgs.Dispose();
            }
            while (m_AcceptSAEAPool.Count > 0)
            {
                if (m_AcceptSAEAPool.TryPop(out eventArgs))
                    eventArgs.Dispose();
            }
            m_RecvSAEAPool = null;
            m_SendSAEAPool = null;
            m_AcceptSAEAPool = null;
            m_BufferManager = null;

            Running.ForceFalse();

            m_MainThreadStopEvent.Set();
            m_MainThreadStopEvent.Close();
            m_MainThreadStopEvent.Dispose();
            m_MainThreadStopEvent = null;

            m_Updater.Stop();

            Thread.Sleep(100);

            m_MainThread = null;
        }