internal HttpConnection(Socket socket, EndPointListener listener)
        {
            _socket   = socket;
            _listener = listener;

            Stream netStream = new NetworkStream(socket, false);

#if BUFFERED
            netStream = new BufferedNetworkStream((NetworkStream)netStream);
#endif
#if !NETCF || BCC || SSL
            if (listener.IsSecure)
            {
                var sslConf   = listener.SslConfiguration;
                var sslStream = new SslStream(netStream, false, sslConf.ClientCertificateValidationCallback);

                sslStream.AuthenticateAsServer(sslConf.ServerCertificate, sslConf.ClientCertificateRequired, sslConf.EnabledSslProtocols,
                                               sslConf.CheckCertificateRevocation);

                _secure = true;
                _stream = sslStream;
            }
            else
#endif
            _stream = netStream;

            _localEndPoint   = socket.LocalEndPoint;
            _remoteEndPoint  = socket.RemoteEndPoint;
            _sync            = new object();
            _timeout         = 90000;     // 90k ms for first request, 15k ms from then on.
            _timeoutCanceled = new Dictionary <int, bool> ();
            _timer           = new Timer(onTimeout, this, Timeout.Infinite, Timeout.Infinite);

            init();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes an input switch
        /// </summary>
        /// <param name="selector"></param>
        public override void ExecuteSwitch(object selector)
        {
            if (!_PowerIsOn)
            {
                PowerOn();
                var tempSelector = selector as Action;


                var inpuptTimer = new Crestron.SimplSharp.CTimer(o =>
                {
                    if (tempSelector != null)
                    {
                        (tempSelector).Invoke();
                    }
                    else
                    {
                        Debug.Console(1, this, "WARNING: ExecuteSwitch cannot handle type {0}", selector.GetType());
                    }
                }, WarmupTime);
            }
            else
            {
                if (selector is Action)
                {
                    (selector as Action).Invoke();
                }
                else
                {
                    Debug.Console(1, this, "WARNING: ExecuteSwitch cannot handle type {0}", selector.GetType());
                }
            }
        }
Exemplo n.º 3
0
        private void setSweepTimer(double interval)
        {
#if NETCF
            _sweepInterval = (long)interval;
            _sweepTimer    = new Timer((o) => Sweep(), Timeout.Infinite, Timeout.Infinite);
#else
            _sweepTimer          = new System.Timers.Timer(interval);
            _sweepTimer.Elapsed += (sender, e) => Sweep();
#endif
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void Close(int timeout)
        {
            var s = socket;

            Debug.WriteLine("NS Close ({0}): Close ({1})", s == null ? "<unknown>" : s.InternalRemoteEndPoint.ToString(), timeout);

            if (timeout < -1)
            {
                throw new ArgumentOutOfRangeException("timeout", "timeout is less than -1");
            }

            var close_timer = new Timer(OnTimeoutClose, timeout);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Power on the display
 /// </summary>
 public override void PowerOn()
 {
     Send(PowerOnCmd);
     if (!PowerIsOnFeedback.BoolValue && !_IsWarmingUp && !_IsCoolingDown)
     {
         _IsWarmingUp = true;
         IsWarmingUpFeedback.FireUpdate();
         // Fake power-up cycle
         WarmupTimer = new Crestron.SimplSharp.CTimer(o =>
         {
             _IsWarmingUp = false;
             _PowerIsOn   = true;
             IsWarmingUpFeedback.FireUpdate();
             PowerIsOnFeedback.FireUpdate();
         }, WarmupTime);
     }
 }
        private void disposeTimer()
        {
            if (_timer == null)
            {
                return;
            }

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

            _timer.Dispose();
            _timer = null;
        }
Exemplo n.º 8
0
        internal EventHandler SendRequest(HttpWebRequest request, string groupName)
        {
            WebConnection cnc;

            lock (this)
            {
                bool created;
                WebConnectionGroup cncGroup = GetConnectionGroup(groupName);
                cnc = cncGroup.GetConnection(request, out created);
                if (created)
                {
                    ++currentConnections;
                    if (idleTimer == null)
                    {
                        idleTimer = new Timer(IdleTimerCallback, null, maxIdleTime, maxIdleTime);
                    }
                }
            }

            return(cnc.SendRequest(request));
        }
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
        {
            this.sock   = sock;
            this.epl    = epl;
            this.secure = secure;
            this.key    = key;
            if (secure == false)
            {
                stream = new NetworkStream(sock, false);
            }
            else
            {
#if SSL
                SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, true, false);
                ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
                ssl_stream.ClientCertValidationDelegate    += OnClientCertificateValidation;
                stream = ssl_stream;
#else
                stream = new NetworkStream(sock, false);
#endif
            }
            timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
Exemplo n.º 10
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);
            }
        }