Пример #1
0
 public void Release(CSocket socket)
 {
     if (!socket.Connected)
     {
         logger.Warn("socket Destroyed,reason: disconnected!host:" + endPoint.Address);
         Destroy(socket);
     }
     else if (socketPoolConfig.AutoShrink && pool.Shrink())
     {
         logger.Info("socket Destroyed,reason: auto shrink!host:" + endPoint.Address);
         Destroy(socket);
     }
     else
     {
         pool.Enqueue(socket);
         releasedEvent.Set();
     }
 }
Пример #2
0
 public void Destroy(CSocket socket)
 {
     try
     {
         socket.Disconnect(false);
     }
     catch (Exception err)
     {
         logger.Error("socket destroy error!host:" + endPoint.Address + ",message:" + err.Message);
     }
     finally
     {
         if (pool != null)
         {
             pool.AllSocket.Remove(socket);
             pool.AllSocket.Remove(null);
         }
         socket = null;
     }
 }
Пример #3
0
        public CSocket GetScoket()
        {
            CSocket rSocket = null;

            lock (pool)
            {
                if (pool.Count > 0)
                {
                    rSocket = pool.Dequeue();
                }
                else if (pool.Total < socketPoolConfig.MaxPoolSize)
                {
                    var socket = new CSocket(endPoint, this, socketPoolConfig);
                    pool.AllSocket.Add(socket);
                    rSocket = socket;
                    logger.Info("created new socket.server:" + _Server.Name);
                }
                else
                {
                    if (!releasedEvent.WaitOne((int)socketPoolConfig.WaitTimeout.TotalMilliseconds))
                    {
                        logger.Error("socket connection pool is full(" + pool.Total + ")!server cu:" + _Server.CurrUserCount);
                        throw new TimeoutException("socket connection pool is full!");
                    }
                    else
                    {
                        rSocket = pool.Dequeue();
                    }
                }
            }
            if (rSocket == null)
            {
                throw new Exception("can't get socket from pool!");
            }
            rSocket.Reset();
            return(rSocket);
        }