Allocate less memory in SendRawMap.

This commit is contained in:
UnknownShadow200 2016-02-23 21:07:26 +11:00
parent 55dbf8d3a4
commit d37b651761
2 changed files with 27 additions and 23 deletions

View File

@ -338,10 +338,11 @@ namespace MCGalaxy {
} }
SendRaw(Opcode.LevelInitialise); SendRaw(Opcode.LevelInitialise);
buffer = buffer.GZip(); int usedLength = 0;
int totalRead = 0; buffer = buffer.GZip(out usedLength);
int totalRead = 0;
while (totalRead < buffer.Length) { while (totalRead < usedLength) {
byte[] packet = new byte[1028]; // need each packet separate for Mono byte[] packet = new byte[1028]; // need each packet separate for Mono
packet[0] = Opcode.LevelDataChunk; packet[0] = Opcode.LevelDataChunk;
short length = (short)Math.Min(buffer.Length - totalRead, 1024); short length = (short)Math.Min(buffer.Length - totalRead, 1024);

View File

@ -24,33 +24,36 @@ using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace MCGalaxy
{ namespace MCGalaxy {
public static class Extensions
{ public static class Extensions {
public static string Truncate(this string source, int maxLength)
{ public static string Truncate(this string source, int maxLength) {
if (source.Length > maxLength) if (source.Length > maxLength)
{
source = source.Substring(0, maxLength); source = source.Substring(0, maxLength);
}
return source; return source;
} }
public static byte[] GZip(this byte[] bytes)
{ public static byte[] GZip(this byte[] bytes) {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) using (MemoryStream ms = new MemoryStream()) {
{ using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true))
GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true); gs.Write(bytes, 0, bytes.Length);
gs.Write(bytes, 0, bytes.Length);
gs.Close();
ms.Position = 0; ms.Position = 0;
bytes = new byte[ms.Length]; return ms.ToArray();
ms.Read(bytes, 0, (int)ms.Length);
ms.Close();
ms.Dispose();
} }
return bytes;
} }
public static byte[] GZip(this byte[] bytes, out int length) {
using (MemoryStream ms = new MemoryStream()) {
using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true))
gs.Write(bytes, 0, bytes.Length);
length = (int)ms.Length;
return ms.GetBuffer();
}
}
public static byte[] Decompress(this byte[] gzip, int capacity = 16) public static byte[] Decompress(this byte[] gzip, int capacity = 16)
{ {
// Create a GZIP stream with decompression mode. // Create a GZIP stream with decompression mode.