예제 #1
0
        public bool Start()
        {
            try
            {
                // 预热数据接收buffer
                int bufferSize = ServerConfig.DefaultReceiveBufferSize;
                if (bufferSize <= 0)
                {
                    bufferSize = 1024 * 4;
                }
                m_ReceiveBufferManager = new ReceiveBuffer(bufferSize * ServerConfig.DefaultMaxConnectionNumber, bufferSize);
                try
                {
                    m_ReceiveBufferManager.InitBuffer();
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to allocate buffer for async socket communication, may because there is no enough memory, please decrease maxConnectionNumber in configuration!", e);
                    return(false);
                }

                // 预热socket连接池
                SocketAsyncEventArgs socketEventArg;
                var socketArgsProxyList = new List <SocketProxy>(ServerConfig.DefaultMaxConnectionNumber);
                for (int i = 0; i < ServerConfig.DefaultMaxConnectionNumber; i++)
                {
                    socketEventArg = new SocketAsyncEventArgs();
                    m_ReceiveBufferManager.SetBuffer(socketEventArg);
                    socketArgsProxyList.Add(new SocketProxy(socketEventArg));
                }
                m_SocketPool = new ConcurrentStack <SocketProxy>(socketArgsProxyList);


                // 预热数据发送buffer
                var sendingQueuePool = new SmartPool <SendingQueue>();
                sendingQueuePool.Initialize(Math.Max(ServerConfig.DefaultMaxConnectionNumber / 6, 256),
                                            Math.Max(ServerConfig.DefaultMaxConnectionNumber * 2, 256),
                                            new SendingQueueSourceCreator(ServerConfig.DefaultSendingQueueSize));
                SendingQueuePool = sendingQueuePool;

                // 打开监听socket
                if (!InitListeners())
                {
                    return(false);
                }

                IsRunning = true;
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
예제 #2
0
        public void Stop()
        {
            if (IsStopped)
            {
                return;
            }
            lock (SyncRoot)
            {
                if (IsStopped)
                {
                    return;
                }

                IsStopped = true;

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

                    listener.Stop();
                }

                Listeners.Clear();

                SendingQueuePool = null;

                IsRunning = false;

                foreach (var item in m_SocketPool)
                {
                    item.SocketEventArgs.Dispose();
                }

                m_SocketPool           = null;
                m_ReceiveBufferManager = null;
                IsRunning = false;
            }
        }