public UnityRequest(UnityHttpClientV2 inst, string url, HttpRequest request, object previousUserData, int requestId) : base(inst, request) { self = inst; OriginalRequest = request; RequestId = requestId; PreviousUserData = previousUserData; Request = new UnityWebRequest(url); // Auto-choose HTTP method Request.method = request.Method ?? (request.Body != null ? "POST" : "GET"); // TODO Missing functionality (currently unsupported by UnityWebRequest). // req.SetRequestHeader("User-agent", request.UserAgent); // req.keepAlive = true; foreach (var pair in request.Headers) { Request.SetRequestHeader(pair.Key, pair.Value); } if (OriginalRequest.Body != null) { UploadHandler uploader = new UploadHandlerRaw(OriginalRequest.Body); if (ContentType != null) uploader.contentType = ContentType; Request.uploadHandler = uploader; } Request.downloadHandler = new DownloadHandlerBuffer(); }
/// <summary>Returns whether this response is in an error state that should be retried according to the request configuration.</summary> public bool ShouldBeRetried(HttpRequest request) { switch (request.RetryPolicy) { case HttpRequest.Policy.NonpermanentErrors: return HasFailed || StatusCode < 100 || (StatusCode >= 300 && StatusCode < 400) || StatusCode >= 500; case HttpRequest.Policy.AllErrors: return HasFailed || StatusCode < 100 || StatusCode >= 300; case HttpRequest.Policy.Never: default: return false; } }
internal HttpRequest MakeUnauthenticatedHttpRequest(string path) { HttpRequest result = new HttpRequest(); if (path.StartsWith("/")) { result.Url = Server + path; } else { result.Url = path; } result.LoadBalancerCount = LoadBalancerCount; result.Headers["x-apikey"] = ApiKey; result.Headers["x-sdkversion"] = SdkVersion; result.Headers["x-apisecret"] = ApiSecret; result.FailedHandler = HttpRequestFailedHandler; result.TimeoutMillisec = HttpTimeoutMillis; result.UserAgent = UserAgent; return result; }
protected override WebRequest CreateRequest(HttpRequest request, string url, object previousUserData) { return new UnityRequest(this, url, request, previousUserData, (RequestCount += 1)); }
private void Run() { int delay = LoopIterationDuration; string messageToAcknowledge = null; bool lastResultPositive = true; while (!Stopped) { if (!lastResultPositive) { // Last time failed, wait a bit to avoid bombing the Internet. Thread.Sleep(PopEventDelayThreadHold); } UrlBuilder url = new UrlBuilder("/v1/gamer/event"); url.Path(Domain).QueryParam("timeout", delay); if (messageToAcknowledge != null) { url.QueryParam("ack", messageToAcknowledge); } CurrentRequest = Gamer.MakeHttpRequest(url); CurrentRequest.RetryPolicy = HttpRequest.Policy.NonpermanentErrors; CurrentRequest.TimeoutMillisec = delay + 30000; CurrentRequest.DoNotEnqueue = true; Managers.HttpClient.Run(CurrentRequest, (HttpResponse res) => { CurrentRequest = null; try { lastResultPositive = true; if (res.StatusCode == 200) { messageToAcknowledge = res.BodyJson["id"]; ProcessEvent(res); } else if (res.StatusCode != 204) { lastResultPositive = false; // Non retriable error -> kill ourselves if (res.StatusCode >= 400 && res.StatusCode < 500) { Stopped = true; } } } catch (Exception e) { Common.LogError("Exception happened in pop event loop: " + e.ToString()); } SynchronousRequestLock.Set(); }); // Wait for request (synchronous) SynchronousRequestLock.WaitOne(); // Wait if suspended if (Paused) { SynchronousRequestLock.WaitOne(); lastResultPositive = true; } } Common.Log("Finished pop event thread " + Thread.CurrentThread.ManagedThreadId); }
/// <summary>Suspends the event thread.</summary> public DomainEventLoop Suspend() { Paused = true; if (CurrentRequest != null) { Managers.HttpClient.Abort(CurrentRequest); CurrentRequest = null; } return this; }
/// <summary> /// Will stop the event thread. Might take some time until the current request finishes. /// You should not use this object for other purposes later on. In particular, do not start it again. /// </summary> public DomainEventLoop Stop() { Stopped = true; Resume(); // Stop and exit cleanly if (CurrentRequest != null) { Managers.HttpClient.Abort(CurrentRequest); CurrentRequest = null; } Cotc.RunningEventLoops.Remove(this); return this; }