Esempio n. 1
0
        protected static T Read <T> (Stream stream, Func <string[], T> parser, int millisecondsTimeout)
            where T : HttpBase
        {
            var timeout = false;
            var timer   = new Timer(
                state =>
            {
                timeout = true;
                stream.Close();
            },
                null,
                millisecondsTimeout,
                -1);

            T         http      = null;
            Exception exception = null;

            try
            {
                http = parser(readHeaders(stream, _headersMaxLength));
                var contentLen = http.Headers["Content-Length"];
                if (contentLen != null && contentLen.Length > 0)
                {
                    http.EntityBodyData = readEntityBody(stream, contentLen);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                timer.Change(-1, -1);
                timer.Dispose();
            }

            var msg = timeout
                                          ? "A timeout has occurred while reading an HTTP request/response."
                                          : exception != null
                                                ? "An exception has occurred while reading an HTTP request/response."
#if SSHARP
                      + String.Format(" [{0}]", exception.Message)
#endif
                                                : null;

            if (msg != null)
            {
                throw new WebSocketException(msg, exception);
            }

            return(http);
        }
        private void disposeTimer()
        {
            if (_timer == null)
            {
                return;
            }

            try
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
            }
            catch
            {
            }

            _timer.Dispose();
            _timer = null;
        }
Esempio n. 3
0
        internal bool CheckAvailableForRecycling(out DateTime outIdleSince)
        {
            outIdleSince = DateTime.MinValue;

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

            lock (this)
            {
                if (groups == null || groups.Count == 0)
                {
                    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.
                 *
                 */

                groupList = new List <WebConnectionGroup> (groups.Values);
            }

            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 (removeList != null)
                {
                    foreach (var group in removeList)
                    {
                        RemoveConnectionGroup(group);
                    }
                }

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

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

                return(false);
            }
        }