Skip to content

Commit

Permalink
Initial Addition of Network Update Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
features-not-bugs committed Feb 3, 2021
1 parent d501188 commit 7c55fd1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/HarmonyPatch/HarmonyPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<!-- make all references non-private, so they won't be copied to the output folder -->
<Target Name="ClearReferenceCopyLocalPaths" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" />
</ItemGroup>
</Target>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\deps\0Harmony.dll</HintPath>
Expand Down Expand Up @@ -353,6 +359,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="NetWrite\Send_Patch.cs" />
<Compile Include="NetWrite\PacketID_Patch.cs" />
<Compile Include="Performance\FPSTimer_Patch.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
32 changes: 32 additions & 0 deletions src/HarmonyPatch/NetWrite/PacketID_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Harmony;
using Network;
using Oxide.Core;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;

namespace RustServerMetrics_HarmonyPatch.NetWrite
{
[HarmonyPatch(typeof(Network.NetWrite), nameof(Network.NetWrite.PacketID))]
public class PacketID_Patch
{
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions)
{
List<CodeInstruction> retList = new List<CodeInstruction>(originalInstructions);

var methodInfo = typeof(Interface).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).FirstOrDefault(x => x.Name == "CallHook" && x.GetParameters().Count() == 2 && x.GetParameters()[1].ParameterType == typeof(object));

retList.InsertRange(retList.Count - 1, new List<CodeInstruction>
{
new CodeInstruction(OpCodes.Ldstr, "OnNetWritePacketID"),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Box, typeof(Message.Type)),
new CodeInstruction(OpCodes.Call, methodInfo),
new CodeInstruction(OpCodes.Pop)
});

return retList;
}
}
}
33 changes: 33 additions & 0 deletions src/HarmonyPatch/NetWrite/Send_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Harmony;
using Network;
using Oxide.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;

namespace RustServerMetrics_HarmonyPatch.NetWrite
{
[HarmonyPatch(typeof(Network.NetWrite), nameof(Network.NetWrite.Send))]
public class Send_Patch
{
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions)
{
List<CodeInstruction> retList = new List<CodeInstruction>(originalInstructions);

var methodInfo = typeof(Interface).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).FirstOrDefault(x => x.Name == "CallHook" && x.GetParameters().Count() == 2 && x.GetParameters()[1].ParameterType == typeof(object));

retList.InsertRange(retList.Count - 1, new List<CodeInstruction>
{
new CodeInstruction(OpCodes.Ldstr, "OnNetWriteSend"),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Box, typeof(SendInfo)),
new CodeInstruction(OpCodes.Call, methodInfo),
new CodeInstruction(OpCodes.Pop)
});

return retList;
}
}
}
61 changes: 60 additions & 1 deletion src/Plugin/ServerMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

namespace Oxide.Plugins
{
[Info("Server Metrics", "Pinkstink", "1.0.12")]
[Info("Server Metrics", "Pinkstink", "1.0.13")]
class ServerMetrics : RustPlugin
{
readonly StringBuilder _stringBuilder = new StringBuilder();
Expand All @@ -30,6 +31,8 @@ Uri BaseUri
}
}
ReportUploader _reportUploader;
readonly Dictionary<Message.Type, int> _networkUpdates = new Dictionary<Message.Type, int>();
Message.Type _lastMessageType;

void OnServerInitialized()
{
Expand Down Expand Up @@ -59,12 +62,22 @@ void OnServerInitialized()
}

Instance = this;
var messageTypes = Enum.GetValues(typeof(Message.Type));
foreach (Message.Type messageType in messageTypes)
{
if (_networkUpdates.ContainsKey(messageType))
continue;
_networkUpdates.Add(messageType, 0);
}
_reportUploader = new GameObject().AddComponent<ReportUploader>();
Subscribe(nameof(OnPerformanceReportGenerated));
Subscribe(nameof(OnPlayerDisconnected));
foreach (var player in BasePlayer.activePlayerList)
OnPlayerConnected(player);
Subscribe(nameof(OnPlayerConnected));
Subscribe(nameof(OnNetWritePacketID));
Subscribe(nameof(OnNetWriteSend));
ServerMgr.Instance.InvokeRepeating(LogNetworkUpdates, UnityEngine.Random.Range(0.05f, 0.15f), 0.1f);
}

void OnPlayerConnected(BasePlayer player)
Expand All @@ -82,6 +95,46 @@ void OnPlayerDisconnected(BasePlayer player, string reason)
_playerStatsActions.Remove(player.userID);
}

void OnNetWritePacketID(Message.Type messageType) => _lastMessageType = messageType;
void OnNetWriteSend(SendInfo sendInfo) => _networkUpdates[_lastMessageType] += sendInfo.connection != null ? 1 : sendInfo.connections.Count;

void LogNetworkUpdates()
{
if (_networkUpdates.Count < 1) return;
var epochNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
_stringBuilder.Append("network_updates,server=");
_stringBuilder.Append(config.serverTag);
_stringBuilder.Append(" ");

var enumerator = _networkUpdates.GetEnumerator();
var networkUpdate = enumerator.Current;
_stringBuilder.Append(networkUpdate.Key.ToString());
_stringBuilder.Append("=");
_stringBuilder.Append(networkUpdate.Value.ToString());
_stringBuilder.Append("i");

while (enumerator.MoveNext())
{
networkUpdate = enumerator.Current;
_stringBuilder.Append(",");
_stringBuilder.Append(networkUpdate.Key.ToString());
_stringBuilder.Append("=");
_stringBuilder.Append(networkUpdate.Value.ToString());
_stringBuilder.Append("i");
}

_stringBuilder.Append(" ");
_stringBuilder.Append(epochNow);
_reportUploader.AddToSendBuffer(_stringBuilder.ToString());
_stringBuilder.Clear();

var enumKeys = _networkUpdates.Keys.ToArray();
foreach (var key in enumKeys)
{
_networkUpdates[key] = 0;
}
}

void GatherPlayerSecondStats(BasePlayer player)
{
var epochNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
Expand All @@ -108,6 +161,9 @@ void Unload()
Unsubscribe(nameof(OnPerformanceReportGenerated));
Unsubscribe(nameof(OnPlayerConnected));
Unsubscribe(nameof(OnPlayerDisconnected));
Unsubscribe(nameof(OnNetWritePacketID));
Unsubscribe(nameof(OnNetWriteSend));
ServerMgr.Instance.CancelInvoke(LogNetworkUpdates);
foreach (var player in _playerStatsActions)
{
var basePlayer = BasePlayer.FindByID(player.Key);
Expand All @@ -125,6 +181,8 @@ void Loaded()
Unsubscribe(nameof(OnPerformanceReportGenerated));
Unsubscribe(nameof(OnPlayerConnected));
Unsubscribe(nameof(OnPlayerDisconnected));
Unsubscribe(nameof(OnNetWritePacketID));
Unsubscribe(nameof(OnNetWriteSend));
}

void OnPerformanceReportGenerated()
Expand Down Expand Up @@ -286,6 +344,7 @@ IEnumerator SendRequest()
if (request.isHttpError)
{
Debug.LogError($"Error submitting metric: {request.error}");
Debug.LogError(request.downloadHandler.text);
yield break;
}
}
Expand Down

0 comments on commit 7c55fd1

Please sign in to comment.