示例#1
0
        WebConnectionGroup GetConnectionGroup(string name)
        {
            if (name == null)
            {
                name = "";
            }

            /*
             * Optimization:
             *
             * In the vast majority of cases, we only have one single WebConnectionGroup per ServicePoint, so we
             * don't need to allocate a dictionary.
             *
             */

            WebConnectionGroup group;

            if (groups != null && groups.TryGetValue(name, out group))
            {
                return(group);
            }

            group = new WebConnectionGroup(this, name);
            group.ConnectionClosed += (s, e) => currentConnections--;

            if (groups == null)
            {
                groups = new Dictionary <string, WebConnectionGroup> ();
            }
            groups.Add(name, group);

            return(group);
        }
示例#2
0
        void RemoveConnectionGroup(WebConnectionGroup group)
        {
            if (groups == null || groups.Count == 0)
            {
                // No more connection groups left.
                if (group != firstGroup)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    firstGroup = null;
                }
                return;
            }

            if (group == firstGroup)
            {
                // Steal one entry from the dictionary.
                var en = groups.GetEnumerator();
                en.MoveNext();
                firstGroup = en.Current.Value;
                groups.Remove(en.Current.Key);
            }
            else
            {
                groups.Remove(group.Name);
            }
        }
示例#3
0
 public WebConnection(WebConnectionGroup group, ServicePoint sPoint)
 {
     this.sPoint  = sPoint;
     buffer       = new byte [4096];
     readState    = ReadState.None;
     Data         = new WebConnectionData();
     initConn     = new WaitCallback(InitConnection);
     abortHandler = new EventHandler(Abort);
     queue        = group.Queue;
 }
示例#4
0
        internal EventHandler SendRequest(HttpWebRequest request, string groupName)
        {
            WebConnection cnc;

            lock (locker) {
                WebConnectionGroup cncGroup = GetConnectionGroup(groupName);
                cnc = cncGroup.GetConnection(request);
            }

            return(cnc.SendRequest(request));
        }
 public WebConnection(WebConnectionGroup group, ServicePoint sPoint)
 {
     this.sPoint                 = sPoint;
     this.buffer                 = new byte[4096];
     this.readState              = ReadState.None;
     this.Data                   = new WebConnectionData();
     this.initConn               = new WaitCallback(this.InitConnection);
     this.queue                  = group.Queue;
     this.abortHelper            = new WebConnection.AbortHelper();
     this.abortHelper.Connection = this;
     this.abortHandler           = new EventHandler(this.abortHelper.Abort);
 }
        internal EventHandler SendRequest(HttpWebRequest request, string groupName)
        {
            object        obj = this.locker;
            WebConnection connection;

            lock (obj)
            {
                WebConnectionGroup connectionGroup = this.GetConnectionGroup(groupName);
                connection = connectionGroup.GetConnection(request);
            }
            return(connection.SendRequest(request));
        }
示例#7
0
        public bool CloseConnectionGroup(string connectionGroupName)
        {
            lock (this) {
                WebConnectionGroup cncGroup = GetConnectionGroup(connectionGroupName);
                if (cncGroup != null)
                {
                    cncGroup.Close();
                    return(true);
                }
            }

            return(false);
        }
示例#8
0
 public WebConnection(WebConnectionGroup group, ServicePoint sPoint)
 {
     this.sPoint = sPoint;
     buffer      = new byte [4096];
     Data        = new WebConnectionData();
     initConn    = new WaitCallback(state => {
         try {
             InitConnection(state);
         } catch {}
     });
     queue                  = group.Queue;
     abortHelper            = new AbortHelper();
     abortHelper.Connection = this;
     abortHandler           = new EventHandler(abortHelper.Abort);
 }
        /// <summary>Removes the specified connection group from this <see cref="T:System.Net.ServicePoint" /> object.</summary>
        /// <returns>A <see cref="T:System.Boolean" /> value that indicates whether the connection group was closed.</returns>
        /// <param name="connectionGroupName">The name of the connection group that contains the connections to close and remove from this service point. </param>
        public bool CloseConnectionGroup(string connectionGroupName)
        {
            object obj = this.locker;

            lock (obj)
            {
                WebConnectionGroup connectionGroup = this.GetConnectionGroup(connectionGroupName);
                if (connectionGroup != null)
                {
                    connectionGroup.Close();
                    return(true);
                }
            }
            return(false);
        }
示例#10
0
        private WebConnectionGroup GetConnectionGroup(string name)
        {
            if (name == null)
            {
                name = string.Empty;
            }
            WebConnectionGroup webConnectionGroup = this.Groups[name] as WebConnectionGroup;

            if (webConnectionGroup != null)
            {
                return(webConnectionGroup);
            }
            webConnectionGroup = new WebConnectionGroup(this, name);
            this.Groups[name]  = webConnectionGroup;
            return(webConnectionGroup);
        }
示例#11
0
        WebConnectionGroup GetConnectionGroup(string name)
        {
            if (name == null)
            {
                name = "";
            }

            WebConnectionGroup group = Groups [name] as WebConnectionGroup;

            if (group != null)
            {
                return(group);
            }

            group         = new WebConnectionGroup(this, name);
            Groups [name] = group;
            return(group);
        }
示例#12
0
        private WebConnection CreateOrReuseConnection(HttpWebRequest request)
        {
            int           num = this.connections.Count;
            WebConnection webConnection;

            for (int i = 0; i < num; i++)
            {
                WeakReference weakReference = this.connections[i] as WeakReference;
                webConnection = (weakReference.Target as WebConnection);
                if (webConnection == null)
                {
                    this.connections.RemoveAt(i);
                    num--;
                    i--;
                }
                else if (!webConnection.Busy)
                {
                    WebConnectionGroup.PrepareSharingNtlm(webConnection, request);
                    return(webConnection);
                }
            }
            if (this.sPoint.ConnectionLimit > num)
            {
                webConnection = new WebConnection(this, this.sPoint);
                this.connections.Add(new WeakReference(webConnection));
                return(webConnection);
            }
            if (this.rnd == null)
            {
                this.rnd = new Random();
            }
            int           index          = (num <= 1) ? 0 : this.rnd.Next(0, num - 1);
            WeakReference weakReference2 = (WeakReference)this.connections[index];

            webConnection = (weakReference2.Target as WebConnection);
            if (webConnection == null)
            {
                webConnection = new WebConnection(this, this.sPoint);
                this.connections.RemoveAt(index);
                this.connections.Add(new WeakReference(webConnection));
            }
            return(webConnection);
        }
示例#13
0
        public bool CloseConnectionGroup(string connectionGroupName)
        {
            WebConnectionGroup cncGroup = null;

            lock (this) {
                cncGroup = GetConnectionGroup(connectionGroupName);
                if (cncGroup != null)
                {
                    RemoveConnectionGroup(cncGroup);
                }
            }

            // WebConnectionGroup.Close() must *not* be called inside the lock
            if (cncGroup != null)
            {
                cncGroup.Close();
                return(true);
            }

            return(false);
        }
示例#14
0
		void RemoveConnectionGroup (WebConnectionGroup group)
		{
			if (groups == null || groups.Count == 0)
				throw new InvalidOperationException ();

			groups.Remove (group.Name);
		}
示例#15
0
		WebConnectionGroup GetConnectionGroup (string name)
		{
			if (name == null)
				name = "";

			/*
			 * Optimization:
			 * 
			 * In the vast majority of cases, we only have one single WebConnectionGroup per ServicePoint, so we
			 * don't need to allocate a dictionary.
			 * 
			 */

			WebConnectionGroup group;
			if (groups != null && groups.TryGetValue (name, out group))
				return group;

			group = new WebConnectionGroup (this, name);
			group.ConnectionClosed += (s, e) => currentConnections--;

			if (groups == null)
				groups = new Dictionary<string, WebConnectionGroup> ();
			groups.Add (name, group);

			return group;
		}
示例#16
0
        internal bool CheckAvailableForRecycling(out DateTime outIdleSince)
        {
            outIdleSince = DateTime.MinValue;

            TimeSpan                  idleTimeSpan;
            WebConnectionGroup        singleGroup, singleRemove = null;
            List <WebConnectionGroup> groupList = null, removeList = null;

            lock (this) {
                if (firstGroup == null)
                {
                    idleSince = DateTime.MinValue;
                    return(true);
                }

                idleTimeSpan = TimeSpan.FromMilliseconds(maxIdleTime);

                /*
                 * WebConnectionGroup.TryRecycle() must run outside the lock, so we need to
                 * copy the group dictionary if it exists.
                 *
                 * In most cases, we only have a single connection group, so we can simply store
                 * that in a local variable instead of copying a collection.
                 *
                 */

                singleGroup = firstGroup;
                if (groups != null)
                {
                    groupList = new List <WebConnectionGroup> (groups.Values);
                }
            }

            if (singleGroup.TryRecycle(idleTimeSpan, ref outIdleSince))
            {
                singleRemove = singleGroup;
            }

            if (groupList != null)
            {
                foreach (var group in groupList)
                {
                    if (!group.TryRecycle(idleTimeSpan, ref outIdleSince))
                    {
                        continue;
                    }
                    if (removeList == null)
                    {
                        removeList = new List <WebConnectionGroup> ();
                    }
                    removeList.Add(group);
                }
            }

            lock (this) {
                idleSince = outIdleSince;

                if (singleRemove != null)
                {
                    RemoveConnectionGroup(singleRemove);
                }

                if (removeList != null)
                {
                    foreach (var group in removeList)
                    {
                        RemoveConnectionGroup(group);
                    }
                }

                if (groups != null && groups.Count == 0)
                {
                    groups = null;
                }

                if (firstGroup == null)
                {
                    idleTimer.Dispose();
                    idleTimer = null;
                    return(true);
                }

                return(false);
            }
        }
示例#17
0
		WebConnectionGroup GetConnectionGroup (string name)
		{
			if (name == null)
				name = "";

			WebConnectionGroup group = Groups [name] as WebConnectionGroup;
			if (group != null)
				return group;

			group = new WebConnectionGroup (this, name);
			Groups [name] = group;
			return group;
		}
示例#18
0
			public ConnectionState (WebConnectionGroup group)
			{
				Group = group;
				idleSince = DateTime.UtcNow;
				Connection = new WebConnection (this, group.sPoint);
			}
示例#19
0
		public WebConnection (WebConnectionGroup group, ServicePoint sPoint)
		{
			this.sPoint = sPoint;
			buffer = new byte [4096];
			readState = ReadState.None;
			Data = new WebConnectionData ();
			initConn = new WaitCallback (state => {
				try {
					InitConnection (state);
				} catch {}
				});
			queue = group.Queue;
			abortHelper = new AbortHelper ();
			abortHelper.Connection = this;
			abortHandler = new EventHandler (abortHelper.Abort);
		}
示例#20
0
 public ConnectionState(WebConnectionGroup group)
 {
     Group      = group;
     idleSince  = DateTime.UtcNow;
     Connection = new WebConnection(this, group.sPoint);
 }