private static void HttpRequestHttpCallback(IPluginHttpUtilClient client, HttpRequest request, ICallInfo info, HttpRequestQueueResultCode result, AsyncHttpRequest asyncHttpRequest, object state) { var response = HttpResponseImpl.CreateHttpResponse(request, info, result, asyncHttpRequest); client.Log.Debug("Got HttpResoonse - executing callback."); // Sync request triggers an event to release the waiting Request thread to continue try { request.Callback(response, state); } catch (Exception ex) { client.Log.Error(ex); if (request.Async) { client.Log.Error(ex); client.PluginReportError(ErrorCodes.AsyncCallbackException, ex, state); } else { throw; } } }
private static void HttpRequestCallback(IPluginHttpUtilClient client, HttpRequest request, HttpRequestQueueResultCode result, AsyncHttpRequest httpRequest, object state, ICallInfo info) { var callInfo = (ICallInfoImpl)info; var doCheck = !callInfo.IsProcessed; if (doCheck) { callInfo.Reset(); } var response = HttpResponseImpl.CreateHttpResponse(request, info, result, httpRequest); client.Log.Debug("Got HttpResponse - executing callback."); try { request.Callback(response, state); string errorMsg; // and check that one of methods was called if (doCheck && !info.StrictModeCheck(out errorMsg)) { var infoTypeName = info.GetType().ToString(); client.PluginReportError(ErrorCodes.MissingCallProcessing, null, infoTypeName); info.Fail(string.Format("HttpRequestCallback: {0}", errorMsg)); } } catch (Exception ex) { client.Log.Error(ex); client.PluginReportError(ErrorCodes.AsyncCallbackException, ex, state); if (!callInfo.IsProcessed) { callInfo.Fail(ex.ToString()); } } }
public static void HttpRequest(IPluginHttpUtilClient client, HttpRequest request, ICallInfo info) { if (request.Callback == null) { var url = request.Url; client.Log.Debug("HttpRequest Callback is not set. Using default to log in case of error. " + url); request.Callback = (response, state) => { if (response.Status != HttpRequestQueueResult.Success) { client.Log.Warn( string.Format( "Request to '{0}' failed. reason={1}, httpcode={2} webstatus={3}, HttpQueueResult={4}.", url, response.Reason, response.HttpCode, response.WebStatus, response.Status)); } }; } var callInfo = (ICallInfoImpl)info; const int RequestRetryCount = 3; if (request.Async) { callInfo.InternalDefer(); } else { var timeout = (int)client.HttpRequestQueue.QueueTimeout.TotalMilliseconds + (RequestRetryCount + 1) * client.HttpQueueRequestTimeout + 5000; Action timeoutAction = () => { client.Log.ErrorFormat("Game did not resumed after {0} ms. http call to {1}. Headers: {2}, CustomHeaders:{3}, " + "Accept:{4}, ContentType:{5}, Method:{6}", timeout, request.Url, request.Headers != null ? Newtonsoft.Json.JsonConvert.SerializeObject(request.Headers) : "<null>", request.CustomHeaders != null ? Newtonsoft.Json.JsonConvert.SerializeObject(request.CustomHeaders) : "<null>", request.Accept, request.ContentType, request.Method); var response = new HttpResponseImpl( request, info, HttpRequestQueueResultCode.RequestTimeout, string.Empty, (int)WebExceptionStatus.Timeout); request.Callback(response, request.UserState); }; client.SuspendClient(timeout, timeoutAction); callInfo.Pause(); } try { var webRequest = (HttpWebRequest)WebRequest.Create(request.Url); webRequest.Proxy = null; webRequest.Method = request.Method ?? "GET"; webRequest.ContentType = request.ContentType; webRequest.Accept = request.Accept; webRequest.Timeout = client.HttpQueueRequestTimeout; if (request.CustomHeaders != null) { AddCustomHttpHeaders(client, request, webRequest); } if (request.Headers != null) { AddPredefinedHttpHeaders(client, request, webRequest); } HttpRequestQueueCallback callback = (result, httpRequest, state) => { if (client.Log.IsDebugEnabled) { client.Log.DebugFormat("callback for request is called.url:{0}.IsAsync:{1}", httpRequest.WebRequest.RequestUri, request.Async); } if (request.Async) { client.ExecutionFiber.Enqueue(() => HttpRequestCallback(client, request, result, httpRequest, state, info)); } else { client.ResumeClient(() => HttpRequestCallback(client, request, result, httpRequest, state, info)); } }; EnqueuWebRequest(client.HttpRequestQueue, request, RequestRetryCount, webRequest, callback); } catch (WebException e) { client.Log.Error(string.Format("Exception calling Url:{0}", request.Url), e); var response = new HttpResponseImpl(request, info, HttpRequestQueueResultCode.Error, e.Message, (int)e.Status); request.Callback(response, request.UserState); } catch (Exception ex) { client.ExecutionFiber.Resume(); client.Log.Error(string.Format("Exception calling Url:{0}", request.Url), ex); var response = new HttpResponseImpl( request, info, HttpRequestQueueResultCode.Error, ex.Message, (int)WebExceptionStatus.UnknownError); request.Callback(response, request.UserState); } }