Example #1
0
        public void PutSocket(CustomSocket socket)
        {
            DebugLogging.Log("Put Socket -> Pool size: " + availableSockets.Count.ToString());
            lock (availableSockets)
            {
                TimeSpan socketLifeTime = DateTime.Now.Subtract(socket.TimeCreated);
                if (availableSockets.Count < POOL_MAX_SIZE && socketLifeTime.Seconds < EXPIRE_SECONDS)
                {
                    if (socket != null)
                    {
                        if (socket.Connected)
                        {
                            availableSockets.Enqueue(socket);

                            DebugLogging.Log("Socket Queued -> Pool size: " + availableSockets.Count.ToString());
                        }
                        else
                        {
                            socket.Close();
                        }
                    }
                }
                else
                {
                    socket.Close();
                    DebugLogging.Log("PutSocket - Socket is forced " +
                                     "to closed -> Pool size: " +
                                     availableSockets.Count.ToString());
                }
            }
        }
Example #2
0
        public CustomSocket GetSocket()
        {
            DebugLogging.Log("Get Socket -> Pool size: " + availableSockets.Count.ToString());
            if (availableSockets.Count > 0)
            {
                lock (availableSockets)
                {
                    CustomSocket socket = null;
                    while (availableSockets.Count > 0)
                    {
                        socket = availableSockets.Dequeue();

                        if (socket.Connected)
                        {
                            DebugLogging.Log("Socket Dequeued -> Pool size: " +
                                             availableSockets.Count.ToString());

                            return(socket);
                        }
                        else
                        {
                            socket.Close();
                            System.Threading.Interlocked.Decrement(ref SocketCounter);
                            DebugLogging.Log("GetSocket -- Close -- Count: " +
                                             SocketCounter.ToString());
                        }
                    }
                }
            }
            return(ConnectSocket());
        }
        public void PutSocket(CustomSocket socket)
        {
            DebugLogging.Log("Put Socket -> Pool size: " + availableSockets.Count.ToString());
            lock (availableSockets)
            {
                TimeSpan socketLifeTime = DateTime.Now.Subtract(socket.TimeCreated);
                if (availableSockets.Count < POOL_MAX_SIZE && socketLifeTime.Seconds < EXPIRE_SECONDS)
                {
                    if (socket != null)
                    {
                        if (socket.Connected)
                        {
                            availableSockets.Enqueue(socket);

                            DebugLogging.Log("Socket Queued -> Pool size: " + availableSockets.Count.ToString());
                        }
                        else
                        {
                            socket.Close();
                        }
                    }
                }
                else
                {
                    socket.Close();
                    DebugLogging.Log("PutSocket - Socket is forced " +
                                       "to closed -> Pool size: " +
                                       availableSockets.Count.ToString());
                }
            }
        }