예제 #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());
                }
            }
        }
예제 #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());
        }
예제 #3
0
        private static string SocketSendReceive(string viewer, string owner, string gadget)
        {
            //  These keys need to match what you see in edu.ucsf.profiles.shindig.service.SecureTokenGeneratorService in Shindig
            string request = "c=default" + (viewer != null ? "&v=" + HttpUtility.UrlEncode(viewer) : "") +
                             (owner != null ? "&o=" + HttpUtility.UrlEncode(owner) : "") + "&u=" + HttpUtility.UrlEncode(gadget) + "\r\n";

            Byte[] bytesSent     = System.Text.Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];

            // Create a socket connection with the specified server and port.
            //Socket s = ConnectSocket(tokenService[0], Int32.Parse(tokenService[1]));

            // during startup we might fail a few times, so be will to retry
            string page = "";

            for (int i = 0; i < 3 && page.Length == 0; i++)
            {
                CustomSocket s = null;
                try
                {
                    s = sockets.GetSocket();

                    if (s == null)
                    {
                        return("Connection failed");
                    }

                    // Send request to the server.
                    DebugLogging.Log("Sending Bytes");
                    s.Send(bytesSent, bytesSent.Length, 0);

                    // Receive the server home page content.
                    int bytes = 0;

                    // The following will block until te page is transmitted.
                    do
                    {
                        DebugLogging.Log("Receiving Bytes");
                        bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                        page  = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
                        DebugLogging.Log("Socket Page=" + page + "|");
                    }while (page.Length == page.TrimEnd().Length&& bytes > 0);
                }
                catch (Exception ex)
                {
                    DebugLogging.Log("Socket Error :" + ex.Message);
                    page = "";
                }
                finally
                {
                    if (sockets != null)
                    {
                        sockets.PutSocket(s);
                    }
                }
            }
            return(page.TrimEnd());
        }
예제 #4
0
        public SocketConnectionPool(string server, int port, int minConnections, int maxConnections, int expire, int timeout)
        {
            this.POOL_MAX_SIZE    = maxConnections;
            this.POOL_MIN_SIZE    = minConnections;
            this.EXPIRE_SECONDS   = expire;
            this.RECIEVE_TIMEOUT  = timeout;
            this.server           = server;
            this.port             = port;
            this.availableSockets = new Queue <CustomSocket>();
            for (int i = 0; i < minConnections; i++)
            {
                CustomSocket cachedSocket = ConnectSocket();
                PutSocket(cachedSocket);
            }

            DebugLogging.Log("Connection Pool is initialized" +
                             " with Max Number of " +
                             POOL_MAX_SIZE.ToString() + " And Min number of " +
                             availableSockets.Count.ToString());
        }
예제 #5
0
        private CustomSocket ConnectSocket()
        {
            CustomSocket s         = null;
            IPHostEntry  hostEntry = null;

            // Get host related information.
            hostEntry = Dns.GetHostEntry(server);

            // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
            // an exception that occurs when the host IP Address is not compatible with the address family
            // (typical in the IPv6 case).
            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint   ipe        = new IPEndPoint(address, port);
                CustomSocket tempSocket =
                    new CustomSocket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    tempSocket.Connect(ipe);
                }
                catch
                {
                    // ignore error, move on to the next entry
                }

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    s.ReceiveTimeout = RECIEVE_TIMEOUT;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return(s);
        }
        private CustomSocket ConnectSocket()
        {
            CustomSocket s = null;
            IPHostEntry hostEntry = null;

            // Get host related information.
            hostEntry = Dns.GetHostEntry(server);

            // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
            // an exception that occurs when the host IP Address is not compatible with the address family
            // (typical in the IPv6 case).
            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                CustomSocket tempSocket =
                    new CustomSocket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    tempSocket.Connect(ipe);
                }
                catch
                {
                    // ignore error, move on to the next entry
                }

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    s.ReceiveTimeout = RECIEVE_TIMEOUT;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return s;
        }
        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.CloseNice();
                        }
                    }
                }
                else
                {
                    socket.CloseNice();
                    DebugLogging.Log("PutSocket - Socket is forced " +
                                       "to closed -> Pool size: " +
                                       availableSockets.Count.ToString());
                }
            }
        }