SignalGo/SignalGo-full-net

Compression Example

larsontim12 opened this issue · 2 comments

I see that you have added compression to the code to use zip. Do you have an example code of how to use it?

Thanks,
Tim

I found an example in the tests Never mind.

Hello @larsontim12 and thank you for your question,

this is an example to use compression in client and server side of signalgo:
Help:
1.use SetServer static method to set compression for your server provider
2.use SetClient static method to set compression for your clientprovider

Note:
this is for signalgo duplex protocol

public class GoCompression : ICompression
    {
        public byte[] Compress(ref byte[] input)
        {
            //ConsoleGo.WriteLine($"Compressing bytes length {input.Length}", ConsoleColor.Magenta);
            using (var outStream = new MemoryStream())
            {
                using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
                using (var mStream = new MemoryStream(input))
                    mStream.CopyTo(tinyStream);
                var result = outStream.ToArray();
                //ConsoleGo.WriteLine($"Compressed bytes length {result.Length}", ConsoleColor.Magenta);
                return result;
            }
        }

        public byte[] Decompress(ref byte[] input)
        {
            //ConsoleGo.WriteLine($"Decompressing bytes length {input.Length}", ConsoleColor.Magenta);
            using (var inStream = new MemoryStream(input))
            using (var bigStream = new GZipStream(inStream, CompressionMode.Decompress))
            using (var bigStreamOut = new MemoryStream())
            {
                bigStream.CopyTo(bigStreamOut);
                var result = bigStreamOut.ToArray();
                //ConsoleGo.WriteLine($"Decompressed bytes length {result.Length}", ConsoleColor.Magenta);
                return result;
            }
        }

        public static void SetServer(ServerProvider serverProvider)
        {
            serverProvider.CurrentCompressionMode = SignalGo.CompressMode.Custom;
            serverProvider.GetCustomCompression = () => new GoCompression();
        }
        public static void SetClient(ClientProvider clientProvider)
        {
            clientProvider.CurrentCompressionMode = SignalGo.CompressMode.Custom;
            clientProvider.GetCustomCompression = () => new GoCompression();
        }
`