Reworked the StatTracker client-side
Reworked the StatTracker client side stuff to match the new server side changes. Also fixed the memory tracking, plus added a 'providerid' for certain server hosting providers
This commit is contained in:
parent
d2fe188001
commit
29abcaf612
2 changed files with 63 additions and 15 deletions
|
|
@ -7,13 +7,25 @@ using System.Threading;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using TerrariaApi.Server;
|
using TerrariaApi.Server;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
public class StatTracker
|
public class StatTracker
|
||||||
{
|
{
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
|
static extern bool GetPhysicallyInstalledSystemMemory(out long totalMemInKb);
|
||||||
|
|
||||||
|
public string ProviderToken = "";
|
||||||
|
public bool OptOut = false;
|
||||||
|
|
||||||
private bool failed;
|
private bool failed;
|
||||||
private bool initialized;
|
private bool initialized;
|
||||||
|
private long totalMem;
|
||||||
|
private string serverId = "";
|
||||||
|
|
||||||
public StatTracker()
|
public StatTracker()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -21,31 +33,56 @@ namespace TShockAPI
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
if (!initialized)
|
if (!initialized && !OptOut)
|
||||||
{
|
{
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
serverId = Guid.NewGuid().ToString(); // Gets reset every server restart
|
||||||
ThreadPool.QueueUserWorkItem(SendUpdate);
|
ThreadPool.QueueUserWorkItem(SendUpdate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SendUpdate(object info)
|
private void SendUpdate(object info)
|
||||||
{
|
{
|
||||||
Thread.Sleep(1000*60*15);
|
Thread.Sleep(1000 * 60 * 5);
|
||||||
var data = new JsonData
|
JsonData data;
|
||||||
|
|
||||||
|
if(ServerApi.RunningMono)
|
||||||
{
|
{
|
||||||
port = Terraria.Netplay.ListenPort,
|
var pc = new PerformanceCounter("Mono Memory", "Total Physical Memory");
|
||||||
currentPlayers = TShock.Utils.ActivePlayers(),
|
totalMem = (pc.RawValue / 1024 / 1024 / 1024);
|
||||||
maxPlayers = TShock.Config.MaxSlots,
|
data = new JsonData
|
||||||
systemRam = 0,
|
{
|
||||||
systemCPUClock = 0,
|
port = Terraria.Netplay.ListenPort,
|
||||||
version = TShock.VersionNum.ToString(),
|
currentPlayers = TShock.Utils.ActivePlayers(),
|
||||||
terrariaVersion = Terraria.Main.versionNumber2,
|
maxPlayers = TShock.Config.MaxSlots,
|
||||||
mono = ServerApi.RunningMono
|
systemRam = totalMem,
|
||||||
};
|
version = TShock.VersionNum.ToString(),
|
||||||
|
terrariaVersion = Terraria.Main.versionNumber2,
|
||||||
|
providerId = ProviderToken,
|
||||||
|
serverId = serverId,
|
||||||
|
mono = true
|
||||||
|
};
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
GetPhysicallyInstalledSystemMemory(out totalMem);
|
||||||
|
totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb
|
||||||
|
data = new JsonData
|
||||||
|
{
|
||||||
|
port = Terraria.Netplay.ListenPort,
|
||||||
|
currentPlayers = TShock.Utils.ActivePlayers(),
|
||||||
|
maxPlayers = TShock.Config.MaxSlots,
|
||||||
|
systemRam = totalMem,
|
||||||
|
version = TShock.VersionNum.ToString(),
|
||||||
|
terrariaVersion = Terraria.Main.versionNumber2,
|
||||||
|
providerId = ProviderToken,
|
||||||
|
serverId = serverId,
|
||||||
|
mono = false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data);
|
var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data);
|
||||||
var encoded = HttpUtility.UrlEncode(serialized);
|
var encoded = HttpUtility.UrlEncode(serialized);
|
||||||
var uri = String.Format("http://stats.tshock.co/publish/{0}", encoded);
|
var uri = String.Format("http://stats.tshock.co/submit/{0}", encoded);
|
||||||
var client = (HttpWebRequest)WebRequest.Create(uri);
|
var client = (HttpWebRequest)WebRequest.Create(uri);
|
||||||
client.Timeout = 5000;
|
client.Timeout = 5000;
|
||||||
try
|
try
|
||||||
|
|
@ -78,10 +115,11 @@ namespace TShockAPI
|
||||||
public int port;
|
public int port;
|
||||||
public int currentPlayers;
|
public int currentPlayers;
|
||||||
public int maxPlayers;
|
public int maxPlayers;
|
||||||
public int systemRam;
|
public long systemRam;
|
||||||
public int systemCPUClock;
|
|
||||||
public string version;
|
public string version;
|
||||||
public string terrariaVersion;
|
public string terrariaVersion;
|
||||||
|
public string providerId;
|
||||||
|
public string serverId;
|
||||||
public bool mono;
|
public bool mono;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -682,6 +682,16 @@ namespace TShockAPI
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "-providertoken":
|
||||||
|
{
|
||||||
|
TShock.StatTracker.ProviderToken = parms[++i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "--stats-optout":
|
||||||
|
{
|
||||||
|
TShock.StatTracker.OptOut = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue