Exemple #1
0
        public static void OnUpdate()
        {
            lock (Locker)
            {
                IsCallingCallbacks = true;
                try
                {
                    for (int i = 0; i < ActiveConnections.Count; i++)
                    {
                        ConnectionBase connectionBase = ActiveConnections[i];
                        switch (connectionBase.State)
                        {
                        case HTTPConnectionStates.Processing:
                            connectionBase.HandleProgressCallback();
                            if (connectionBase.CurrentRequest.UseStreaming && connectionBase.CurrentRequest.Response != null && connectionBase.CurrentRequest.Response.HasStreamedFragments())
                            {
                                connectionBase.HandleCallback();
                            }
                            if (((!connectionBase.CurrentRequest.UseStreaming && connectionBase.CurrentRequest.UploadStream == null) || connectionBase.CurrentRequest.EnableTimoutForStreaming) && DateTime.UtcNow - connectionBase.StartTime > connectionBase.CurrentRequest.Timeout)
                            {
                                connectionBase.Abort(HTTPConnectionStates.TimedOut);
                            }
                            break;

                        case HTTPConnectionStates.TimedOut:
                            if (DateTime.UtcNow - connectionBase.TimedOutStart > TimeSpan.FromMilliseconds(500.0))
                            {
                                Logger.Information("HTTPManager", "Hard aborting connection becouse of a long waiting TimedOut state");
                                connectionBase.CurrentRequest.Response = null;
                                connectionBase.CurrentRequest.State    = HTTPRequestStates.TimedOut;
                                connectionBase.HandleCallback();
                                RecycleConnection(connectionBase);
                            }
                            break;

                        case HTTPConnectionStates.Redirected:
                            SendRequest(connectionBase.CurrentRequest);
                            RecycleConnection(connectionBase);
                            break;

                        case HTTPConnectionStates.WaitForRecycle:
                            connectionBase.CurrentRequest.FinishStreaming();
                            connectionBase.HandleCallback();
                            RecycleConnection(connectionBase);
                            break;

                        case HTTPConnectionStates.Upgraded:
                            connectionBase.HandleCallback();
                            break;

                        case HTTPConnectionStates.WaitForProtocolShutdown:
                        {
                            IProtocol protocol = connectionBase.CurrentRequest.Response as IProtocol;
                            protocol?.HandleEvents();
                            if (protocol == null || protocol.IsClosed)
                            {
                                connectionBase.HandleCallback();
                                connectionBase.Dispose();
                                RecycleConnection(connectionBase);
                            }
                            break;
                        }

                        case HTTPConnectionStates.AbortRequested:
                        {
                            IProtocol protocol = connectionBase.CurrentRequest.Response as IProtocol;
                            if (protocol != null)
                            {
                                protocol.HandleEvents();
                                if (protocol.IsClosed)
                                {
                                    connectionBase.HandleCallback();
                                    connectionBase.Dispose();
                                    RecycleConnection(connectionBase);
                                }
                            }
                            break;
                        }

                        case HTTPConnectionStates.Closed:
                            connectionBase.CurrentRequest.FinishStreaming();
                            connectionBase.HandleCallback();
                            RecycleConnection(connectionBase);
                            break;

                        case HTTPConnectionStates.Free:
                            RecycleConnection(connectionBase);
                            break;
                        }
                    }
                }
                finally
                {
                    IsCallingCallbacks = false;
                }
                lock (RecycledConnections)
                {
                    if (RecycledConnections.Count > 0)
                    {
                        for (int j = 0; j < RecycledConnections.Count; j++)
                        {
                            ConnectionBase connectionBase2 = RecycledConnections[j];
                            if (connectionBase2.IsFree)
                            {
                                ActiveConnections.Remove(connectionBase2);
                                FreeConnections.Add(connectionBase2);
                            }
                        }
                        RecycledConnections.Clear();
                    }
                }
                if (FreeConnections.Count > 0)
                {
                    for (int k = 0; k < FreeConnections.Count; k++)
                    {
                        ConnectionBase connectionBase3 = FreeConnections[k];
                        if (connectionBase3.IsRemovable)
                        {
                            List <ConnectionBase> value = null;
                            if (Connections.TryGetValue(connectionBase3.ServerAddress, out value))
                            {
                                value.Remove(connectionBase3);
                            }
                            connectionBase3.Dispose();
                            FreeConnections.RemoveAt(k);
                            k--;
                        }
                    }
                }
                if (CanProcessFromQueue())
                {
                    if (RequestQueue.Find((HTTPRequest req) => req.Priority != 0) != null)
                    {
                        RequestQueue.Sort((HTTPRequest req1, HTTPRequest req2) => req1.Priority - req2.Priority);
                    }
                    HTTPRequest[] array = RequestQueue.ToArray();
                    RequestQueue.Clear();
                    for (int l = 0; l < array.Length; l++)
                    {
                        SendRequest(array[l]);
                    }
                }
            }
            if (heartbeats != null)
            {
                heartbeats.Update();
            }
        }
Exemple #2
0
 private static void RecycleConnection(ConnectionBase conn)
 {
     conn.Recycle(OnConnectionRecylced);
 }