Ejemplo n.º 1
0
        public static byte[] ServiceOSDRequest(string url, byte[] buffer, string method, int timeout)
        {
            // MainConsole.Instance.DebugFormat("[WEB UTIL]: <{0}> start osd request for {1}, method {2}",reqnum,url,method);

            string         errorMessage  = "unknown error";
            int            tickstart     = Util.EnvironmentTickCount();
            int            tickdata      = 0;
            int            tickserialize = 0;
            HttpWebRequest request       = null;

            try
            {
                request           = (HttpWebRequest)WebRequest.Create(url);
                request.Method    = method;
                request.Timeout   = timeout;
                request.KeepAlive = false;
                request.MaximumAutomaticRedirections = 10;
                request.ReadWriteTimeout             = timeout / 4;
                request.SendChunked = true;

                // If there is some input, write it into the request
                if (buffer != null && buffer.Length > 0)
                {
                    request.ContentType   = "application/json";
                    request.ContentLength = buffer.Length; //Count bytes to send
                    using (Stream requestStream = request.GetRequestStream())
                        HttpServerHandlerHelpers.WriteChunked(requestStream, buffer);
                }

                // capture how much time was spent writing, this may seem silly
                // but with the number concurrent requests, this often blocks
                tickdata = Util.EnvironmentTickCountSubtract(tickstart);

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        tickserialize = Util.EnvironmentTickCountSubtract(tickstart) - tickdata;
                        return(HttpServerHandlerHelpers.ReadBytes(responseStream));
                    }
                }
            }
            catch (WebException we)
            {
                errorMessage = we.Message;
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse webResponse = (HttpWebResponse)we.Response;
                    if (webResponse.StatusCode == HttpStatusCode.BadRequest)
                    {
                        //AR: Removed JSON Data filling console on connecting to down regions
                        MainConsole.Instance.WarnFormat("[WebUtils]: WebException bad request to {0}", url);
                    }
                    else
                    {
                        //AR: Removed JSON Data filling console on connecting to down regions
                        MainConsole.Instance.Warn(string.Format("[WebUtils]: WebException {0} to {1}",
                                                                webResponse.StatusCode, url));
                    }
                    return(new byte[0]);
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            catch (Exception ex)
            {
                if (ex is System.UriFormatException)
                {
                    errorMessage = ex.ToString();
                }
                else
                {
                    errorMessage = ex.Message;
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            finally
            {
                if (MainConsole.Instance != null)
                {
                    if (errorMessage == "unknown error")
                    {
                        // This just dumps a warning for any operation that takes more than 500 ms
                        int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
                        if (MainConsole.Instance.IsTraceEnabled)
                        {
                            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();

                            MainConsole.Instance.Trace(
                                string.Format("[WebUtils]: osd request (URI:{0}, METHOD:{1}, UPSTACK(4):{5}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                              url, method, tickdiff, tickdata, tickserialize,
                                              (stackTrace.FrameCount > 4 ? stackTrace.GetFrame(4) : stackTrace.GetFrame(stackTrace.FrameCount - 1)).GetMethod().Name));
                        }
                        else if (MainConsole.Instance.IsDebugEnabled)
                        {
                            MainConsole.Instance.Debug(
                                string.Format("[WebUtils]: request (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                              url, method, tickdiff, tickdata, tickserialize));
                        }
                        if (tickdiff > 5000)
                        {
                            MainConsole.Instance.Info(
                                string.Format("[WebUtils]: request took too long (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                              url, method, tickdiff, tickdata, tickserialize));
                        }
                    }
                }
            }

            if (MainConsole.Instance != null)
            {
                using (MemoryStream stream = new MemoryStream(buffer))
                    //AR: Removed JSON Data filling console on connecting to down regions
                    MainConsole.Instance.WarnFormat("[WebUtils]: request failed: {0} to {1}", errorMessage, url);
            }
            return(new byte[0]);
        }