Example #1
0
        /// <summary>
        /// Gets a Gzip decompressed byte array from a GZipped byte array
        /// </summary>
        /// <param name="value">GZipped byte array</param>
        /// <returns>Decompressed array</returns>
        public static SubArray <byte> FromGzip(this SubArray <byte> value)
        {
            if (!value.IsGzip())
            {
                return(value);
            }
            var gzipCompressor = CompressorManager.Get <GZipCompressor>() ?? new GZipCompressor();

            return(gzipCompressor.Decompress(value));
        }
Example #2
0
        /// <summary>
        /// Gets a GZip byte array from the source byte array
        /// </summary>
        /// <param name="value">Source byte array</param>
        /// <returns>GZipped byte array</returns>
        public static SubArray <byte> ToGzip(this byte[] value)
        {
            if (value.IsGzip())
            {
                return(value);
            }
            var gzipCompressor = CompressorManager.Get <GZipCompressor>() ?? new GZipCompressor();

            return(gzipCompressor.Compress(value));
        }
Example #3
0
        private void RunTest(object value, int times, bool useGZip)
        {
            var vType          = value?.GetType() ?? typeof(object);
            var compressor     = useGZip ? CompressorManager.GetByEncodingType("gzip") : null;
            var memStream      = new RecycleMemoryStream();
            var jsonSerializer = new JsonTextSerializer {
                Compressor = compressor
            };
            var xmlSerializer = new XmlTextSerializer {
                Compressor = compressor
            };
            var binarySerializer = new BinaryFormatterSerializer {
                Compressor = compressor
            };
            var ut8JsonSerializer = new Utf8JsonTextSerializer {
                Compressor = compressor
            };
            var msgPackSerializer = new MsgPackSerializer {
                Compressor = compressor
            };
            var nBinarySerializer = new NBinarySerializer {
                Compressor = compressor
            };
            var rawBinarySerializer = new RawBinarySerializer {
                Compressor = compressor
            };
            var wBinarySerializer = new WBinarySerializer {
                Compressor = compressor
            };
            var pwBinarySerializer = new PWBinarySerializer {
                Compressor = compressor
            };

            Core.Log.Warning("Running Serializer Test. Use GZIP = {0}", useGZip);
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("By size:");
            Core.Log.InfoBasic("\tJson Bytes Count: {0}", SerializerSizeProcess(value, vType, jsonSerializer));
            Core.Log.InfoBasic("\tXml Bytes Count: {0}", SerializerSizeProcess(value, vType, xmlSerializer));
            Core.Log.InfoBasic("\tBinaryFormatter Bytes Count: {0}", SerializerSizeProcess(value, vType, binarySerializer));
            Core.Log.InfoBasic("\tUtf8Json Bytes Count: {0}", SerializerSizeProcess(value, vType, ut8JsonSerializer));
            Core.Log.InfoBasic("\tMessagePack Bytes Count: {0}", SerializerSizeProcess(value, vType, msgPackSerializer));
            Core.Log.InfoBasic("\tNBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, nBinarySerializer));
            Core.Log.InfoBasic("\tRawBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, rawBinarySerializer));
            Core.Log.InfoBasic("\tWBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, wBinarySerializer));
            Core.Log.InfoBasic("\tPortable WBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, pwBinarySerializer));
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("By Times: {0}", times);
            SerializerProcess("Json", value, vType, times, jsonSerializer, memStream);
            SerializerProcess("Utf8Json", value, vType, times, ut8JsonSerializer, memStream);
            SerializerProcess("NBinary", value, vType, times, nBinarySerializer, memStream);
            SerializerProcess("RawBinary", value, vType, times, rawBinarySerializer, memStream);
            SerializerProcess("WBinary", value, vType, times, wBinarySerializer, memStream);
            SerializerProcess("PWBinary", value, vType, times, pwBinarySerializer, memStream);
            Console.ReadLine();
        }
Example #4
0
        private void RunSingleTest(object value, int times, bool useGZip)
        {
            var vType             = value?.GetType() ?? typeof(object);
            var memStream         = new RecycleMemoryStream();
            var compressor        = useGZip ? CompressorManager.GetByEncodingType("gzip") : null;
            var nBinarySerializer = new NBinarySerializer {
                Compressor = compressor
            };

            Core.Log.Warning("Running Serializer Test. Use GZIP = {0}, Times = {1}", useGZip, times);
            SerializerProcess("NBinary", value, vType, times, nBinarySerializer, memStream, false);
        }
Example #5
0
        protected override ITransportClient CreateTransport(KeyValueCollection parameters)
        {
            var host = parameters["Host"];

            if (host == null || host == Factory.SkipInstanceValue)
            {
                Core.Log.Warning("Skipping transport instance by Host value.");
                return(null);
            }
            var strPort = parameters["Port"];

            if (strPort == null || strPort == Factory.SkipInstanceValue)
            {
                Core.Log.Warning("Skipping transport instance by Port value.");
                return(null);
            }
            var port = strPort.ParseTo(0);

            var timeout            = parameters["Timeout"].ParseTo(20000);
            var serializerMimeType = parameters["SerializerMimeType"];
            var socketsPerClient   = parameters["SocketsPerClient"].ParseTo <byte>(1);
            var compressorEncoding = parameters["CompressorEncoding"];
            var serializer         = SerializerManager.GetByMimeType(serializerMimeType);

            if (compressorEncoding.IsNotNullOrEmpty())
            {
                var compressor = CompressorManager.GetByEncodingType(compressorEncoding);
                if (compressor != null)
                {
                    serializer.Compressor = compressor;
                }
            }
            var lclient = new DefaultTransportClient(host, port, socketsPerClient, serializer)
            {
                InvokeMethodTimeout = timeout
            };
            ITransportClient client = lclient;

            Core.Log.LibDebug("Creating a new DefaultTransportClient with parameters:");
            Core.Log.LibDebug("\tHost: {0}", host);
            Core.Log.LibDebug("\tPort: {0}", port);
            Core.Log.LibDebug("\tSocketsPerClient: {0}", socketsPerClient);
            if (serializerMimeType == null)
            {
                return(client);
            }
            Core.Log.LibDebug("\tSerializer: {0}", serializer);
            if (serializer?.Compressor != null)
            {
                Core.Log.LibDebug("\tCompressorEncoding: {0}", compressorEncoding);
            }
            return(client);
        }
Example #6
0
        private static ISerializer CreateSerializer(string serMimeType)
        {
            var idx     = serMimeType.IndexOf(':');
            var serMime = idx < 0 ? serMimeType : serMimeType.Substring(0, idx);
            var serComp = idx < 0 ? null : serMimeType.Substring(idx + 1);
            var ser     = SerializerManager.GetByMimeType(serMime);

            if (ser is null)
            {
                ThrowSerializerNotFound(serMime);
            }
            if (!string.IsNullOrWhiteSpace(serComp))
            {
                ser.Compressor = CompressorManager.GetByEncodingType(serComp);
            }
            return(ser);
        }
Example #7
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

            if (response.Content?.Headers?.ContentEncoding.IsNotNullOrEmpty() != true)
            {
                return(response);
            }
            var encoding   = response.Content.Headers.ContentEncoding.First();
            var compressor = CompressorManager.GetByEncodingType(encoding);

            if (compressor != null)
            {
                response.Content = await DecompressContentAsync(response.Content, compressor).ConfigureAwait(false);
            }
            return(response);
        }
Example #8
0
        public void Init(MQPairConfig config)
        {
            if (config == null)
            {
                return;
            }
            Config = config;

            Name             = Config.Name;
            SenderSerializer = SerializerManager.GetByMimeType(Config.RequestOptions?.SerializerMimeType);
            if (SenderSerializer != null && Config.RequestOptions?.CompressorEncodingType.IsNotNullOrEmpty() == true)
            {
                SenderSerializer.Compressor = CompressorManager.GetByEncodingType(Config.RequestOptions?.CompressorEncodingType);
            }
            ReceiverSerializer = SerializerManager.GetByMimeType(Config.ResponseOptions?.SerializerMimeType);
            if (ReceiverSerializer != null && Config.ResponseOptions?.CompressorEncodingType.IsNotNullOrEmpty() == true)
            {
                ReceiverSerializer.Compressor = CompressorManager.GetByEncodingType(Config.ResponseOptions?.CompressorEncodingType);
            }

            OnInit();
        }
Example #9
0
        protected override ITransportServer CreateTransport(KeyValueCollection parameters)
        {
            var strPort = parameters["Port"];

            if (strPort == null || strPort == Factory.SkipInstanceValue)
            {
                Core.Log.Warning("Skipping transport instance by Port value.");
                return(null);
            }
            var port = strPort.ParseTo(0);
            var serializerMimeType = parameters["SerializerMimeType"];
            var compressorEncoding = parameters["CompressorEncoding"];
            var serializer         = SerializerManager.GetByMimeType(serializerMimeType);

            if (compressorEncoding.IsNotNullOrEmpty())
            {
                var compressor = CompressorManager.GetByEncodingType(compressorEncoding);
                if (compressor != null)
                {
                    serializer.Compressor = compressor;
                }
            }
            var tServer = new DefaultTransportServer(port, serializer);

            Core.Log.LibDebug("Creating a new DefaultTransportServer with parameters:");
            Core.Log.LibDebug("\tPort: {0}", port);
            if (serializerMimeType != null)
            {
                Core.Log.LibDebug("\tSerializer: {0}", serializer);
                if (serializer?.Compressor != null)
                {
                    Core.Log.LibDebug("\tCompressorEncoding: {0}", compressorEncoding);
                }
            }
            return(tServer);
        }
Example #10
0
        public static MultiArray <byte> FromDeflate(this MultiArray <byte> value)
        {
            var deflateCompressor = CompressorManager.Get <DeflateCompressor>() ?? new DeflateCompressor();

            return(deflateCompressor.Decompress(value));
        }
Example #11
0
        public void StartListeners()
        {
            if (_tokenSource != null)
            {
                StopListeners();
            }

            Core.Log.InfoBasic("Configuring server mode for {0} with ResponseServer = {1}", Name, ResponseServer);
            if (ResponseServer)
            {
                ReceiverSerializer = SerializerManager.GetByMimeType(Config.ResponseOptions?.SerializerMimeType);
                if (ReceiverSerializer != null && Config.ResponseOptions?.CompressorEncodingType.IsNotNullOrEmpty() == true)
                {
                    ReceiverSerializer.Compressor = CompressorManager.GetByEncodingType(Config.ResponseOptions?.CompressorEncodingType);
                }

                SenderSerializer = null;

                Core.Log.InfoBasic("Adding queue client listener for {0}, Environment: {1}", Name, Core.EnvironmentName);
                _clientQueues = Config.ClientQueues?.FirstOf(
                    c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true && c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                    c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true,
                    c => c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                    c => c.EnvironmentName.IsNullOrWhitespace());

                if (_clientQueues?.RecvQueue != null)
                {
                    var queueListener = OnCreateQueueServerListener(_clientQueues.RecvQueue, ResponseServer);
                    queueListener.ResponseReceived += QueueListener_ResponseReceived;
                    QueueServerListeners.Add(queueListener);
                }
                else
                {
                    Core.Log.Warning("There aren't any Receiver Queue for the Client Listener. Check the ClientQueues element on the queue config file.");
                }
            }
            else
            {
                ReceiverSerializer = SerializerManager.GetByMimeType(Config.RequestOptions?.SerializerMimeType);
                if (ReceiverSerializer != null && Config.RequestOptions?.CompressorEncodingType.IsNotNullOrEmpty() == true)
                {
                    ReceiverSerializer.Compressor = CompressorManager.GetByEncodingType(Config.RequestOptions?.CompressorEncodingType);
                }

                SenderSerializer = SerializerManager.GetByMimeType(Config.ResponseOptions?.SerializerMimeType);
                if (SenderSerializer != null && Config.ResponseOptions?.CompressorEncodingType.IsNotNullOrEmpty() == true)
                {
                    SenderSerializer.Compressor = CompressorManager.GetByEncodingType(Config.ResponseOptions?.CompressorEncodingType);
                }


                Core.Log.InfoBasic("Adding queue server listeners for {0}, Environment: {1}", Name, Core.EnvironmentName);
                _serverQueues = Config.ServerQueues?.FirstOf(
                    c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true && c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                    c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true,
                    c => c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                    c => c.EnvironmentName.IsNullOrWhitespace());

                if (_serverQueues?.RecvQueues?.Any() == true)
                {
                    _serverQueues.RecvQueues.Each(queue =>
                    {
                        var queueListener              = OnCreateQueueServerListener(queue, ResponseServer);
                        queueListener.RequestReceived += QueueListener_RequestReceived;
                        QueueServerListeners.Add(queueListener);
                    });
                }
                else
                {
                    Core.Log.Warning("There aren't any Receiver Queue for the Server Listeners. Check the ServerQueues element on the queue config file.");
                }
            }

            if (QueueServerListeners.Count > 0)
            {
                Core.Log.InfoBasic("Starting queue server listeners for {0}", Name);
                _tokenSource = new CancellationTokenSource();
                _listenerTasks.Clear();

                foreach (var listener in QueueServerListeners)
                {
                    _listenerTasks.Add(InitListener(listener));
                }
            }
            else
            {
                Core.Log.Warning("There are not server listener to start.");
            }
        }
Example #12
0
        /// <summary>
        /// Gets the cache client connection pool
        /// </summary>
        /// <param name="name">Cache name</param>
        /// <returns>Cache client pool</returns>
        public async Task <CacheClientPoolAsync> GetCacheClientAsync(string name)
        {
            if (Caches?.Contains(name) != true)
            {
                return(null);
            }
            var cacheConfig = Caches[name];
            var cConfig     = cacheConfig.ClientOptionsList?.FirstOf(
                c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true && c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true,
                c => c.EnvironmentName.IsNullOrWhitespace());

            if (cConfig?.Pool == null)
            {
                return(null);
            }
            var         pingDelay        = cConfig.Pool.PingDelay.ParseTo(5000);
            var         pingDelayOnError = cConfig.Pool.PingDelayOnError.ParseTo(30000);
            var         readMode         = cConfig.Pool.ReadMode;
            var         writeMode        = cConfig.Pool.WriteMode;
            var         selectionOrder   = cConfig.Pool.SelectionOrder;
            var         forceNetworkItem = cConfig.Pool.ForceAtLeastOneNetworkItemEnabled;
            ISerializer serializer;

            if (cConfig.Pool.SerializerMimeType.IsNotNullOrEmpty())
            {
                serializer = SerializerManager.GetByMimeType(cConfig.Pool.SerializerMimeType);
                Ensure.ReferenceNotNull(serializer, $"The Serializer \"{cConfig.Pool.SerializerMimeType}\" couldn't be loaded.");
                if (cConfig.Pool.CompressorEncoding.IsNotNullOrEmpty())
                {
                    serializer.Compressor = CompressorManager.GetByEncodingType(cConfig.Pool.CompressorEncoding);
                }
            }
            else
            {
                serializer = SerializerManager.DefaultBinarySerializer;
            }
            var ccp = new CacheClientPoolAsync(name, pingDelay, pingDelayOnError, readMode, writeMode, selectionOrder)
            {
                Serializer = serializer,
                ForceAtLeastOneNetworkItemEnabled = forceNetworkItem
            };

            if (cConfig.Pool.Items?.Any() != true)
            {
                return(ccp);
            }

            var idx = 0;

            foreach (var pitem in cConfig.Pool.Items)
            {
                idx++;
                if (!pitem.Enabled)
                {
                    continue;
                }

                var objType = pitem.CreateInstance <object>();
                switch (objType)
                {
                case ITransportClient transport:
                {
                    var hostParam = pitem.Parameters?.FirstOrDefault(p => p.Key == "Host");
                    var portParam = pitem.Parameters?.FirstOrDefault(p => p.Key == "Port");

                    var proxy = await CacheClientProxy.GetClientAsync(transport).ConfigureAwait(false);

                    var cppName = Core.EnvironmentName + "." + Core.MachineName + "." + name + ".Storage(" + transport.GetType().Name + "-" + hostParam?.Value + "-" + portParam?.Value + ")." + idx;
                    ccp.Add(cppName, (IStorageAsync)proxy, pitem.Mode);
                    break;
                }

                case StorageBase sto:
                {
                    var cppName = Core.EnvironmentName + "." + Core.MachineName + "." + name + ".Storage(" + sto.Type + ")." + idx;
                    ccp.Add(cppName, sto);
                    break;
                }
                }
            }
            return(ccp);
        }
Example #13
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CompressorManager cm = new CompressorManager();

            cm.Start();
        }
Example #14
0
        /// <summary>
        /// Gets a Deflated byte array from the source byte array
        /// </summary>
        /// <param name="value">Source byte array</param>
        /// <returns>Deflated byte array</returns>
        public static SubArray <byte> ToDeflate(this SubArray <byte> value)
        {
            var deflateCompressor = CompressorManager.Get <DeflateCompressor>() ?? new DeflateCompressor();

            return(deflateCompressor.Compress(value));
        }