mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 12:42:22 -04:00
Don't create a separate thread just to initalise a timer.
This commit is contained in:
parent
220c5b0512
commit
7e8a5b35a1
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2012 MCForge
|
Copyright 2012 MCForge
|
||||||
|
|
||||||
Dual-licensed under the Educational Community License, Version 2.0 and
|
Dual-licensed under the Educational Community License, Version 2.0 and
|
||||||
the GNU General Public License, Version 3 (the "Licenses"); you may
|
the GNU General Public License, Version 3 (the "Licenses"); you may
|
||||||
not use this file except in compliance with the Licenses. You may
|
not use this file except in compliance with the Licenses. You may
|
||||||
obtain a copy of the Licenses at
|
obtain a copy of the Licenses at
|
||||||
@ -26,139 +26,85 @@ namespace MCGalaxy {
|
|||||||
|
|
||||||
public static class Heart {
|
public static class Heart {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary> The max number of retries it runs for a beat </summary>
|
||||||
/// The max number of retries it runs for a beat
|
|
||||||
/// </summary>
|
|
||||||
public const int MAX_RETRIES = 3;
|
public const int MAX_RETRIES = 3;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary> Gets or sets a value indicating whether this instance can beat. </summary>
|
||||||
/// Gets or sets a value indicating whether this instance can beat.
|
/// <value> <c>true</c> if this instance can beat; otherwise, <c>false</c>. </value>
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// <c>true</c> if this instance can beat; otherwise, <c>false</c>.
|
|
||||||
/// </value>
|
|
||||||
public static bool CanBeat { get; set; }
|
public static bool CanBeat { get; set; }
|
||||||
|
|
||||||
static Timer Timer;
|
static Timer timer;
|
||||||
static object Lock = new object();
|
|
||||||
|
|
||||||
|
readonly static IBeat[] Beats = {
|
||||||
private readonly static IBeat[] Beats = {
|
|
||||||
|
|
||||||
//Keep in this order.
|
|
||||||
new ClassiCubeBeat()
|
new ClassiCubeBeat()
|
||||||
};
|
}; //Keep these in order
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static Heart() {
|
static Heart() {
|
||||||
Thread t = new Thread(new ThreadStart(() => {
|
timer = new Timer(
|
||||||
Timer = new Timer(OnBeat, null,
|
OnBeat, null,
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
6000, 6000
|
6000, 6000
|
||||||
#else
|
#else
|
||||||
45000, 45000
|
45000, 45000
|
||||||
#endif
|
#endif
|
||||||
);
|
);
|
||||||
}));
|
|
||||||
t.Name = "MCG_Heartbeat";
|
|
||||||
t.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnBeat(object state) {
|
static void OnBeat(object state) {
|
||||||
for ( int i = 0; i < Beats.Length; i++ ) {
|
for (int i = 0; i < Beats.Length; i++) {
|
||||||
if ( Beats[i].Persistance )
|
if (Beats[i].Persistance) Pump(Beats[i]);
|
||||||
Pump(Beats[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Inits this instance. </summary>
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Inits this instance.
|
|
||||||
/// </summary>
|
|
||||||
public static void Init() {
|
public static void Init() {
|
||||||
if ( Server.logbeat ) {
|
if (Server.logbeat && !File.Exists("heartbeat.log")) {
|
||||||
if ( !File.Exists("heartbeat.log") ) {
|
using (File.Create("heartbeat.log")) { }
|
||||||
using ( File.Create("heartbeat.log") ) { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CanBeat = true;
|
CanBeat = true;
|
||||||
|
for (int i = 0; i < Beats.Length; i++)
|
||||||
for ( int i = 0; i < Beats.Length; i++ )
|
|
||||||
Pump(Beats[i]);
|
Pump(Beats[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary> Pumps the specified beat. </summary>
|
||||||
/// Pumps the specified beat.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="beat">The beat.</param>
|
/// <param name="beat">The beat.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static void Pump(IBeat beat) {
|
public static void Pump(IBeat beat) {
|
||||||
|
if(!CanBeat) return;
|
||||||
if(!CanBeat)
|
|
||||||
return;
|
|
||||||
|
|
||||||
byte[] data = Encoding.ASCII.GetBytes(beat.Prepare());
|
byte[] data = Encoding.ASCII.GetBytes(beat.Prepare());
|
||||||
|
|
||||||
for ( int i = 0; i < MAX_RETRIES; i++ ) {
|
for (int i = 0; i < MAX_RETRIES; i++) {
|
||||||
try {
|
try {
|
||||||
var request = WebRequest.Create(beat.URL) as HttpWebRequest;
|
var req = WebRequest.Create(beat.URL) as HttpWebRequest;
|
||||||
request.Method = "POST";
|
req.Method = "POST";
|
||||||
request.ContentType = "application/x-www-form-urlencoded";
|
req.ContentType = "application/x-www-form-urlencoded";
|
||||||
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||||
request.Timeout = 15000;
|
req.Timeout = 15000;
|
||||||
request.ContentLength = data.Length;
|
req.ContentLength = data.Length;
|
||||||
|
|
||||||
using ( var writer = request.GetRequestStream() ) {
|
using (var w = req.GetRequestStream()) {
|
||||||
writer.Write(data, 0, data.Length);
|
w.Write(data, 0, data.Length);
|
||||||
|
|
||||||
if ( Server.logbeat )
|
if (Server.logbeat)
|
||||||
Server.s.Log("Beat " + beat.ToString() + " was sent");
|
Server.s.Log("Beat " + beat.ToString() + " was sent");
|
||||||
}
|
}
|
||||||
|
|
||||||
using ( var reader = new StreamReader(request.GetResponse().GetResponseStream()) ) {
|
using (var r = new StreamReader(req.GetResponse().GetResponseStream())) {
|
||||||
string read = reader.ReadToEnd().Trim();
|
string read = r.ReadToEnd().Trim();
|
||||||
beat.OnResponse(read);
|
beat.OnResponse(read);
|
||||||
|
|
||||||
if ( Server.logbeat )
|
if (Server.logbeat)
|
||||||
Server.s.Log("Beat: \"" + read + "\" was recieved");
|
Server.s.Log("Beat: \"" + read + "\" was recieved");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( Server.logbeat )
|
if (Server.logbeat)
|
||||||
Server.s.Log("Beat: " + beat.ToString() + " failed.");
|
Server.s.Log("Beat: " + beat.ToString() + " failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Encodes the URL.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="input">The input.</param>
|
|
||||||
/// <returns>An encoded url</returns>
|
|
||||||
public static string EncodeUrl(string input) {
|
|
||||||
StringBuilder output = new StringBuilder();
|
|
||||||
for ( int i = 0; i < input.Length; i++ ) {
|
|
||||||
if ( ( input[i] >= '0' && input[i] <= '9' ) ||
|
|
||||||
( input[i] >= 'a' && input[i] <= 'z' ) ||
|
|
||||||
( input[i] >= 'A' && input[i] <= 'Z' ) ||
|
|
||||||
input[i] == '-' || input[i] == '_' || input[i] == '.' || input[i] == '~' ) {
|
|
||||||
output.Append(input[i]);
|
|
||||||
}
|
|
||||||
else if ( Array.IndexOf<char>(ReservedChars, input[i]) != -1 ) {
|
|
||||||
output.Append('%').Append(( (int)input[i] ).ToString("X"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly char[] ReservedChars = { ' ', '!', '*', '\'', '(', ')', ';', ':', '@', '&', '=', '+', '$', ',', '/', '?', '%', '#', '[', ']' };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,8 @@ namespace MCGalaxy {
|
|||||||
|
|
||||||
void DoLocationChecks() {
|
void DoLocationChecks() {
|
||||||
while (true) {
|
while (true) {
|
||||||
Player[] players = PlayerInfo.Online.Items;
|
Player[] players = PlayerInfo.Online.Items;
|
||||||
Thread.Sleep(players.Length == 0 ? 16 : 3);
|
Thread.Sleep(players.Length == 0 ? 16 : 3);
|
||||||
players = PlayerInfo.Online.Items;
|
players = PlayerInfo.Online.Items;
|
||||||
|
|
||||||
for (int i = 0; i < players.Length; i++) {
|
for (int i = 0; i < players.Length; i++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user