Holds connection cache for a server on one IP address
Example #1
0
 Html PrintCachedServer(CachedServer server)
 {
     Html data = Html.Format ("<h3>{0}</h3>", server.endpoint);
     foreach (CachedConnection c in server.Connections) {
         if (c.Busy)
             data += Html.Format (" <span style=\"background: green;\">busy</span> ") + c.served;
         else
             data += Html.Format (" <span style=\"background: gray;\">free</span> ") + c.served;
     }
     return data;
 }
Example #2
0
        /// <summary>
        /// Get cached or create a new connection to specific ip-addresses.
        /// </summary>
        /// <param name='dns'>
        /// Dns.
        /// </param>
        /// <param name='port'>
        /// Port.
        /// </param>
        /// <param name='forceNew'>
        /// Don't reuse, always start a new connection
        /// </param>
        /// <param name='wait'>
        /// Wait until there is a slot free, otherwise it will return null.
        /// </param>
        public CachedConnection Connect(DnsLookup dns, int port, bool forceNew, bool wait)
        {
            CachedConnection c = null;
            while (true) {
                //Search for cached connections, return if found.
                //Also calculates the server with the least number of connections to
                CachedServer leastUsedServer = null;
                lock (serverCache) {
                    //Search for open connection and least used server
                    foreach (IPAddress ip in dns.AList) {
                        IPEndPoint ep = new IPEndPoint (ip, port);
                        CachedServer server;
                        if (serverCache.TryGetValue (ep, out server) == false) {
                            server = new CachedServer (ep, this);
                            serverCache.Add (ep, server);
                        }

                        //Test for least used server
                        if (leastUsedServer == null) {
                            leastUsedServer = server;
                        }
                        if (server.ConnectionCount < leastUsedServer.ConnectionCount)
                            leastUsedServer = server;

                        if (forceNew == false)
                            continue;

                        c = server.GetActiveConnection ();
                        if (c != null)
                            return c;
                    }
                }

                //No cached connection found, create one
                if (forceNew)
                    c = leastUsedServer.GetNewConnection ();
                else
                    c = leastUsedServer.GetUnlimitedNewConnection ();
                if (c != null)
                    return c;

                if (wait == false)
                    return null;

                //Maximum number of connections to all servers were already reached.
                //Wait for new connections
                releasedConnection.WaitOne (TimeSpan.FromSeconds (5));
            }
        }
Example #3
0
 public CachedConnection(CachedServer server)
 {
     this.server = server;
 }