protected override ITransportServer CreateTransport(KeyValueCollection parameters) { var queueServerName = parameters["QueueServerName"]; var enableGetDescriptors = parameters["EnableGetDescriptors"].ParseTo(true); var serializerMimeType = parameters["SerializerMimeType"]; var compressorEncoding = parameters["CompressorEncoding"]; var serializer = SerializerManager.GetByMimeType(serializerMimeType); if (serializer != null && compressorEncoding.IsNotNullOrEmpty()) { var compressor = CompressorManager.GetByEncodingType(compressorEncoding); if (compressor != null) { serializer.Compressor = compressor; } } Core.Log.LibDebug("Creating a new MessagingTransportServer with the parameters:"); Core.Log.LibDebug("\tQueueServerName: {0}", queueServerName.IsNotNullOrWhitespace() ? queueServerName : "Using Default QueueServer from configuration file."); Core.Log.LibDebug("\tEnableGetDescriptors: {0}", enableGetDescriptors); if (serializerMimeType != null) { Core.Log.LibDebug("\tSerializer: {0}", serializer); if (serializer?.Compressor != null) { Core.Log.LibDebug("\tCompressorEncoding: {0}", compressorEncoding); } } var queueServer = queueServerName.IsNotNullOrWhitespace() ? Core.Services.GetQueueServer(queueServerName) : Core.Services.GetQueueServer(); return(new MessagingTransportServer(queueServer, serializer)); }
public void Init(MQPairConfig config, bool sendOnly) { if (config is null) { return; } Config = config; SendOnly = sendOnly; Name = Config.Name; Counters = new MQClientCounters(Name, Config.IgnoreClientCounters); 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(); }
protected override void OnHandler(ParameterHandlerInfo info) { Core.Log.Warning("Starting COMPRESSOR TEST"); Core.Log.InfoBasic("Generating Random Bytes"); var rnd = new Random(); var bytes = new byte[ushort.MaxValue * 2000]; for (var i = 0; i < bytes.Length; i++) { bytes[i] = (byte)rnd.Next(50, 60); } Core.Log.InfoBasic("Total Bytes = {0}", bytes.Length.ToReadableBytes()); Core.Log.InfoBasic("Creating GZip Compressor"); var gzipCompressor = CompressorManager.GetByEncodingType("gzip"); using (Watch.Create("GZIP COMPRESSOR")) { var gzipBytes = gzipCompressor.Compress(bytes); Core.Log.InfoBasic("Total GZIP Bytes = {0} - {1:0.000}%", gzipBytes.Count.ToReadableBytes(), ((double)gzipBytes.Count * 100) / bytes.Length); } Core.Log.InfoBasic("Creating Deflate Compressor"); var deflateCompressor = CompressorManager.GetByEncodingType("deflate"); using (Watch.Create("DEFLATE COMPRESSOR")) { var deflateBytes = deflateCompressor.Compress(bytes); Core.Log.InfoBasic("Total DEFLATE Bytes = {0} - {1:0.000}%", deflateBytes.Count.ToReadableBytes(), ((double)deflateBytes.Count * 100) / bytes.Length); } }
public object GetValue() { if (Data == null) { return(null); } if (SerializedObjects.TryGetValue(Data, out var value)) { return(value); } var type = string.IsNullOrWhiteSpace(DataType) ? typeof(object) : Core.GetType(DataType, true); if (string.IsNullOrWhiteSpace(SerializerMimeType)) { return(type == typeof(byte[]) ? Data : null); } var idx = SerializerMimeType.IndexOf(':'); var serMime = idx < 0 ? SerializerMimeType : SerializerMimeType.Substring(0, idx); var serComp = idx < 0 ? null : SerializerMimeType.Substring(idx + 1); var serializer = SerializerManager.GetByMimeType(serMime); if (serializer == null) { throw new FormatException($"The serializer with MimeType = {serMime} wasn't found."); } if (!string.IsNullOrWhiteSpace(serComp)) { serializer.Compressor = CompressorManager.GetByEncodingType(serComp); } value = serializer.Deserialize(Data, type); SerializedObjects.TryAdd(Data, value); return(value); }
protected override ITransportClient CreateTransport(KeyValueCollection parameters) { var queueClientName = parameters["QueueClientName"]; var serializerMimeType = parameters["SerializerMimeType"]; var compressorEncoding = parameters["CompressorEncoding"]; var serializer = SerializerManager.GetByMimeType(serializerMimeType); if (serializer != null && compressorEncoding.IsNotNullOrEmpty()) { var compressor = CompressorManager.GetByEncodingType(compressorEncoding); if (compressor != null) { serializer.Compressor = compressor; } } Core.Log.LibDebug("Creating a new MessagingTransportServer with the parameters:"); Core.Log.LibDebug("\tQueueClientName: {0}", queueClientName); if (serializerMimeType != null) { Core.Log.LibDebug("\tSerializer: {0}", serializer); if (serializer?.Compressor != null) { Core.Log.LibDebug("\tCompressorEncoding: {0}", compressorEncoding); } } Ensure.ArgumentNotNull(queueClientName, "The QueueClientName parameter in the MessagingTransportClientFactory can't be null."); var queueClient = Core.Services.GetQueueClient(queueClientName); return(new MessagingTransportClient(queueClient, serializer)); }
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(); }
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); }
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); }
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); }
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); }
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(); }
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); }
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."); } }
/// <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); }