Beispiel #1
0
        //added for Viveport, the HttpWebRequest requires additional configuration
        private void ExecuteRequestWithExpectedStatusCodes(HttpWebRequest webRequest, byte[] postData, Action <AsyncHttpResponse, ClientAuthenticationQueue> callback, object state, List <HttpStatusCode> expectedStatusCodes)
        {
            try
            {
                webRequest.Proxy   = null;
                webRequest.Timeout = this.requestTimeoutMilliseconds;

                HttpRequestQueueCallback queueCallback =
                    (result, httpRequest, userState) =>
                    this.fiber.Enqueue(() => this.OnCallbackReturnExpectedStatusCodes(result, httpRequest, userState, callback, expectedStatusCodes));


                if (postData != null)
                {
                    webRequest.Method = "POST";
                    this.httpRequestQueue.Enqueue(webRequest, postData, queueCallback, state);
                }
                else
                {
                    webRequest.Method = "GET";
                    this.httpRequestQueue.Enqueue(webRequest, queueCallback, state);
                }
            }
            catch (Exception ex)
            {
                var message = string.Format("Exception during ExecuteRequest to url '{0}'. Exception Msg:{1}",
                                            webRequest.RequestUri, ex.Message);
                log.Error(message, ex);
                ThreadPool.QueueUserWorkItem(delegate { callback(new AsyncHttpResponse(HttpRequestQueueResultCode.Error, this.RejectIfUnavailable, state), this); });
            }
        }
Beispiel #2
0
        private void ExecuteRequest(HttpWebRequest webRequest, HttpRequestQueueCallback callback, object state)
        {
            if (this.queue.Count > this.MaxQueuedRequests)
            {
                callback(HttpRequestQueueResultCode.QueueFull, null, state);
                return;
            }

            var request = new QueuedRequest {
                Request = webRequest, Callback = callback, PostData = null, State = state,
            };

            switch (this.QueueState)
            {
            case HttpRequestQueueState.Connecting:
                this.ExecuteRequestConnecting(request);
                break;

            case HttpRequestQueueState.Reconnecting:
                this.ExecuteRequestReconnecting(request);
                break;

            case HttpRequestQueueState.Running:
                this.ExecuteRequestOnline(request);
                break;

            case HttpRequestQueueState.Offline:
                this.ExecuteRequestOffline(request);
                break;
            }
        }
Beispiel #3
0
        private void ExecuteRequest(string clientAuthenticationRequestUrl, byte[] postData, string contentType, Action <AsyncHttpResponse, ClientAuthenticationQueue> callback, object state)
        {
            try
            {
                var webRequest = (HttpWebRequest)WebRequest.Create(clientAuthenticationRequestUrl);
                webRequest.Proxy       = null;
                webRequest.Timeout     = this.requestTimeoutMilliseconds;
                webRequest.ContentType = contentType;

                HttpRequestQueueCallback queueCallback =
                    (result, httpRequest, userState) =>
                    this.fiber.Enqueue(() => this.OnCallback(result, httpRequest, userState, callback));


                if (postData != null)
                {
                    webRequest.Method = "POST";
                    this.httpRequestQueue.Enqueue(webRequest, postData, queueCallback, state);
                }
                else
                {
                    webRequest.Method = "GET";
                    this.httpRequestQueue.Enqueue(webRequest, queueCallback, state);
                }
            }
            catch (Exception ex)
            {
                var message = string.Format("Exception during ExecuteRequest to url '{0}'. Exception Msg:{1}", clientAuthenticationRequestUrl, ex.Message);
                log.Error(this.execRequestLogGuard, message, ex);
                ThreadPool.QueueUserWorkItem(delegate { callback(new AsyncHttpResponse(HttpRequestQueueResultCode.Error, this.RejectIfUnavailable, state), this); });
            }
        }
Beispiel #4
0
        private bool SendHttpRequest(PeerBase peer, string uri, string data, WebRpcRequest rpcRequest)
        {
            var binData = Encoding.UTF8.GetBytes(data);

            HttpRequestQueueCallback callback =
                (result, request, state) => this.HandleHttpResponse(peer, rpcRequest.OperationRequest.OperationCode, result, rpcRequest, request, uri);


            var webRequest = (HttpWebRequest)WebRequest.Create(uri);

            webRequest.Proxy       = null;
            webRequest.Method      = "POST";
            webRequest.ContentType = "application/json";
            webRequest.Accept      = "application/json";
            webRequest.Timeout     = this.httpRequestTimeout;

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("WebRpc request {0}:{1}", uri, data);
            }

            this.httpRequestQueue.Enqueue(webRequest, binData, callback, null);

            return(true);
        }
        private void ExecuteRequest(string clientQueryStringParamters, byte[] postData, Action <AsyncHttpResponse, ClientAuthenticationQueue> callback, object state)
        {
            var clientAuthenticationRequestUrl = string.Empty;

            try
            {
                clientAuthenticationRequestUrl = this.ConcatenateQueryString(this.Uri, new[] { this.QueryStringParameters, clientQueryStringParamters });

                var webRequest = (HttpWebRequest)WebRequest.Create(clientAuthenticationRequestUrl);
                webRequest.Proxy   = null;
                webRequest.Timeout = this.requestTimeoutMilliseconds;

                HttpRequestQueueCallback queueCallback =
                    (result, httpRequest, userState) =>
                    this.fiber.Enqueue(() => this.OnCallback(result, httpRequest, userState, callback));


                if (postData != null)
                {
                    webRequest.Method = "POST";
                    this.httpRequestQueue.Enqueue(webRequest, postData, queueCallback, state);
                }
                else
                {
                    webRequest.Method = "GET";
                    this.httpRequestQueue.Enqueue(webRequest, queueCallback, state);
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Exception during ExecuteRequest to url '{0}'. Exception:{1}", clientAuthenticationRequestUrl, ex);
                ThreadPool.QueueUserWorkItem(delegate { callback(new AsyncHttpResponse(HttpRequestQueueResultCode.Error, this.RejectIfUnavailable, state), this); });
            }
        }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
 private static void EnqueuWebRequest(HttpRequestQueue queue, HttpRequest request, int RequestRetryCount, HttpWebRequest webRequest, HttpRequestQueueCallback callback)
 {
     switch (webRequest.Method)
     {
     case "GET":
     case "TRACE":
     case "HEAD":
         queue.Enqueue(webRequest, callback, request.UserState, RequestRetryCount);
         return;
     }
     if (request.DataStream != null)
     {
         queue.Enqueue(webRequest, request.DataStream.ToArray(), callback, request.UserState, RequestRetryCount);
     }
     else
     {
         queue.Enqueue(webRequest, callback, request.UserState, RequestRetryCount);
     }
 }
Beispiel #8
0
 public void Enqueue(HttpWebRequest webRequest, HttpRequestQueueCallback callback, object state)
 {
     this.fiber.Enqueue(() => this.ExecuteRequest(webRequest, callback, state));
 }
Beispiel #9
0
        public void Enqueue(string requestUri, HttpRequestQueueCallback callback, object state)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);

            this.fiber.Enqueue(() => this.ExecuteRequest(webRequest, callback, state));
        }
 public void Enqueue(HttpWebRequest webRequest, HttpRequestQueueCallback callback, object state)
 {
     this.fiber.Enqueue(() => this.ExecuteRequest(webRequest, callback, state));
 }
 public void Enqueue(string requestUri, HttpRequestQueueCallback callback, object state)
 {
     var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
     this.fiber.Enqueue(() => this.ExecuteRequest(webRequest, callback, state));
 }
        private void ExecuteRequest(HttpWebRequest webRequest, HttpRequestQueueCallback callback, object state)
        {
            if (this.queue.Count > this.MaxQueuedRequests)
            {
                callback(HttpRequestQueueResultCode.QueueFull, null, state);
                return;
            }

            var request = new QueuedRequest { Request = webRequest, Callback = callback, PostData = null, State = state, };

            switch (this.QueueState)
            {
                case HttpRequestQueueState.Connecting:
                    this.ExecuteRequestConnecting(request);
                    break;

                case HttpRequestQueueState.Reconnecting:
                    this.ExecuteRequestReconnecting(request);
                    break;

                case HttpRequestQueueState.Running:
                    this.ExecuteRequestOnline(request);
                    break;

                case HttpRequestQueueState.Offline:
                    this.ExecuteRequestOffline(request);
                    break;
            }
        }