added decompression on client side

This commit is contained in:
cxxpxr 2021-04-03 23:07:20 -04:00
parent 2732aa90a1
commit c666651908
2 changed files with 37 additions and 7 deletions

View file

@ -1,6 +1,9 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using UnityEngine; using UnityEngine;
namespace LightReflectiveMirror namespace LightReflectiveMirror
@ -118,4 +121,32 @@ namespace LightReflectiveMirror
return value; return value;
} }
} }
internal static class CompressorExtensions
{
/// <summary>
/// Decompresses the string.
/// </summary>
/// <param name="compressedText">The compressed text.</param>
/// <returns></returns>
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);
}
}
}
} }

View file

@ -320,7 +320,7 @@ namespace LightReflectiveMirror
_directConnectEndpoint = new IPEndPoint(IPAddress.Parse(ip), port); _directConnectEndpoint = new IPEndPoint(IPAddress.Parse(ip), port);
if (useNATPunch) if (useNATPunch && attemptNatPunch)
{ {
StartCoroutine(NATPunch(_directConnectEndpoint)); StartCoroutine(NATPunch(_directConnectEndpoint));
} }
@ -343,8 +343,7 @@ namespace LightReflectiveMirror
case OpCodes.RequestNATConnection: case OpCodes.RequestNATConnection:
if (GetLocalIp() != null && _directConnectModule != null) if (GetLocalIp() != null && _directConnectModule != null)
{ {
_NATPuncher = new UdpClient(); _NATPuncher = new UdpClient { ExclusiveAddressUse = false };
_NATPuncher.ExclusiveAddressUse = false;
_NATIP = new IPEndPoint(IPAddress.Parse(GetLocalIp()), UnityEngine.Random.Range(16000, 17000)); _NATIP = new IPEndPoint(IPAddress.Parse(GetLocalIp()), UnityEngine.Random.Range(16000, 17000));
_NATPuncher.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _NATPuncher.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_NATPuncher.Client.Bind(_NATIP); _NATPuncher.Client.Bind(_NATIP);
@ -370,13 +369,13 @@ namespace LightReflectiveMirror
IEnumerator GetServerList() IEnumerator GetServerList()
{ {
string uri = $"http://{serverIP}:{endpointServerPort}/api/servers"; string uri = $"http://{serverIP}:{endpointServerPort}/api/compressed/servers";
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{ {
// Request and wait for the desired page. // Request and wait for the desired page.
yield return webRequest.SendWebRequest(); yield return webRequest.SendWebRequest();
var result = webRequest.downloadHandler.text; var result = webRequest.downloadHandler.text.Decompress();
#if UNITY_2020_1_OR_NEWER #if UNITY_2020_1_OR_NEWER
switch (webRequest.result) switch (webRequest.result)
@ -388,7 +387,7 @@ namespace LightReflectiveMirror
break; break;
case UnityWebRequest.Result.Success: 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!"); Debug.LogWarning("LRM | Server list request denied. Make sure you enable 'EndpointServerList' in server config!");
break; break;
@ -408,7 +407,7 @@ namespace LightReflectiveMirror
} }
else 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!"); Debug.LogWarning("LRM | Server list request denied. Make sure you enable 'EndpointServerList' in server config!");
} }