コード例 #1
0
ファイル: HostNode.cs プロジェクト: zzgzzgok/cyqdata
 /// <summary>
 /// Returns a socket to the pool.
 /// If the socket is dead, it will be destroyed.
 /// If there are more than MaxPoolSize sockets in the pool, it will be destroyed.
 /// If there are less than MinPoolSize sockets in the pool, it will always be put back.
 /// If there are something inbetween those values, the age of the socket is checked.
 /// If it is older than the SocketRecycleAge, it is destroyed, otherwise it will be
 /// put back in the pool.
 /// </summary>
 internal void Return(MSocket socket)
 {
     //If the socket is dead, destroy it.
     if (!socket.IsAlive || hasDisponse)
     {
         Interlocked.Increment(ref DeadSocketsOnReturn);
         socket.Close();
     }
     else
     {
         //Clean up socket
         socket.Reset();
         //Check pool size.
         if (socketQueue.Count >= maxQueue)
         {
             //If the pool is full, destroy the socket.
             socket.Close();
         }
         else if (socketQueue.Count > minQueue && socket.CreateTime.AddMinutes(30) < DateTime.Now)
         {
             //socket 服务超过半小时的,也可以休息了,只保留最底个数。
             //If we have more than the minimum amount of sockets, but less than the max, and the socket is older than the recycle age, we destroy it.
             socket.Close();
         }
         else
         {
             //Put the socket back in the pool.
             lock (socketQueue)
             {
                 socketQueue.Enqueue(socket);
             }
         }
     }
 }
コード例 #2
0
        internal void Execute(SocketPool pool, UseSocket use)
        {
            MSocket sock = null;

            try
            {
                //Acquire a socket
                sock = pool.Acquire();

                //Use the socket as a parameter to the delegate and return its result.
                if (sock != null)
                {
                    use(sock);
                }
            }
            catch (Exception e)
            {
                logger.Error("Error in Execute: " + pool.Host, e);

                //Socket is probably broken
                if (sock != null)
                {
                    sock.Close();
                }
            }
            finally
            {
                if (sock != null)
                {
                    sock.Dispose();
                }
            }
        }
コード例 #3
0
        internal T Execute <T>(SocketPool pool, T defaultValue, UseSocket <T> use)
        {
            MSocket sock = null;

            try
            {
                //Acquire a socket
                sock = pool.Acquire();

                //Use the socket as a parameter to the delegate and return its result.
                if (sock != null)
                {
                    return(use(sock));
                }
            }
            catch (Exception e)
            {
                logger.Error("Error in Execute<T>: " + pool.Host, e);

                //Socket is probably broken
                if (sock != null)
                {
                    sock.Close();
                }
            }
            finally
            {
                if (sock != null)
                {
                    sock.Dispose();
                }
            }
            return(defaultValue);
        }
コード例 #4
0
ファイル: SocketPool.cs プロジェクト: yanshen6/cyqdata
        /// <summary>
        /// Returns a socket to the pool.
        /// If the socket is dead, it will be destroyed.
        /// If there are more than MaxPoolSize sockets in the pool, it will be destroyed.
        /// If there are less than MinPoolSize sockets in the pool, it will always be put back.
        /// If there are something inbetween those values, the age of the socket is checked.
        /// If it is older than the SocketRecycleAge, it is destroyed, otherwise it will be
        /// put back in the pool.
        /// </summary>
        internal void Return(MSocket socket)
        {
            //If the socket is dead, destroy it.
            if (!socket.IsAlive)
            {
                Interlocked.Increment(ref deadsocketsonreturn);
                socket.Close();
            }
            else
            {
                //Clean up socket
                if (socket.Reset())
                {
                    Interlocked.Increment(ref dirtysocketsonreturn);
                }

                //Check pool size.
                if (queue.Count >= owner.MaxPoolSize)
                {
                    //If the pool is full, destroy the socket.
                    socket.Close();
                }
                else if (queue.Count > owner.MinPoolSize && DateTime.Now - socket.Created > owner.SocketRecycleAge)
                {
                    //If we have more than the minimum amount of sockets, but less than the max, and the socket is older than the recycle age, we destroy it.
                    socket.Close();
                }
                else
                {
                    //Put the socket back in the pool.
                    lock (queue)
                    {
                        queue.Enqueue(socket);
                    }
                }
            }
        }