private void ClientOnOnMessageReceived(RpcClient rpcClient1, RPCMessage rpcMessage)
        {
            switch (rpcMessage)
            {
            case RPCResponseMessage responseMessage:
                if (!_messageResponsesHandlers.TryRemove(responseMessage.RequestMessageId, out var value))
                {
                    return;
                }
                value.Message = responseMessage;
                value.Event.Set();
                break;

            case RPCEventMessage eventMessage:
                _previousMessages.GetOrAdd(eventMessage.MessageId, mId =>
                {
                    OnEventReceived?.InvokeAsync(this, new EventDataEventArgs(eventMessage.ServiceName, eventMessage.EventName, eventMessage.EventArgs));
                    return(null);
                });
                break;

            case RPCPushMessage pushMessage:
                _previousMessages.GetOrAdd(pushMessage.MessageId, mId =>
                {
                    OnPushMessageReceived?.InvokeAsync(this, new EventArgs <RPCPushMessage>(pushMessage));
                    return(null);
                });
                break;

            case RPCError errorMessage:
                var respMsg = new RPCResponseMessage {
                    Exception = errorMessage.Exception
                };
                foreach (var mHandler in _messageResponsesHandlers.ToArray())
                {
                    mHandler.Value.Message = respMsg;
                    mHandler.Value.Event.Set();
                    _messageResponsesHandlers.TryRemove(mHandler.Key, out var _);
                }
                break;
            }
        }
Esempio n. 2
0
            internal static WItem CreateItem(string message)
            {
                var item = ItemPools.New();

                item._lastMessage = message;
                item._id          = Interlocked.Increment(ref _watcherCount);
                item._groupValue  = null;
                item._counter     = Counters.GetOrAdd(message, msg => new StatusCounter(CounterPreffix + msg));
                item.StartedTap(message);
                return(item);
            }
Esempio n. 3
0
        /// <summary>
        /// Run CSharp code and execute it using the IRuntimeCode interface
        /// </summary>
        /// <param name="code">CSharp code</param>
        /// <param name="value">Object value instance to inject to the IRuntimeCode interface</param>
        /// <returns>IRuntimeCode response</returns>
        public static object Run(string code, object value)
        {
            var instance = CachedCompilations.GetOrAdd(code, mCode =>
            {
                Core.Log.InfoBasic("Compiling code...");
                var syntax        = CSharpSyntaxTree.ParseText(code);
                var assemblyName  = Path.GetRandomFileName();
                var csCompilation = CSharpCompilation.Create(assemblyName,
                                                             syntaxTrees: new[] { syntax },
                                                             references: References,
                                                             options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                using (var compiledCode = new MemoryStream())
                    using (var compiledPdb = new MemoryStream())
                    {
                        var compilerResult = csCompilation.Emit(compiledCode, compiledPdb);

                        if (!compilerResult.Success)
                        {
                            Core.Log.Warning("Error when compiling the code.");
                            var sbErrors = new StringBuilder();
                            sbErrors.AppendLine("Compilation error:");
                            var failures = compilerResult.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
                            foreach (var diagnostic in failures)
                            {
                                sbErrors.AppendLine(diagnostic.Id + " (" + diagnostic.Severity + ") : " + diagnostic.GetMessage());
                            }
                            throw new Exception(sbErrors.ToString());
                        }

                        Core.Log.InfoBasic("Compilation Success.");
                        compiledCode.Position = 0;
                        compiledPdb.Position  = 0;

                        var assembly = Assembly.Load(compiledCode.ToArray(), compiledPdb.ToArray());


                        var runType = assembly.GetTypes().FirstOrDefault(type => type.GetInterface(typeof(IRuntimeCode).FullName) != null);
                        if (runType is null)
                        {
                            throw new Exception("An implementation of the IRuntimeCode interface can't be found.");
                        }

                        var runCode = (IRuntimeCode)Activator.CreateInstance(runType);
                        return(runCode);
                    }
            });

            return(instance.Execute(value));
        }
Esempio n. 4
0
 public virtual Guid GetGuid(string obj) => StringGuidHashCache.GetOrAdd(_instanceName + obj, key => GetGuid(Encoding.GetBytes(obj)));
Esempio n. 5
0
 public virtual byte[] GetBytes(string obj) => StringBytesHashCache.GetOrAdd(_instanceName + obj, key => GetBytes(Encoding.GetBytes(obj)));
Esempio n. 6
0
 public MultiArray <byte> GetBytes(string obj) => StringBytesHashCache.GetOrAdd(_instanceName + obj, key => GetBytes(Encoding.GetBytes(obj)));
Esempio n. 7
0
        public static void Run(int min, int max, int numberElements, int capacity)
        {
            var randNum       = new Random();
            var randomNumbers = Enumerable.Repeat(0, numberElements).Select((i, vTuple) => vTuple.randNum.Next(vTuple.min, vTuple.max), (randNum, min, max)).ToArray();
            var lru           = new LRUCollection <int, long>(capacity);
            var lfu           = new LFUCollection <int, long>(capacity);
            var lru2Q         = new LRU2QCollection <int, long>(capacity);
            var lru2QSimple   = new LRU2QSimpleCollection <int, long>(capacity);

            Core.Log.InfoBasic("Processing: {0} elements", numberElements);
            Core.Log.InfoBasic("Collections Capacity: {0} elements", capacity);
            Core.Log.InfoBasic("Random numbers from {0} to {1}", min, max);
            Core.Log.WriteEmptyLine();

            using (var w = Watch.Create("LFU Start", "LFU End"))
            {
                Parallel.ForEach(randomNumbers, item =>
                {
                    var value = lfu.GetOrAdd(item, item);
                    if (value != item)
                    {
                        Core.Log.Warning("Bad value: " + value + " " + item);
                    }
                });
                var ms = w.GlobalElapsedMilliseconds / numberElements;
                Core.Log.InfoDetail("LFU Hits: {0}", lfu.Hits);
                Core.Log.InfoDetail("LFU Deletes: {0}", lfu.Deletes);
                Core.Log.InfoDetail("LFU Inserts: {0}", lfu.Inserts);
                Core.Log.InfoDetail("LFU ms per item: {0}ms", ms);
                Core.Log.InfoDetail("LFU ns per item: {0}ns", ms * 1_000_000);
            }
            Thread.Sleep(1000);
            Core.Log.WriteEmptyLine();


            using (var w = Watch.Create("LRU Start", "LRU End"))
            {
                Parallel.ForEach(randomNumbers, item =>
                {
                    var value = lru.GetOrAdd(item, item);
                    if (value != item)
                    {
                        Core.Log.Warning("Bad value: " + value + " " + item);
                    }
                });
                var ms = w.GlobalElapsedMilliseconds / numberElements;
                Core.Log.InfoDetail("LRU Hits: {0}", lru.Hits);
                Core.Log.InfoDetail("LRU Deletes: {0}", lru.Deletes);
                Core.Log.InfoDetail("LRU Inserts: {0}", lru.Inserts);
                Core.Log.InfoDetail("LRU ms per item: {0}ms", ms);
                Core.Log.InfoDetail("LRU ns per item: {0}ns", ms * 1_000_000);
            }
            Thread.Sleep(1000);
            Core.Log.WriteEmptyLine();


            using (var w = Watch.Create("LRU2QSimple Start", "LRU2QSimple End"))
            {
                Parallel.ForEach(randomNumbers, item =>
                {
                    var value = lru2QSimple.GetOrAdd(item, item);
                    if (value != item)
                    {
                        Core.Log.Warning("Bad value: " + value + " " + item);
                    }
                });
                var ms = w.GlobalElapsedMilliseconds / numberElements;
                Core.Log.InfoDetail("LRU2QSimple Hits: {0}", lru2QSimple.Hits);
                Core.Log.InfoDetail("LRU2QSimple Deletes: {0}", lru2QSimple.Deletes);
                Core.Log.InfoDetail("LRU2QSimple Inserts: {0}", lru2QSimple.Inserts);
                Core.Log.InfoDetail("LRU2QSimple ms per item: {0}ms", ms);
                Core.Log.InfoDetail("LRU2QSimple ns per item: {0}ns", ms * 1_000_000);
            }
            Thread.Sleep(1000);
            Core.Log.WriteEmptyLine();


            using (var w = Watch.Create("LRU2Q Start", "LRU2Q End"))
            {
                Parallel.ForEach(randomNumbers, item =>
                {
                    var value = lru2Q.GetOrAdd(item, item);
                    if (value != item)
                    {
                        Core.Log.Warning("Bad value: " + value + " " + item);
                    }
                });
                var ms = w.GlobalElapsedMilliseconds / numberElements;
                Core.Log.InfoDetail("LRU2Q Hits: {0}", lru2Q.Hits);
                Core.Log.InfoDetail("LRU2Q Deletes: {0}", lru2Q.Deletes);
                Core.Log.InfoDetail("LRU2Q Inserts: {0}", lru2Q.Inserts);
                Core.Log.InfoDetail("LRU2Q ms per item: {0}ms", ms);
                Core.Log.InfoDetail("LRU2Q ns per item: {0}ns", ms * 1_000_000);
            }
            Thread.Sleep(1000);
            Core.Log.WriteEmptyLine();
        }