Ejemplo n.º 1
0
        public static bool Pump(Beat beat)
        {
            //lock (Lock)
            //{
                String beattype = beat.GetType().Name;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(beat.URL));

                beat.Parameters = DefaultParameters;

                if (beat.Log)
                {
                    beatlogger = new StreamWriter("heartbeat.log", true);
                }

                int totalTries = 0;
                int totalTriesStream = 0;

            retry: try
                {
                    totalTries++;
                    totalTriesStream = 0;

                    beat.Prepare();

                    // Set all the request settings
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    byte[] formData = Encoding.ASCII.GetBytes(beat.Parameters);
                    request.ContentLength = formData.Length;
                    request.Timeout = 15000; // 15 seconds

              retryStream: try
                    {
                        totalTriesStream++;
                        using (Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(formData, 0, formData.Length);
                            if (Server.logbeat && beat.Log)
                            {
                                BeatLog(beat, beattype + " request sent at " + DateTime.Now.ToString());
                            }
                            requestStream.Flush();
                            requestStream.Close();
                        }
                    }
                    catch (WebException e)
                    {
                        //Server.ErrorLog(e);
                        if (e.Status == WebExceptionStatus.Timeout)
                        {
                            if (Server.logbeat && beat.Log)
                            {
            #if DEBUG
                                Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
            #endif
                                BeatLog(beat, beattype + " timeout detected at " + DateTime.Now.ToString());
                            }
                            if (totalTriesStream < max_retries)
                            {
                                goto retryStream;
                            }
                            else
                            {
                                if (Server.logbeat && beat.Log)
                                    BeatLog(beat, beattype + " timed out " + max_retries + " times. Aborting this request. " + DateTime.Now.ToString());
                                Server.s.Log(beattype + " timed out " + max_retries + " times. Aborting this request.");
                                //throw new WebException("Failed during request.GetRequestStream()", e.InnerException, e.Status, e.Response);
                                beatlogger.Close();
                                return false;
                            }
                        }
                        else if (Server.logbeat && beat.Log)
                        {
            #if DEBUG
                            Server.s.Log(beattype + " non-timeout exception detected: " + e.Message);
            #endif
                            BeatLog(beat, beattype + " non-timeout exception detected: " + e.Message);
                            BeatLog(beat, "Stack Trace: " + e.StackTrace);
                        }
                    }

                    //if (hash == null)
                    //{
                    using (WebResponse response = request.GetResponse())
                    {
                        using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
                        {
                            if (Server.logbeat && beat.Log)
                            {
            #if DEBUG
                                Server.s.Log(beattype + " response received at " + DateTime.Now.ToString());
            #endif
                                BeatLog(beat, beattype + " response received at " + DateTime.Now.ToString());
                            }

                            if (String.IsNullOrEmpty(hash) && response.ContentLength > 0)
                            {
                                // Instead of getting a single line, get the whole damn thing and we'll strip stuff out
                                string line = responseReader.ReadToEnd().Trim();
                                if (Server.logbeat && beat.Log)
                                {
                                    BeatLog(beat, "Received: " + line);
                                }

                                beat.OnPump(line);
                            }
                            else
                            {
                                beat.OnPump(String.Empty);
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    if (e.Status == WebExceptionStatus.Timeout)
                    {
                        if (Server.logbeat && beat.Log)
                        {
            #if DEBUG
                            Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
            #endif
                            BeatLog(beat, "Timeout detected at " + DateTime.Now.ToString());
                        }
                        Pump(beat);
                    }
                }
                catch (Exception e)
                {
                    if (Server.logbeat && beat.Log)
                    {
                        BeatLog(beat, beattype + " failure #" + totalTries + " at " + DateTime.Now.ToString());
                    }
                    if (totalTries < max_retries) goto retry;
                    if (Server.logbeat && beat.Log)
                    {
            #if DEBUG
                        Server.s.Log(beattype + " failed " + max_retries + " times.  Stopping.");
            #endif
                        BeatLog(beat, "Failed " + max_retries + " times.  Stopping.");
                        beatlogger.Close();
                    }
                    return false;
                }
                finally
                {
                    request.Abort();
                }
                if (beatlogger != null)
                {
                    beatlogger.Close();
                }
            //}
            return true;
        }
Ejemplo n.º 2
0
        public static bool Pump(Beat beat)
        {
            //lock (Lock)
            //{
            String beattype = beat.GetType().Name;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(beat.URL));

            beat.Parameters = DefaultParameters;

            if (beat.Log)
            {
                beatlogger = new StreamWriter("heartbeat.log", true);
            }

            int totalTries       = 0;
            int totalTriesStream = 0;

            retry : try
            {
                totalTries++;
                totalTriesStream = 0;

                beat.Prepare();
                if (beat.GetType() == typeof(MinecraftBeat))
                {
                    File.WriteAllText("text/heartbeaturl.txt", beat.URL + "?" + beat.Parameters, Encoding.UTF8);
                }

                // Set all the request settings
                //Server.s.Log(beat.Parameters);
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent   = "Mozilla/5.0 (compatible; Konqueror/3.5; Windows NT 6.0) KHTML/3.5.6 (like Gecko)";
                request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                byte[] formData = Encoding.ASCII.GetBytes(beat.Parameters);
                request.ContentLength = formData.Length;
                request.Timeout       = 15000; // 15 seconds

                retryStream : try
                {
                    totalTriesStream++;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(formData, 0, formData.Length);
                        if (Server.logbeat && beat.Log)
                        {
                            BeatLog(beat, beattype + " request sent at " + DateTime.Now.ToString());
                        }
                        requestStream.Flush();
                        requestStream.Close();
                    }
                }
                catch (WebException e)
                {
                    //Server.ErrorLog(e);
                    if (e.Status == WebExceptionStatus.Timeout)
                    {
                        if (Server.logbeat && beat.Log)
                        {
#if DEBUG
                            Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
#endif
                            BeatLog(beat, beattype + " timeout detected at " + DateTime.Now.ToString());
                        }
                        if (totalTriesStream < max_retries)
                        {
                            goto retryStream;
                        }
                        else
                        {
                            if (Server.logbeat && beat.Log)
                            {
                                BeatLog(beat, beattype + " timed out " + max_retries + " times. Aborting this request. " + DateTime.Now.ToString());
                            }
                            Server.s.Log(beattype + " timed out " + max_retries + " times. Aborting this request.");
                            //throw new WebException("Failed during request.GetRequestStream()", e.InnerException, e.Status, e.Response);
                            beatlogger.Close();
                            return(false);
                        }
                    }
                    else if (Server.logbeat && beat.Log)
                    {
#if DEBUG
                        Server.s.Log(beattype + " non-timeout exception detected: " + e.Message);
#endif
                        BeatLog(beat, beattype + " non-timeout exception detected: " + e.Message);
                        BeatLog(beat, "Stack Trace: " + e.StackTrace);
                    }
                }

                //if (hash == null)
                //{
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
                    {
                        if (Server.logbeat && beat.Log)
                        {
#if DEBUG
                            Server.s.Log(beattype + " response received at " + DateTime.Now.ToString());
#endif
                            BeatLog(beat, beattype + " response received at " + DateTime.Now.ToString());
                        }

                        if (String.IsNullOrEmpty(hash) && response.ContentLength > 0)
                        {
                            // Instead of getting a single line, get the whole damn thing and we'll strip stuff out
                            string line = responseReader.ReadToEnd().Trim();
                            if (Server.logbeat && beat.Log)
                            {
                                BeatLog(beat, "Received: " + line);
                            }

                            beat.OnPump(line);
                        }
                        else
                        {
                            beat.OnPump(String.Empty);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    if (Server.logbeat && beat.Log)
                    {
#if DEBUG
                        Server.s.Log(beattype + " timeout detected at " + DateTime.Now.ToString());
#endif
                        BeatLog(beat, "Timeout detected at " + DateTime.Now.ToString());
                    }
                    Pump(beat);
                }
            }
            catch (Exception)
            {
                if (Server.logbeat && beat.Log)
                {
                    BeatLog(beat, beattype + " failure #" + totalTries + " at " + DateTime.Now.ToString());
                }
                if (totalTries < max_retries)
                {
                    goto retry;
                }
                if (Server.logbeat && beat.Log)
                {
#if DEBUG
                    Server.s.Log(beattype + " failed " + max_retries + " times.  Stopping.");
#endif
                    BeatLog(beat, "Failed " + max_retries + " times.  Stopping.");
                    beatlogger.Close();
                }
                return(false);
            }
            finally
            {
                request.Abort();
            }
            if (beatlogger != null)
            {
                beatlogger.Close();
            }
            //}
            return(true);
        }