Created
February 11, 2022 17:18
-
-
Save James-Frowen/9c8aacc850b64c033c56c0a0ed17ec93 to your computer and use it in GitHub Desktop.
Message tracker for mirror
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2021 James Frowen | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using System; | |
using System.IO; | |
using Mirror; | |
using Mirror.RemoteCalls; | |
using UnityEngine; | |
namespace JamesFrowen.NetworkProfiling | |
{ | |
public sealed class MessageTracker : IDisposable | |
{ | |
readonly StreamWriter writer; | |
public MessageTracker(string savefile) | |
{ | |
writer = new StreamWriter(savefile) | |
{ | |
AutoFlush = true | |
}; | |
NetworkDiagnostics.InMessageEvent += InMessageEvent; | |
NetworkDiagnostics.OutMessageEvent += OutMessageEvent; | |
} | |
private void InMessageEvent(NetworkDiagnostics.MessageInfo obj) | |
{ | |
writer.WriteLine($"{Time.time}, [IN], {GetMethodName(obj)}, {obj.bytes}, {obj.count}, {obj.count * obj.bytes}"); | |
} | |
private void OutMessageEvent(NetworkDiagnostics.MessageInfo obj) | |
{ | |
writer.WriteLine($"{Time.time}, [OUT], {GetMethodName(obj)}, {obj.bytes}, {obj.count}, {obj.count * obj.bytes}"); | |
} | |
public void Dispose() | |
{ | |
writer.Dispose(); | |
NetworkDiagnostics.InMessageEvent -= InMessageEvent; | |
NetworkDiagnostics.OutMessageEvent -= OutMessageEvent; | |
} | |
string GetMethodName(NetworkDiagnostics.MessageInfo obj) | |
{ | |
NetworkMessage message = obj.message; | |
switch (message) | |
{ | |
case CommandMessage msg: | |
return "CMD." + GetMethodName(msg.functionHash, "InvokeUserCode_"); | |
case RpcMessage msg: | |
return "RPC." + GetMethodName(msg.functionHash, "InvokeUserCode_"); | |
default: | |
return message.GetType().Name; | |
} | |
} | |
string GetMethodName(int functionHash, string prefix) | |
{ | |
string fullMethodName = RemoteProcedureCalls.GetDelegate(functionHash).Method.Name; | |
if (fullMethodName.StartsWith(prefix, StringComparison.Ordinal)) | |
return fullMethodName.Substring(prefix.Length); | |
return fullMethodName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment