public object Send(object data) { try { CheckConnection(); byte[] hdr = new byte[5]; hdr[0] = (UseBJSON ? (byte)3 : (byte)0); byte[] dat = fastBinaryJSON.BJSON.ToBJSON(data); bool compressed = false; if (dat.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + dat.Length.ToString("#,#")); compressed = true; dat = MiniLZO.Compress(dat); log.Debug("new size : " + dat.Length.ToString("#,#")); } byte[] len = Helper.GetBytes(dat.Length, false); hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(len, 0, hdr, 1, 4); _client.Client.Send(hdr); _client.Client.Send(dat); byte[] rechdr = new byte[5]; using (NetworkStream n = new NetworkStream(_client.Client)) { n.Read(rechdr, 0, 5); int c = Helper.ToInt32(rechdr, 1); byte[] recd = new byte[c]; int bytesRead = 0; int chunksize = 1; while (bytesRead < c && chunksize > 0) { bytesRead += chunksize = n.Read (recd, bytesRead, c - bytesRead); } if ((rechdr[0] & (byte)4) == (byte)4) { recd = MiniLZO.Decompress(recd); } if ((rechdr[0] & (byte)3) == (byte)3) { return(fastBinaryJSON.BJSON.ToObject(recd)); } } } catch { } return(null); }
public static bool MiniLzoCompress(Stream fsIn, Stream fsOut) { var buf = new byte[LooseBufferSize]; try { while (true) { var count = fsIn.Read(buf, 0, LooseBufferSize); if (count != 0) { //compressing.Write(buf, 0, count); if (count < LooseBufferSize) { var x = new byte[count]; Array.Copy(buf, x, count); // buf = null; buf = x; } MiniLZO.Compress(buf, out var compData); var b = (byte)(compData.Length & 0xff); fsOut.WriteByte(b); b = (byte)((compData.Length >> 8) & 0xff); fsOut.WriteByte(b); fsOut.Write(compData, 0, compData.Length); // compData = null; } if (count != LooseBufferSize) { break; } } return(true); } catch (Exception ex) { Debug.WriteLine(ex); } return(false); }
private Stream MiniLZOCompressStream(Stream input) { MemoryStream memoryStream = new MemoryStream(); const int BUFFERSIZE = 10000; byte[] buffer = new byte[BUFFERSIZE]; while (true) { int bytesRead = input.Read(buffer, 0, BUFFERSIZE); if (bytesRead == 0) { break; } memoryStream.Write(buffer, 0, bytesRead); } memoryStream.Seek(0, SeekOrigin.Begin); return(new MemoryStream(MiniLZO.Compress(memoryStream))); }
public void Write(GameBoxWriter w, ClassIDRemap remap) { if (GBX.BodyCompression == 'C') { using (var msBody = new MemoryStream()) using (var gbxwBody = new GameBoxWriter(msBody, this)) { GBX.MainNode.Write(gbxwBody, remap); MiniLZO.Compress(msBody.ToArray(), out byte[] output); w.Write((int)msBody.Length); // Uncompressed w.Write(output.Length); // Compressed w.Write(output, 0, output.Length); // Compressed body data } } else { GBX.MainNode.Write(w); } // ... }
void Accept(TcpClient client) { using (NetworkStream n = client.GetStream()) { while (client.Connected) { this.count++; byte[] c = new byte[5]; n.Read(c, 0, 5); int count = BitConverter.ToInt32(c, 1); byte[] data = new byte[count]; int bytesRead = 0; int chunksize = 1; while (bytesRead < count && chunksize > 0) { bytesRead += chunksize = n.Read (data, bytesRead, count - bytesRead); } object o = fastBinaryJSON.BJSON.ToObject(data); object r = _handler(o); bool compressed = false; data = fastBinaryJSON.BJSON.ToBJSON(r); if (data.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + data.Length.ToString("#,#")); compressed = true; data = MiniLZO.Compress(data); log.Debug("new size : " + data.Length.ToString("#,#")); } if (data.Length > NetworkClient.Config.LogDataSizesOver) { log.Debug("data size (bytes) = " + data.Length.ToString("#,#")); } byte[] b = BitConverter.GetBytes(data.Length); byte[] hdr = new byte[5]; hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(b, 0, hdr, 1, 4); n.Write(hdr, 0, 5); n.Write(data, 0, data.Length); int wait = 0; while (n.DataAvailable == false) { wait++; if (wait < 10000) // kludge : for insert performance { Thread.Sleep(0); } else { Thread.Sleep(1); } // FEATURE : if wait > 10 min -> close connection } } } }
void Accept(TcpClient client) { using (NetworkStream n = client.GetStream()) { while (client.Connected) { this.count++; byte[] c = new byte[5]; n.Read(c, 0, 5); int count = BitConverter.ToInt32(c, 1); byte[] data = new byte[count]; int bytesRead = 0; int chunksize = 1; while (bytesRead < count && chunksize > 0) { bytesRead += chunksize = n.Read (data, bytesRead, count - bytesRead); } object o = fastBinaryJSON.BJSON.ToObject(data); if ((c[0] & (byte)4) == (byte)4) { data = MiniLZO.Decompress(data); } object r = _handler(o); bool compressed = false; var dataret = fastBinaryJSON.BJSON.ToBJSON(r); r = null; if (dataret.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + dataret.Length.ToString("#,#")); compressed = true; dataret = MiniLZO.Compress(dataret); log.Debug("new size : " + dataret.Length.ToString("#,#")); } if (dataret.Length > NetworkClient.Config.LogDataSizesOver) { log.Debug("data size (bytes) = " + dataret.Length.ToString("#,#")); } byte[] b = BitConverter.GetBytes(dataret.Length); byte[] hdr = new byte[5]; hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(b, 0, hdr, 1, 4); n.Write(hdr, 0, 5); n.Write(dataret, 0, dataret.Length); //n.Flush(); //return; int wait = 0; bool close = false; var dt = FastDateTime.Now; while (n.DataAvailable == false && close == false) { wait++; if (wait < 10000) // kludge : for insert performance { Thread.Sleep(0); } else { Thread.Sleep(1); // wait done -> close connection if (FastDateTime.Now.Subtract(dt).TotalSeconds > NetworkClient.Config.KillConnectionSeconds) { close = true; } } } if (close) { break; } } n.Close(); } client.Close(); }
private MemoryStream MiniLZOCompressMemoryStream(MemoryStream input) { return(new MemoryStream(MiniLZO.Compress(input))); }