Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Diagnostics;
- using Slapshot.Backbone.Messages;
- public class MtrRunner : MonoBehaviour {
- private Process process = null;
- public void StartReport(string endpoint, int cycles) {
- if(!Utils.IsSteamPlatform()) {
- UnityEngine.Debug.Log("Unable to start MTR report, platform not supported");
- return;
- }
- if(process != null && process.HasExited) {
- process = null;
- }
- if(process != null) {
- UnityEngine.Debug.Log("Unable to start MTR report, one is already in progress");
- return;
- }
- try {
- process = new Process() {
- EnableRaisingEvents = false,
- StartInfo = {
- #if UNITY_EDITOR
- FileName = $"{Application.dataPath}/plugins/SlapshotMTR.exe",
- #else
- FileName = $"{Application.dataPath}/Managed/SlapshotMTR.exe",
- #endif
- Arguments = $"{endpoint} {cycles}",
- UseShellExecute = false,
- CreateNoWindow = true,
- RedirectStandardOutput = true,
- RedirectStandardInput = true,
- RedirectStandardError = true,
- WindowStyle = ProcessWindowStyle.Hidden,
- },
- };
- process.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
- process.ErrorDataReceived += new DataReceivedEventHandler(ErrorReceived);
- process.Exited += (object sender, EventArgs e) => KillProcess();
- process.Start();
- process.BeginOutputReadLine();
- UnityEngine.Debug.Log("Successfully started MTR Runner...");
- } catch (Exception e) {
- UnityEngine.Debug.LogError("Unable to started MTR Runner: " + e.Message);
- }
- }
- private void DataReceived(object sender, DataReceivedEventArgs eventArgs) {
- UnityMainThreadDispatcher.Instance().Enqueue(() => {
- UnityEngine.Debug.Log(eventArgs.Data);
- if(!Utils.IsJsonString(eventArgs.Data)) {
- return;
- }
- BackboneSocket socket = AppManager.Instance.BackboneSocket;
- if(socket == null || !socket.IsConnected) {
- UnityEngine.Debug.Log("Not connected to the Slapshot Services, cannot send MTR report");
- return;
- }
- // Send it over
- UnityEngine.Debug.Log("Sending over latency data");
- socket.Send(new MTRReportMessage(eventArgs.Data));
- });
- }
- private void ErrorReceived(object sender, DataReceivedEventArgs eventArgs) {
- UnityMainThreadDispatcher.Instance().Enqueue(() => {
- UnityEngine.Debug.LogError(eventArgs.Data);
- });
- }
- public void OnApplicationQuit() {
- KillProcess();
- }
- private void KillProcess() {
- if (process != null && !process.HasExited) {
- process.Kill();
- }
- process = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement