Пример #1
0
        public static void OnUpdate()
        {
            object locker = HTTPManager.Locker;

            lock (locker)
            {
                HTTPManager.IsCallingCallbacks = true;
                try
                {
                    for (int i = 0; i < HTTPManager.ActiveConnections.Count; i++)
                    {
                        HTTPConnection hTTPConnection = HTTPManager.ActiveConnections[i];
                        switch (hTTPConnection.State)
                        {
                        case HTTPConnectionStates.Processing:
                            hTTPConnection.HandleProgressCallback();
                            if (hTTPConnection.CurrentRequest.UseStreaming && hTTPConnection.CurrentRequest.Response != null && hTTPConnection.CurrentRequest.Response.HasStreamedFragments())
                            {
                                hTTPConnection.HandleCallback();
                            }
                            if (((!hTTPConnection.CurrentRequest.UseStreaming && hTTPConnection.CurrentRequest.UploadStream == null) || hTTPConnection.CurrentRequest.EnableTimoutForStreaming) && DateTime.UtcNow - hTTPConnection.StartTime > hTTPConnection.CurrentRequest.Timeout)
                            {
                                hTTPConnection.Abort(HTTPConnectionStates.TimedOut);
                            }
                            break;

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

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

                        case HTTPConnectionStates.WaitForProtocolShutdown:
                        {
                            WebSocketResponse webSocketResponse = hTTPConnection.CurrentRequest.Response as WebSocketResponse;
                            if (webSocketResponse != null)
                            {
                                webSocketResponse.HandleEvents();
                            }
                            if (webSocketResponse == null || webSocketResponse.IsClosed)
                            {
                                hTTPConnection.HandleCallback();
                                hTTPConnection.Dispose();
                                HTTPManager.RecycleConnection(hTTPConnection);
                            }
                            break;
                        }

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

                        case HTTPConnectionStates.AbortRequested:
                        {
                            WebSocketResponse webSocketResponse = hTTPConnection.CurrentRequest.Response as WebSocketResponse;
                            if (webSocketResponse != null)
                            {
                                webSocketResponse.HandleEvents();
                                if (webSocketResponse.IsClosed)
                                {
                                    hTTPConnection.HandleCallback();
                                    hTTPConnection.Dispose();
                                    HTTPManager.RecycleConnection(hTTPConnection);
                                }
                            }
                            break;
                        }

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

                        case HTTPConnectionStates.Closed:
                            hTTPConnection.CurrentRequest.FinishStreaming();
                            hTTPConnection.HandleCallback();
                            HTTPManager.RecycleConnection(hTTPConnection);
                            break;
                        }
                    }
                }
                finally
                {
                    HTTPManager.IsCallingCallbacks = false;
                }
                if (HTTPManager.RecycledConnections.Count > 0)
                {
                    for (int j = 0; j < HTTPManager.RecycledConnections.Count; j++)
                    {
                        HTTPConnection hTTPConnection2 = HTTPManager.RecycledConnections[j];
                        if (hTTPConnection2.IsFree)
                        {
                            HTTPManager.ActiveConnections.Remove(hTTPConnection2);
                            HTTPManager.FreeConnections.Add(hTTPConnection2);
                        }
                    }
                    HTTPManager.RecycledConnections.Clear();
                }
                if (HTTPManager.FreeConnections.Count > 0)
                {
                    for (int k = 0; k < HTTPManager.FreeConnections.Count; k++)
                    {
                        HTTPConnection hTTPConnection3 = HTTPManager.FreeConnections[k];
                        if (hTTPConnection3.IsRemovable)
                        {
                            List <HTTPConnection> list = null;
                            if (HTTPManager.Connections.TryGetValue(hTTPConnection3.ServerAddress, out list))
                            {
                                list.Remove(hTTPConnection3);
                            }
                            hTTPConnection3.Dispose();
                            HTTPManager.FreeConnections.RemoveAt(k);
                            k--;
                        }
                    }
                }
                if (HTTPManager.CanProcessFromQueue())
                {
                    if (HTTPManager.RequestQueue.Find((HTTPRequest req) => req.Priority != 0) != null)
                    {
                        HTTPManager.RequestQueue.Sort((HTTPRequest req1, HTTPRequest req2) => req1.Priority - req2.Priority);
                    }
                    HTTPRequest[] array = HTTPManager.RequestQueue.ToArray();
                    HTTPManager.RequestQueue.Clear();
                    for (int l = 0; l < array.Length; l++)
                    {
                        HTTPManager.SendRequest(array[l]);
                    }
                }
            }
            if (HTTPManager.heartbeats != null)
            {
                HTTPManager.heartbeats.Update();
            }
        }