Example #1
0
        /// <summary>
        /// Get an open socket from the connection pool.
        /// </summary>
        /// <returns>Socket returned from the pool or new socket
        /// opened.</returns>
        public SharedCacheTcpClient GetSocket()
        {
            #region Access Log
                        #if TRACE
            {
                Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;");
            }
                        #endif
            #endregion Access Log

            SharedCacheTcpClient socket = null;
            bool stillCheckingSockets   = false;
            lock (bulkObject)
            {
                if (this.availableSockets.Count > 0)
                {
                    socket = this.availableSockets.Dequeue();
                    stillCheckingSockets = true;
                }
            }
            while (stillCheckingSockets)
            {
                if (socket.Connected)
                {
                    TimeSpan sp = DateTime.Now.Subtract(socket.LastUsed);
                    //TODO: read data from config file: SocketPoolTimeout
                    if (sp.Minutes >= 2)
                    {
                        socket.Close();
                        return(this.OpenSocket());
                    }
                    else
                    {
                        return(socket);
                    }
                }
                else
                {
                    if (socket != null)
                    {
                        // MAYBE we should consider to reconnect it instead to close it?
                        socket.Close();
                    }
                }
                stillCheckingSockets = false;
                lock (bulkObject)
                {
                    if (this.availableSockets.Count > 0)
                    {
                        socket = this.availableSockets.Dequeue();
                        stillCheckingSockets = true;
                    }
                }
            }
            return(this.OpenSocket());
        }
Example #2
0
 /// <summary>
 /// Return the given socket back to the socket pool.
 /// </summary>
 /// <param name="socket">Socket connection to return.</param>
 public void PutSocket(SharedCacheTcpClient socket)
 {
     #region Access Log
                 #if TRACE
     {
         Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;");
     }
                 #endif
     #endregion Access Log
     //TODO: Provider.Cache.IndexusDistributionCache.ProviderSection.ClientSetting.SocketPoolMinAvailableSize
     // if (this.availableSockets.Count < Provider.Cache.IndexusDistributionCache.ProviderSection.ClientSetting.SocketPoolMinAvailableSize)
     if (this.availableSockets.Count <= this.PoolSize)
     // if (this.availableSockets.Count < TcpSocketConnectionPool.POOL_SIZE)
     {
         if (socket != null)
         {
             if (socket.Connected)
             {
                 // Set the socket back to blocking and enqueue
                 socket.SetBlockingMode(true);
                 socket.LastUsed = DateTime.Now;
                 lock (bulkObject)
                 {
                     this.availableSockets.Enqueue(socket);
                 }
             }
             else
             {
                 socket.Close();
             }
         }
     }
     else
     {
         // Number of sockets is above the pool size, so just close it.
         socket.Close();
     }
 }