Esempio n. 1
0
        public virtual void Initialize(IAppSession appSession)
        {
            AppSession = appSession;
            Config     = appSession.Config;
            SyncSend   = Config.SyncSend;

            if (m_SendingQueuePool == null)
            {
                m_SendingQueuePool = ((SocketServerBase)((ISocketServerAccessor)appSession.AppServer).SocketServer).SendingQueuePool;
            }

            SendingQueue queue;

            if (m_SendingQueuePool.TryGet(out queue))
            {
                m_SendingQueue = queue;
                queue.StartEnqueue();
            }

            if (Config.CollectSendIntervalMillSec > 0)
            {
                CollectSendBuffer = new ReuseLockBaseBuffer(Config.ReceiveBufferSize);
                SyncSend          = true;
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public virtual void Initialize(IAppSession appSession)
        {
            AppSession         = appSession;
            Config             = appSession.Config;
            SyncSend           = Config.SyncSend;
            m_SendingQueuePool = ((SocketServerBase)((ISocketServerAccessor)appSession.AppServer).SocketServer).SendingQueuePool;

            SendingQueue queue;

            if (m_SendingQueuePool.TryGet(out queue))
            {
                m_SendingQueue = queue;
                queue.StartEnqueue();
            }
        }
        public AsyncSocketSession(Socket client, SocketProxy socketAsyncProxy, ISmartPool <SendingQueue> sendingPool)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            SessionID      = Guid.NewGuid().ToString();
            m_Client       = client;
            LocalEndPoint  = (IPEndPoint)client.LocalEndPoint;
            RemoteEndPoint = (IPEndPoint)client.RemoteEndPoint;

            m_SendingQueuePool = sendingPool;
            SocketAsyncProxy   = socketAsyncProxy;
            m_IsReset          = false;
        }
Esempio n. 5
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;
            }
        }
Esempio n. 6
0
        public virtual void Initialize(IAppSession appSession)
        {
            AppSession = appSession;
            Config = appSession.Config;
            SyncSend = Config.SyncSend;
            m_SendingQueuePool = ((SocketServerBase)((ISocketServerAccessor)appSession.AppServer).SocketServer).SendingQueuePool;

            SendingQueue queue;
            if (m_SendingQueuePool.TryGet(out queue))
            {
                m_SendingQueue = queue;
                queue.StartEnqueue();
            }
        }
Esempio n. 7
0
 public void InitializeSendingQueue(ISmartPool <SendingQueue> pool)
 {
     m_SendingQueuePool = pool;
 }