From c6666519087ad43efb9ea087937f293bbc3385a2 Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Sat, 3 Apr 2021 23:07:20 -0400 Subject: [PATCH] added decompression on client side --- UnityTransport/LRMTools.cs | 31 +++++++++++++++++++ .../LightReflectiveMirrorTransport.cs | 13 ++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/UnityTransport/LRMTools.cs b/UnityTransport/LRMTools.cs index 08802eb..eaa3447 100644 --- a/UnityTransport/LRMTools.cs +++ b/UnityTransport/LRMTools.cs @@ -1,6 +1,9 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Text; using UnityEngine; namespace LightReflectiveMirror @@ -118,4 +121,32 @@ namespace LightReflectiveMirror return value; } } + + internal static class CompressorExtensions + { + /// + /// Decompresses the string. + /// + /// The compressed text. + /// + public static string Decompress(this string compressedText) + { + byte[] gZipBuffer = Convert.FromBase64String(compressedText); + using (var memoryStream = new MemoryStream()) + { + int dataLength = BitConverter.ToInt32(gZipBuffer, 0); + memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); + + var buffer = new byte[dataLength]; + + memoryStream.Position = 0; + using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) + { + gZipStream.Read(buffer, 0, buffer.Length); + } + + return Encoding.UTF8.GetString(buffer); + } + } + } } \ No newline at end of file diff --git a/UnityTransport/LightReflectiveMirrorTransport.cs b/UnityTransport/LightReflectiveMirrorTransport.cs index c539e79..b35db27 100644 --- a/UnityTransport/LightReflectiveMirrorTransport.cs +++ b/UnityTransport/LightReflectiveMirrorTransport.cs @@ -320,7 +320,7 @@ namespace LightReflectiveMirror _directConnectEndpoint = new IPEndPoint(IPAddress.Parse(ip), port); - if (useNATPunch) + if (useNATPunch && attemptNatPunch) { StartCoroutine(NATPunch(_directConnectEndpoint)); } @@ -343,8 +343,7 @@ namespace LightReflectiveMirror case OpCodes.RequestNATConnection: if (GetLocalIp() != null && _directConnectModule != null) { - _NATPuncher = new UdpClient(); - _NATPuncher.ExclusiveAddressUse = false; + _NATPuncher = new UdpClient { ExclusiveAddressUse = false }; _NATIP = new IPEndPoint(IPAddress.Parse(GetLocalIp()), UnityEngine.Random.Range(16000, 17000)); _NATPuncher.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _NATPuncher.Client.Bind(_NATIP); @@ -370,13 +369,13 @@ namespace LightReflectiveMirror IEnumerator GetServerList() { - string uri = $"http://{serverIP}:{endpointServerPort}/api/servers"; + string uri = $"http://{serverIP}:{endpointServerPort}/api/compressed/servers"; using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. yield return webRequest.SendWebRequest(); - var result = webRequest.downloadHandler.text; + var result = webRequest.downloadHandler.text.Decompress(); #if UNITY_2020_1_OR_NEWER switch (webRequest.result) @@ -388,7 +387,7 @@ namespace LightReflectiveMirror break; case UnityWebRequest.Result.Success: - if (result == "Access Denied") + if (result.Contains("403:Forbidden")) { Debug.LogWarning("LRM | Server list request denied. Make sure you enable 'EndpointServerList' in server config!"); break; @@ -408,7 +407,7 @@ namespace LightReflectiveMirror } else { - if (result == "Access Denied") + if (result.Contains("403:Forbidden")) { Debug.LogWarning("LRM | Server list request denied. Make sure you enable 'EndpointServerList' in server config!"); }