コード例 #1
0
        public void PopAndPush_CallPushBeforePop_CorrectCounters()
        {
            ConcurentQueue<int> concurentQueue = new ConcurentQueue<int>(10000);

            ConcurrentBag<int> popResult = new ConcurrentBag<int>();
            ConcurrentBag<Exception> popResultEx = new ConcurrentBag<Exception>();
            ConcurrentBag<int> pushItems = new ConcurrentBag<int>();
            Parallel.For(0, 10, i =>
            {
                concurentQueue.Push(i);
            });

            Parallel.For(0, 10, i =>
            {
                pushItems.Add(i);
                try
                {
                    var temp = concurentQueue.Pop();
                    popResult.Add(temp);
                }
                catch (Exception Exp)
                {
                    popResultEx.Add(Exp);
                }
            });

            Assert.AreEqual(pushItems.Count, popResult.Count + popResultEx.Count + concurentQueue.Count);
        }
        public void when_sending_message_with_session_then_session_receiver_gets_both_messages_fast()
        {
            var sender = this.Settings.CreateTopicClient(this.Topic);
            var signal = new AutoResetEvent(false);
            var body1 = Guid.NewGuid().ToString();
            var body2 = Guid.NewGuid().ToString();
            var stopWatch = new Stopwatch();

            var receiver = new SessionSubscriptionReceiver(this.Settings, this.Topic, this.Subscription);

            sender.Send(new BrokeredMessage(body1) { SessionId = "foo" });
            sender.Send(new BrokeredMessage(body2) { SessionId = "bar" });

            var received = new ConcurrentBag<string>();

            receiver.Start(
                m =>
                {
                    received.Add(m.GetBody<string>());
                    signal.Set();
                    return MessageReleaseAction.CompleteMessage;
                });

            signal.WaitOne();
            stopWatch.Start();
            signal.WaitOne();
            stopWatch.Stop();

            receiver.Stop();

            Assert.Contains(body1, received);
            Assert.Contains(body2, received);
            Assert.InRange(stopWatch.Elapsed, TimeSpan.Zero, TimeSpan.FromSeconds(2));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: fmsaraujo/crank
        private static Task ConnectBatches(string url, int clients, int batchSize, int batchInterval, ConcurrentBag<Connection> connections)
        {
            int processed = Math.Min(clients, batchSize);

            var sw = Stopwatch.StartNew();
            Console.WriteLine("Remaining clients {0}", clients);
            return ConnectBatch(url, processed, connections).ContinueWith(t =>
            {
                sw.Stop();
                Console.WriteLine("Batch took {0}", sw.Elapsed);

                int remaining = clients - processed;

                if (remaining > 0)
                {
                    // Give this batch a few seconds to connect
                    Thread.Sleep(batchInterval);

                    return ConnectBatches(url, remaining, batchSize, batchInterval, connections);
                }

                var tcs = new TaskCompletionSource<object>();
                tcs.TrySetResult(null);
                return tcs.Task;
            })
            .Unwrap();
        }
コード例 #4
0
        public IList<Type> GetExceptions(Assembly assembly, IEnumerable<Type> exceptionsToIgnore)
        {
            Type typeOfException = typeof(Exception);
            ConcurrentBag<Type> types = new ConcurrentBag<Type>();

            Parallel.ForEach(
                assembly.GetTypes(),
                type =>
                {
                    if (exceptionsToIgnore != null && exceptionsToIgnore.Any())
                    {
                        if (exceptionsToIgnore.Contains(type))
                        {
                            return;
                        }
                    }

                    if (typeOfException.IsAssignableFrom(type))
                    {
                        types.Add(type);
                    }
                });

            return types.ToList();
        }
コード例 #5
0
        public void finish_successfully()
        {
            var handling = new AsyncHandling(ObjectMother.InvocationContext());
            var list = new ConcurrentBag<string>();

            var task1 = Task.Factory.StartNew(() => {
                Thread.Sleep(100);
                list.Add("A");
            });

            var task2 = Task.Factory.StartNew(() => {
                Thread.Sleep(100);
                list.Add("B");
            });

            var task3 = Task.Factory.StartNew(() => {
                Thread.Sleep(100);
                list.Add("C");
            });

            handling.Push(task1);
            handling.Push(task2);
            handling.Push(task3);

            handling.WaitForAll();

            list.OrderBy(x => x).ShouldHaveTheSameElementsAs("A", "B", "C");
        }
コード例 #6
0
        public async Task ItHasBeenFixed()
        {
            var activator = new BuiltinHandlerActivator();

            Using(activator);

            var receivedMessageIds = new ConcurrentBag<string>();

            activator.Handle<string>(async (_, context, message) =>
            {
                receivedMessageIds.Add(context.TransportMessage.Headers[Headers.MessageId]);
            });

            var bus = Configure.With(activator)
                .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "buggerino"))
                .Start();

            var customHeaders = new Dictionary<string, string>
            {
                {"custom-header", "woohoo"}
            };

            const string repeatedMessage = "hej med dig";

            await bus.SendLocal(repeatedMessage, customHeaders);
            await bus.SendLocal("hej igen med", customHeaders);
            await bus.SendLocal(repeatedMessage, customHeaders);

            await Task.Delay(TimeSpan.FromSeconds(1));

            Assert.That(receivedMessageIds.Distinct().Count(), Is.EqualTo(3), "Expected three unique message IDs - got: {0}", string.Join(", ", receivedMessageIds));
        }
コード例 #7
0
ファイル: TrueCraftGame.cs プロジェクト: Luigifan/TrueCraft
        public TrueCraftGame(MultiplayerClient client, IPEndPoint endPoint)
        {
            Window.Title = "TrueCraft";
            Content.RootDirectory = "Content";
            Graphics = new GraphicsDeviceManager(this);
            Graphics.SynchronizeWithVerticalRetrace = false;
            Graphics.IsFullScreen = UserSettings.Local.IsFullscreen;
            Graphics.PreferredBackBufferWidth = UserSettings.Local.WindowResolution.Width;
            Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height;
            Client = client;
            EndPoint = endPoint;
            NextPhysicsUpdate = DateTime.MinValue;
            ChunkMeshes = new List<Mesh>();
            IncomingChunks = new ConcurrentBag<Mesh>();
            PendingMainThreadActions = new ConcurrentBag<Action>();
            MouseCaptured = true;

            var keyboardComponent = new KeyboardComponent(this);
            KeyboardComponent = keyboardComponent;
            Components.Add(keyboardComponent);

            var mouseComponent = new MouseComponent(this);
            MouseComponent = mouseComponent;
            Components.Add(mouseComponent);
        }
コード例 #8
0
 internal XmlFileLoadInfo(string dir, string path, ConcurrentBag<string> errors, string psSnapinName)
 {
     fileDirectory = dir;
     filePath = path;
     this.errors = errors;
     this.psSnapinName = psSnapinName;
 }
コード例 #9
0
ファイル: TestPlan.cs プロジェクト: reuzel/CqlSharp-loadtest
 public TestPlan(string name, int iterationsCount, Action<int> action)
 {
     Name = name;
     IterationsCount = iterationsCount;
     Action = action;
     Results = new ConcurrentBag<TestResult>();
 }
コード例 #10
0
        public void SetUp()
        {
            Clock.Reset();

            disposables = new CompositeDisposable
            {
                Disposable.Create(Clock.Reset)
            };

            schedule = GetScheduleDelegate();

            commandsScheduled = new ConcurrentBag<IScheduledCommand>();
            commandsDelivered = new ConcurrentBag<IScheduledCommand>();

            var configuration = new Configuration()
                .TraceScheduledCommands() // trace to console
                .TraceScheduledCommands(
                    onScheduling: _ => { },
                    onScheduled: c => commandsScheduled.Add(c),
                    onDelivering: _ => { },
                    onDelivered: c => commandsDelivered.Add(c));

            Configure(configuration, d => disposables.Add(d));

            disposables.Add(ConfigurationContext.Establish(configuration));
        }
コード例 #11
0
ファイル: SeqGuidFixture.cs プロジェクト: NikGovorov/Taijutsu
        public void ShouldBeUnique()
        {
            var generatedIds = new ConcurrentBag<Guid>();

            var tasks = new List<Task>();

            var i = Count;
            while (i-- > 0)
            {
                tasks.Add(
                    Task.Factory.StartNew(
                        () =>
                        {
                            for (var j = 0; j < 100; j++)
                            {
                                var id = SeqGuid.NewGuid();
                                generatedIds.Add(id);
                            }
                        }));
            }

            Task.WaitAll(tasks.ToArray());

            Assert.That(new HashSet<Guid>(generatedIds).Count, Is.EqualTo(Count * 100));
        }
コード例 #12
0
 public DynamicSaveStrategy()
 {
     _decayBag = new ConcurrentBag<Item>();
     _itemThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
     _mobileThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
     _guildThreadWriters = new BlockingCollection<QueuedMemoryWriter>();
 }
コード例 #13
0
ファイル: ConcurrentBagTests.cs プロジェクト: noahfalk/corefx
        public static void TestBasicScenarios()
        {
            ConcurrentBag<int> cb = new ConcurrentBag<int>();
            Task[] tks = new Task[2];
            tks[0] = Task.Run(() =>
                {
                    cb.Add(4);
                    cb.Add(5);
                    cb.Add(6);
                });

            // Consume the items in the bag 
            tks[1] = Task.Run(() =>
                {
                    int item;
                    while (!cb.IsEmpty)
                    {
                        bool ret = cb.TryTake(out item);
                        Assert.True(ret);
                        // loose check
                        Assert.Contains(item, new[] { 4, 5, 6 });
                    }
                });

            Task.WaitAll(tks);
        }
コード例 #14
0
 /// <summary>
 /// ctor
 /// </summary>
 public QueryRunner(SqlQuery query, IEnumerable<WorkerDb> databases)
 {
     _query = query;
     _results = new ConcurrentBag<WorkerResult>();
     _databases = new List<WorkerDb>();
     _databases.AddRange(databases);
 }
コード例 #15
0
ファイル: World.cs プロジェクト: pboutell/amud-server
        public World()
        {
            worldTime = new DateTime();
            rooms = new List<Room>();
            rooms.Add(new Room("The Void", "You are standing in the middle of nothing."));
            mobs = new ConcurrentBag<NPC>();

            NPC test = new NPC("mob", "A slimy sticky stinky mob", new Stats(50, 100), this);
            rooms.First().addNPC(test);
            mobs.Add(test);
            test = new NPC("bob", "A slimy sticky stinky bob", new Stats(50, 100), this);
            rooms.First().addNPC(test);
            mobs.Add(test);
            test = new NPC("sob", "A slimy sticky stinky sob", new Stats(50, 100), this);
            rooms.First().addNPC(test);
            mobs.Add(test);
            test = new NPC("cob", "A slimy sticky stinky cob", new Stats(50, 100), this);
            rooms.First().addNPC(test);
            mobs.Add(test);

            Merchant merch = new Merchant();
            merch.name = "merchant";
            merch.description = "a merchant of souls";
            merch.world = this;
            merch.stats = new Stats(10000, 10000);
            Item i = new Item("health", "a potion of restore health", 1, "none", 1);
            merch.items.addToInventory(i);
            rooms.First().addNPC(merch);
            mobs.Add(merch);

            rooms.First().addItem(new Item("leggings", "a worn pair of leather leggings", 2, "legs", 2));
        }
コード例 #16
0
ファイル: TrueCraftGame.cs プロジェクト: Khazarak/TrueCraft
        public TrueCraftGame(MultiplayerClient client, IPEndPoint endPoint)
        {
            Window.Title = "TrueCraft";
            Content.RootDirectory = "Content";
            Graphics = new GraphicsDeviceManager(this);
            Graphics.SynchronizeWithVerticalRetrace = false;
            Graphics.IsFullScreen = UserSettings.Local.IsFullscreen;
            Graphics.PreferredBackBufferWidth = UserSettings.Local.WindowResolution.Width;
            Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height;
            Graphics.ApplyChanges();
            Window.ClientSizeChanged += Window_ClientSizeChanged;
            Client = client;
            EndPoint = endPoint;
            LastPhysicsUpdate = DateTime.MinValue;
            NextPhysicsUpdate = DateTime.MinValue;
            PendingMainThreadActions = new ConcurrentBag<Action>();
            MouseCaptured = true;
            Bobbing = 0;

            KeyboardComponent = new KeyboardHandler(this);
            Components.Add(KeyboardComponent);

            MouseComponent = new MouseHandler(this);
            Components.Add(MouseComponent);

            GamePadComponent = new GamePadHandler(this);
            Components.Add(GamePadComponent);
        }
コード例 #17
0
        public IEnumerable<INews> FirstNews()
        {
            var newsInstance = InterNewsBL.Instance;
            var newsList = new ConcurrentBag<INews>();
            try
            {
                var result = newsInstance.SelectTopNews();
                result.AsParallel().AsOrdered().ForAll(val =>
                {
                    newsList.Add(new News
                    {
                        NewsID = val.NewsID,
                        DisplayOrder = val.DisplayOrder,
                        Heading = val.Heading,
                        ImageUrl = val.ImageUrl,
                        ShortDesc = val.ShortDescription,
                        IsRss = val.IsRss,
                        //NewsDesc= val.NewsDescription,
                        DttmCreated = val.DttmCreated
                    });

                });

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                newsInstance.Dispose();
            }

            return newsList.OrderByDescending(v => v.DttmCreated);
        }
コード例 #18
0
ファイル: Reducer.cs プロジェクト: JordiCorbilla/MapReduce
 /// <summary>
 /// Constructor
 /// </summary>
 public Reducer()
 {
     _distinctWordList = new ConcurrentDictionary<string, int>();
     var concurrentBag = new ConcurrentBag<string>();
     _wordChunks = new BlockingCollection<string>(concurrentBag);
     Numwords = 0;
 }
コード例 #19
0
ファイル: PerformEades.cs プロジェクト: michielvh1995/OMI
        public override void ExecuteTests()
        {
            int verticesAmt = 20;

            var qualityVar = new ConcurrentBag<Tuple<double[], double[]>>();

            Parallel.For(1, 10, aw10 =>
            {
                double aW = (double)aw10 / 10;
                for (double rW = 0.1; rW < 1; rW += 0.1)
                {
                    for (double k = 0.1; k < 1; k += 0.1)
                    {
                        Vertex[] vertices = base.GenerateVertices(verticesAmt);

                        // The amount of UpdateForces iterations
                        for (int i = 0; i < 1000; i++)
                            vertices = UpdateForces(verticesAmt, vertices, aW, rW, k);

                        // And put the quality of the graph into a Tuple along with its a and r weights
                        var outTuple = new Tuple<double[], double[]>(new[] { aW, rW, k }, QualityTest.TestAll(vertices));

                        qualityVar.Add(outTuple);

                        // Store the Graph in a file with the parameters
                        Save.SaveGraph(new[] { aW, rW, k }, vertices);
                    }
                }
            });

            // Save the string array into a file
            Save.SaveStrings(GetQualitiesStrings(qualityVar));
        }
コード例 #20
0
        public MainController()
        {
            _model = new MainViewModel();

            _nodes = new Dictionary<long, Node>();
            _ways = new ConcurrentBag<Way>();
            _watch = new Stopwatch();

            timerRefresh = new Timer(500);
            timerRefresh.Elapsed += TimerRefreshElapsed;
            timerRefresh.Start();

            _xmlOSMFileLoader = new XmlOSMFileLoader(_ways);
            _dbLoader = new DBLoader(_ways,Model.QueryString);

            _phase = MainControllerStates.None;

            //load current Query String
            var v = ConfigurationManager.ConnectionStrings["CS"];

            if (v != null)
            {
                Model.QueryString = v.ConnectionString;
            }
        }
コード例 #21
0
 public RedisSubscriptionBroker(ConnectionMultiplexer connection, string prefix)
 {
     this.connection = connection;
     this.subscriptionChannel = string.Format("{0}.subscriptions.broker", prefix);
     this.handlers = new ConcurrentBag<Action<SubscriptionChange, Subscription>>();
     this.initializationTask = new Lazy<Task>(() => this.Initialize());
 }
コード例 #22
0
 public GuildMemberListPacket(int Amount)
     : base((ushort)(20 + Amount * 48), PacketType.GuildMemberListPacket)
 {
     MemberList = new ConcurrentBag<GuildMemberListPacket.GuildMemberInfo>();
     WriteInt32(Amount, 12);
     this.Amount = Amount;
 }
コード例 #23
0
        public void Instance_ThreadSafe()
        {

            using (var gate = new Barrier(5))
            {
                var result = new ConcurrentBag<AnyConstructorFinder>();

                Action test = () =>
                {
                    gate.SignalAndWait(20);

                    var instance = AnyConstructorFinder.Instance;

                    Thread.MemoryBarrier();

                    result.Add(instance);
                };

                var cycleState = Parallel.For(0, 200,
                    new ParallelOptions { MaxDegreeOfParallelism = 15 },
                    x => { test(); })
                    ;

                while (!cycleState.IsCompleted) 
                {
                    Thread.Sleep(100);
                }

                Assert.IsTrue(result.All(x => x != null));
                Assert.IsTrue(result.Distinct().Count() == 1);
            }
        }
コード例 #24
0
ファイル: AnalyzeCSharp.cs プロジェクト: martinvobr/Wyam
        public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
        {
            // Get syntax trees (supply path so that XML doc includes can be resolved)
            ConcurrentBag<SyntaxTree> syntaxTrees = new ConcurrentBag<SyntaxTree>();
            Parallel.ForEach(inputs, input =>
            {
                using (Stream stream = input.GetStream())
                {
                    SourceText sourceText = SourceText.From(stream);
                    syntaxTrees.Add(CSharpSyntaxTree.ParseText(sourceText, 
                        path: input.String(Keys.SourceFilePath, string.Empty)));
                }
            });

            // Create the compilation (have to supply an XmlReferenceResolver to handle include XML doc comments)
            MetadataReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            CSharpCompilation compilation = CSharpCompilation
                .Create("CodeAnalysisModule", syntaxTrees)
                .WithReferences(mscorlib)
                .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
                    xmlReferenceResolver: new XmlFileResolver(context.InputFolder)));

            // Get and return the document tree
            AnalyzeSymbolVisitor visitor = new AnalyzeSymbolVisitor(context, _symbolPredicate,
                _writePath ?? (x => DefaultWritePath(x, _writePathPrefix)), _cssClasses, _docsForImplicitSymbols);
            visitor.Visit(compilation.Assembly.GlobalNamespace);
            return visitor.Finish();
        }
コード例 #25
0
        public virtual async Task<CodeAction> GetFixAsync(
            ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
            FixAllContext fixAllContext)
        {
            if (documentsAndDiagnosticsToFixMap != null && documentsAndDiagnosticsToFixMap.Any())
            {
                FixAllLogger.LogDiagnosticsStats(documentsAndDiagnosticsToFixMap);

                var fixesBag = new ConcurrentBag<CodeAction>();

                using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Fixes, fixAllContext.CancellationToken))
                {
                    fixAllContext.CancellationToken.ThrowIfCancellationRequested();

                    var documents = documentsAndDiagnosticsToFixMap.Keys.ToImmutableArray();
                    var options = new ParallelOptions() { CancellationToken = fixAllContext.CancellationToken };
                    Parallel.ForEach(documents, options, document =>
                    {
                        fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                        AddDocumentFixesAsync(document, documentsAndDiagnosticsToFixMap[document], fixesBag.Add, fixAllContext).Wait(fixAllContext.CancellationToken);
                    });
                }

                if (fixesBag.Any())
                {
                    using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Merge, fixAllContext.CancellationToken))
                    {
                        FixAllLogger.LogFixesToMergeStats(fixesBag);
                        return await TryGetMergedFixAsync(fixesBag, fixAllContext).ConfigureAwait(false);
                    }
                }
            }

            return null;
        }
コード例 #26
0
ファイル: RabbitConnection.cs プロジェクト: liemqv/EventFlow
 public RabbitConnection(ILog log, int maxModels, IConnection connection)
 {
     _connection = connection;
     _log = log;
     _asyncLock = new AsyncLock(maxModels);
     _models = new ConcurrentBag<IModel>(Enumerable.Range(0, maxModels).Select(_ => connection.CreateModel()));
 }
コード例 #27
0
ファイル: BufferPool.cs プロジェクト: osjimenez/orleans
        /// <summary>
        /// Creates a buffer pool.
        /// </summary>
        /// <param name="bufferSize">The size, in bytes, of each buffer.</param>
        /// <param name="maxBuffers">The maximum number of buffers to keep around, unused; by default, the number of unused buffers is unbounded.</param>
        /// <param name="preallocationSize">Initial number of buffers to allocate.</param>
        /// <param name="name">Name of the buffer pool.</param>
        private BufferPool(int bufferSize, int maxBuffers, int preallocationSize, string name)
        {
            Name = name;
            byteBufferSize = bufferSize;
            maxBuffersCount = maxBuffers;
            limitBuffersCount = maxBuffers > 0;
            buffers = new ConcurrentBag<byte[]>();

            var globalPoolSizeStat = IntValueStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_BUFFERS_INPOOL,
                                                                    () => Count);
            allocatedBufferCounter = CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_ALLOCATED_BUFFERS);
            checkedOutBufferCounter = CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_CHECKED_OUT_BUFFERS);
            checkedInBufferCounter = CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_CHECKED_IN_BUFFERS);
            droppedBufferCounter = CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_DROPPED_BUFFERS);
            droppedTooLargeBufferCounter = CounterStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_DROPPED_TOO_LARGE_BUFFERS);

            // Those 2 counters should be equal. If not, it means we don't release all buffers.
            IntValueStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_INUSE_CHECKED_OUT_NOT_CHECKED_IN_BUFFERS,
                () => checkedOutBufferCounter.GetCurrentValue()
                      - checkedInBufferCounter.GetCurrentValue()
                      - droppedBufferCounter.GetCurrentValue());

            IntValueStatistic.FindOrCreate(StatisticNames.SERIALIZATION_BUFFERPOOL_INUSE_ALLOCATED_NOT_INPOOL_BUFFERS,
                () => allocatedBufferCounter.GetCurrentValue()
                      - globalPoolSizeStat.GetCurrentValue()
                      - droppedBufferCounter.GetCurrentValue());

            if (preallocationSize <= 0) return;

            var dummy = GetMultiBuffer(preallocationSize * Size);
            Release(dummy);
        }
コード例 #28
0
        protected override IEnumerable<Achievement> GetUnlockedAchievements(StatisAnalysisSession statisAnalysisSession, IEnumerable<Achievement> availableAchievements)
        {
            var unlockedAchievements = new ConcurrentBag<Achievement>();
            var tasks = new Task[availableAchievements.Count()];
            var i = 0;

            foreach (var uncompletedAchievement in availableAchievements)
            {
                var a = uncompletedAchievement;

                tasks[i++] = Task.Factory.StartNew(() =>
                {
                    /*   Technically we create a lot of objects all the time.
                     *   It's possible that these objects could have a lifespan longer than just a session.
                     *   However maintaining state is always a PITA */
                    var achievement = (StaticAnalysisAchievementBase)Activator.CreateInstance(a.AchievementType);

                    if (achievement.IsAchievementUnlocked(statisAnalysisSession))
                    {
                        a.CodeOrigin = achievement.AchievementCodeOrigin;
                        a.IsCompleted = true;
                        unlockedAchievements.Add(a);
                    }
                });
            }

            Task.WaitAll(tasks);

            return unlockedAchievements;
        }
コード例 #29
0
ファイル: Program.cs プロジェクト: jbijoux/Exam70_483
        static void Main(string[] args)
        {
            ConcurrentBag<int> bag = new ConcurrentBag<int>();
            bag.Add(42);
            bag.Add(21);

            int result;
            if (bag.TryTake(out result))
            {
                Console.WriteLine(result);
            }

            if (bag.TryPeek(out result))
            {
                Console.WriteLine("There is a next item: {0}", result);
            }

            if (bag.TryTake(out result))
            {
                Console.WriteLine(result);
            }

            Console.Write("Press a key to exit");
            Console.ReadKey();
        }
コード例 #30
0
        private static IEnumerable<string> TryDeduceMatchingCandidateSnapshot(
            ImageWrapper transformedOriginalSnapshot,
            IEnumerable<string> candidateSnapshots,
            Func<ImageWrapper, Maybe<ImageWrapper>> candidateTranform
            )
        {
            var candidateResultList = new ConcurrentBag<Tuple<string, int>>();
            using (var originaImageAsLockbit = new LockBitImage(transformedOriginalSnapshot.Image))
            {
                Parallel.ForEach(candidateSnapshots, candidateSnapshot =>
                {
                    var candidateComparisonResult = from loadedCandidateSnapshot in TryLoadImage(candidateSnapshot)
                                                    from transformedCandidate in candidateTranform.Invoke(loadedCandidateSnapshot)
                                                    let candidateLockbitImage = new LockBitImage(transformedCandidate.Image)
                                                    let similarityIndex = SimilarityCalculator.CalculateSimilarityIndex(
                                                        originaImageAsLockbit,
                                                        candidateLockbitImage
                                                    )
                                                    select CommonFunctions.ExecThenDispose(
                                                        () => Tuple.Create<string, int>(candidateSnapshot, similarityIndex),
                                                        loadedCandidateSnapshot,
                                                        transformedCandidate,
                                                        candidateLockbitImage
                                                    );
                    candidateComparisonResult.Apply(i => candidateResultList.Add(i));
                });
            }

            return from candidateTuple in candidateResultList
                   where candidateTuple.Item2 >= 69
                   select candidateTuple.Item1;
        }
コード例 #31
0
        public void GetFilesToPatch(ConcurrentBag <FileToPatch> filesToPatch, IGraphicsInfo gInfo)
        {
            if (!(gInfo is PkmdlConstants pkmdlInfo))
            {
                return;
            }

            var spriteFiles = _overrideSpriteProvider.GetAllSpriteFiles(pkmdlInfo.Type);

            if (!spriteFiles.Any(i => i.IsOverride))
            {
                return;
            }

            // temporary link files
            string texLink = Path.GetTempFileName();
            string atxLink = Path.GetTempFileName();
            string dtxLink = Path.GetTempFileName();
            string pacLink = Path.GetTempFileName();

            // link files unpacked previously
            string texUnpacked = Path.Combine(_graphicsProviderFolder, pkmdlInfo.TEXLinkFolder);
            string atxUnpacked = Path.Combine(_graphicsProviderFolder, pkmdlInfo.ATXLinkFolder);
            string dtxUnpacked = Path.Combine(_graphicsProviderFolder, pkmdlInfo.DTXLinkFolder);
            string pacUnpacked = Path.Combine(_graphicsProviderFolder, pkmdlInfo.PACLinkFolder);

            var spriteFileDict = spriteFiles.ToDictionary(i => i.Id);

            var texLinkFiles = new string[200];
            var atxLinkFiles = new string[200];
            var dtxLinkFiles = new string[200];
            var pacLinkFiles = new string[200];

            Parallel.For(0, 200, i =>
            {
                string fileName = i.ToString().PadLeft(4, '0');
                var spriteFile  = spriteFileDict[i];
                if (spriteFile.IsOverride)
                {
                    using (var combinedImage = Image.Load <Rgba32>(spriteFile.File))
                    {
                        // populate palette ------------------------------------------------------------------------------------------------------

                        var palette = new List <Rgba32> {
                            Color.Transparent
                        };

                        // TEX ------------------------------------------------------------------------------------------------------

                        string texSource = Path.Combine(texUnpacked, fileName);
                        string texTemp   = Path.GetTempFileName();
                        File.Copy(texSource, texTemp, true);
                        BTX0 btx0  = new BTX0(texSource);
                        int height = btx0.Texture.PixelMap.Length / _pokemonSpriteWidth;
                        using (var texImg = combinedImage.Clone(g =>
                        {
                            g.Crop(new Rectangle(0, 0, _pokemonSpriteWidth, height));
                        }))
                        {
                            btx0.Texture.PixelMap = ImageUtil.FromImage(texImg, palette, PointUtil.GetIndex);
                        }

                        // ATX ------------------------------------------------------------------------------------------------------

                        var atxDecompressedLen = new FileInfo(Path.Combine(atxUnpacked, fileName)).Length * 2;
                        using (var atxImg = combinedImage.Clone(g =>
                        {
                            g.Crop(new Rectangle(_pokemonSpriteWidth, 0, _pokemonSpriteWidth, (int)(atxDecompressedLen / _pokemonSpriteWidth)));
                        }))
                        {
                            string atxTemp = Path.GetTempFileName();
                            File.WriteAllBytes(atxTemp, RawChar.Compress(ImageUtil.FromImage(atxImg, palette, PointUtil.GetIndex)));
                            atxLinkFiles[i] = atxTemp;
                        }


                        // DTX ------------------------------------------------------------------------------------------------------

                        var dtxDecompressedLen = new FileInfo(Path.Combine(atxUnpacked, fileName)).Length * 2;
                        using (var dtxImg = combinedImage.Clone(g =>
                        {
                            g.Crop(new Rectangle(_pokemonSpriteWidth * 2, 0, _pokemonSpriteWidth, (int)(dtxDecompressedLen / _pokemonSpriteWidth)));
                        }))
                        {
                            string dtxTemp = Path.GetTempFileName();
                            File.WriteAllBytes(dtxTemp, RawChar.Compress(ImageUtil.FromImage(dtxImg, palette, PointUtil.GetIndex)));
                            dtxLinkFiles[i] = dtxTemp;
                        }


                        // PAC ------------------------------------------------------------------------------------------------------

                        string pacUnpackedFolder = Path.Combine(pacUnpacked, fileName + "-Unpacked");
                        var pacDecompressedLen   = new FileInfo(Path.Combine(pacUnpackedFolder, "0003")).Length * 2;
                        using (var pacImg = combinedImage.Clone(g =>
                        {
                            g.Crop(new Rectangle(_pokemonSpriteWidth * 3, 0, _pokemonSpriteWidth, (int)(pacDecompressedLen / _pokemonSpriteWidth)));
                        }))
                        {
                            string pacCharTemp = Path.GetTempFileName();
                            File.WriteAllBytes(pacCharTemp, RawChar.Compress(ImageUtil.FromImage(pacImg, palette, PointUtil.GetIndex)));
                            string pacTemp    = Path.GetTempFileName();
                            string[] pacFiles = new string[]
                            {
                                Path.Combine(pacUnpackedFolder, "0000"),
                                Path.Combine(pacUnpackedFolder, "0001"),
                                Path.Combine(pacUnpackedFolder, "0002"),
                                pacCharTemp
                            };
                            PAC.Pack(pacFiles, new[] { 0, 2, 5, 6 }, pacTemp, 1);
                            File.Delete(pacCharTemp);
                            pacLinkFiles[i] = pacTemp;
                        }


                        // TEX Palette -------------------------------------------------------------------------------------------
                        if (palette.Count > 16)
                        {
                            throw new System.Exception($"More than 16 colors in image when building tex file in {nameof(PkmdlPatchBuilder)}. This should have been filtered out by palette simplifier");
                        }

                        var resizedPalette = new Rgba32[16];
                        for (int paletteIndex = 0; paletteIndex < palette.Count; paletteIndex++)
                        {
                            resizedPalette[paletteIndex] = palette[paletteIndex];
                        }
                        resizedPalette[0] = Color.FromRgb(120, 120, 120);

                        var convertedPalette  = RawPalette.From32bitColors(resizedPalette);
                        btx0.Texture.Palette1 = convertedPalette;
                        btx0.Texture.Palette2 = convertedPalette;
                        btx0.WriteTo(texTemp);
                        texLinkFiles[i] = texTemp;
                    }
                }
                else
                {
                    texLinkFiles[i] = Path.Combine(texUnpacked, fileName);
                    atxLinkFiles[i] = Path.Combine(atxUnpacked, fileName);
                    dtxLinkFiles[i] = Path.Combine(dtxUnpacked, fileName);
                    pacLinkFiles[i] = Path.Combine(pacUnpacked, fileName);
                }
            });

            LINK.Pack(texLinkFiles, texLink);
            LINK.Pack(atxLinkFiles, atxLink);
            LINK.Pack(dtxLinkFiles, dtxLink);
            LINK.Pack(pacLinkFiles, pacLink);

            filesToPatch.Add(new FileToPatch(pkmdlInfo.TEXLink, texLink, FilePatchOptions.DeleteSourceWhenDone | FilePatchOptions.VariableLength));
            filesToPatch.Add(new FileToPatch(pkmdlInfo.ATXLink, atxLink, FilePatchOptions.DeleteSourceWhenDone | FilePatchOptions.VariableLength));
            filesToPatch.Add(new FileToPatch(pkmdlInfo.DTXLink, dtxLink, FilePatchOptions.DeleteSourceWhenDone | FilePatchOptions.VariableLength));
            filesToPatch.Add(new FileToPatch(pkmdlInfo.PACLink, pacLink, FilePatchOptions.DeleteSourceWhenDone | FilePatchOptions.VariableLength));
        }
 public InMemoryCaseWorkerTaskCommandRepository(ConcurrentBag <CaseWorkerTaskAggregate> caseWorkerTaskLst)
 {
     _caseWorkerTaskLst = caseWorkerTaskLst;
 }
コード例 #33
0
ファイル: ScriptValidator.cs プロジェクト: xia7410/BitSharp
 protected override void SubStart()
 {
     this.validationExceptions = new ConcurrentBag <Exception>();
 }
コード例 #34
0
ファイル: Concurrency.cs プロジェクト: zmjack/NStandard
        /// <summary>
        /// Use mutil-thread to simulate concurrent scenarios.
        /// </summary>
        /// <typeparam name="TRet"></typeparam>
        /// <param name="task"></param>
        /// <param name="level"></param>
        /// <param name="threadCount">If the value is 0, <see cref="Environment.ProcessorCount"/> will be used.</param>
        /// <returns></returns>
        public static TestReport <TRet> Run <TRet>(Func <TestId, TRet> task, int level, int threadCount = 0)
        {
            if (level < 1)
            {
                throw new ArgumentException($"The `{nameof(level)}` must be greater than 0.", nameof(level));
            }
            if (threadCount < 0)
            {
                throw new ArgumentException($"The `{nameof(threadCount)}` must be non-negative.", nameof(threadCount));
            }
            if (threadCount == 0)
            {
                threadCount = Environment.ProcessorCount;
            }

            var div = level / threadCount;
            var mod = level % threadCount;

            threadCount = Math.Min(level, threadCount);

#if NET35
            var results = new List <TestResult <TRet> >();
#else
            var results = new ConcurrentBag <TestResult <TRet> >();
#endif
            var threads = new Thread[threadCount];
            foreach (var threadNumber in new int[threadCount].Let(i => i))
            {
                threads[threadNumber] = new Thread(() =>
                {
                    var s_count = threadNumber < mod ? div + 1 : div;
                    for (int invokeNumber = 0; invokeNumber < s_count; invokeNumber++)
                    {
                        var threadId  = Thread.CurrentThread.ManagedThreadId;
                        var stopwatch = new Stopwatch();

                        stopwatch.Start();
                        try
                        {
                            var taskRet = task(new TestId(threadId, invokeNumber));
                            stopwatch.Stop();
                            var id     = new TestId(threadId, invokeNumber);
                            var result = new TestResult <TRet>
                            {
                                ThreadId     = threadId,
                                InvokeNumber = invokeNumber,
                                Success      = true,

                                Return    = taskRet,
                                Elapsed   = stopwatch.Elapsed,
                                Exception = null,
                            };
#if NET35
                            lock (results)
                            {
                                results.Add(result);
                            }
#else
                            results.Add(result);
#endif
                        }
                        catch (Exception ex)
                        {
                            stopwatch.Stop();
                            var id     = new TestId(threadId, invokeNumber);
                            var result = new TestResult <TRet>
                            {
                                ThreadId     = threadId,
                                InvokeNumber = invokeNumber,
                                Success      = false,

                                Return    = default,
コード例 #35
0
 public SandboxedArray(PooledArray <T> array, ConcurrentBag <IDisposable> pooledArrays)
 {
     this.array        = array;
     this.pooledArrays = pooledArrays;
     pooledArrays.Add(array);
 }
コード例 #36
0
        public void LoadCharacters(bool blnRefreshFavorites = true, bool blnRefreshRecents = true, bool blnRefreshWatch = true, bool blnRefreshPlugins = true)
        {
            ReadOnlyObservableCollection <string> lstFavorites = new ReadOnlyObservableCollection <string>(GlobalOptions.FavoritedCharacters);
            bool     blnAddFavoriteNode = false;
            TreeNode objFavoriteNode    = null;

            TreeNode[] lstFavoritesNodes = null;
            if (blnRefreshFavorites)
            {
                objFavoriteNode = treCharacterList.FindNode("Favorite", false);
                if (objFavoriteNode == null)
                {
                    objFavoriteNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_FavoriteCharacters"))
                    {
                        Tag = "Favorite"
                    };
                    blnAddFavoriteNode = true;
                }

                lstFavoritesNodes = new TreeNode[lstFavorites.Count];
            }

            List <string> lstRecents = new List <string>(GlobalOptions.MostRecentlyUsedCharacters);

            Dictionary <string, string> dicWatch = new Dictionary <string, string>();
            int intWatchFolderCount = 0;

            if (!string.IsNullOrEmpty(GlobalOptions.CharacterRosterPath) && Directory.Exists(GlobalOptions.CharacterRosterPath))
            {
                intWatchFolderCount++;
                foreach (string strFile in Directory.GetFiles(GlobalOptions.CharacterRosterPath, "*.chum5", SearchOption.AllDirectories))
                {
                    // Make sure we're not loading a character that was already loaded by the MRU list.
                    if (lstFavorites.Contains(strFile) ||
                        lstRecents.Contains(strFile))
                    {
                        continue;
                    }
                    FileInfo objInfo = new FileInfo(strFile);
                    if (objInfo.Directory == null || objInfo.Directory.FullName == GlobalOptions.CharacterRosterPath)
                    {
                        dicWatch.Add(strFile, "Watch");
                        continue;
                    }

                    string strNewParent = objInfo.Directory.FullName.Replace(GlobalOptions.CharacterRosterPath + "\\", string.Empty);
                    dicWatch.Add(strFile, strNewParent);
                }

                intWatchFolderCount++;
            }

            bool     blnAddWatchNode = false;
            TreeNode objWatchNode    = null;

            TreeNode[] lstWatchNodes = null;
            if (blnRefreshWatch)
            {
                objWatchNode = treCharacterList.FindNode("Watch", false);
                objWatchNode?.Remove();
                blnAddWatchNode = dicWatch.Count > 0;

                if (blnAddWatchNode)
                {
                    objWatchNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_WatchFolder"))
                    {
                        Tag = "Watch"
                    };
                }

                lstWatchNodes = new TreeNode[intWatchFolderCount];
            }

            bool     blnAddRecentNode = false;
            TreeNode objRecentNode    = null;

            TreeNode[] lstRecentsNodes = null;
            if (blnRefreshRecents)
            {
                // Add any characters that are open to the displayed list so we can have more than 10 characters listed
                foreach (CharacterShared objCharacterForm in Program.MainForm.OpenCharacterForms)
                {
                    string strFile = objCharacterForm.CharacterObject.FileName;
                    // Make sure we're not loading a character that was already loaded by the MRU list.
                    if (lstFavorites.Contains(strFile) ||
                        lstRecents.Contains(strFile) ||
                        dicWatch.ContainsValue(strFile))
                    {
                        continue;
                    }

                    lstRecents.Add(strFile);
                }

                foreach (string strFavorite in lstFavorites)
                {
                    lstRecents.Remove(strFavorite);
                }

                objRecentNode = treCharacterList.FindNode("Recent", false);
                if (objRecentNode == null && lstRecents.Count > 0)
                {
                    objRecentNode = new TreeNode(LanguageManager.GetString("Treenode_Roster_RecentCharacters"))
                    {
                        Tag = "Recent"
                    };
                    blnAddRecentNode = true;
                }

                lstRecentsNodes = new TreeNode[lstRecents.Count];
            }
            Parallel.Invoke(
                () => {
                if (objFavoriteNode != null && lstFavoritesNodes != null)
                {
                    object lstFavoritesNodesLock = new object();

                    Parallel.For(0, lstFavorites.Count, i =>
                    {
                        string strFile   = lstFavorites[i];
                        TreeNode objNode = CacheCharacter(strFile);
                        lock (lstFavoritesNodesLock)
                            lstFavoritesNodes[i] = objNode;
                    });

                    if (blnAddFavoriteNode)
                    {
                        foreach (TreeNode objNode in lstFavoritesNodes)
                        {
                            if (objNode != null)
                            {
                                objFavoriteNode.Nodes.Add(objNode);
                            }
                        }
                    }
                }
            },
                () => {
                if (objRecentNode != null && lstRecentsNodes != null)
                {
                    object lstRecentsNodesLock = new object();

                    Parallel.For(0, lstRecents.Count, i =>
                    {
                        string strFile   = lstRecents[i];
                        TreeNode objNode = CacheCharacter(strFile);
                        lock (lstRecentsNodesLock)
                            lstRecentsNodes[i] = objNode;
                    });

                    if (blnAddRecentNode)
                    {
                        foreach (TreeNode objNode in lstRecentsNodes)
                        {
                            if (objNode != null)
                            {
                                objRecentNode.Nodes.Add(objNode);
                            }
                        }
                    }
                }
            },
                () =>
            {
                if (objWatchNode != null && lstWatchNodes != null)
                {
                    ConcurrentBag <KeyValuePair <TreeNode, string> > bagNodes = new ConcurrentBag <KeyValuePair <TreeNode, string> >();
                    Parallel.ForEach(dicWatch, i => bagNodes.Add(new KeyValuePair <TreeNode, string>(CacheCharacter(i.Key), i.Value)));
                    if (blnAddWatchNode)
                    {
                        foreach (string s in dicWatch.Values.Distinct())
                        {
                            if (s == "Watch")
                            {
                                continue;
                            }
                            objWatchNode.Nodes.Add(new TreeNode(s)
                            {
                                Tag = s
                            });
                        }
                        foreach (KeyValuePair <TreeNode, string> kvtNode in bagNodes)
                        {
                            if (kvtNode.Value == "Watch")
                            {
                                objWatchNode.Nodes.Add(kvtNode.Key);
                            }
                            else
                            {
                                foreach (TreeNode objNode in objWatchNode.Nodes)
                                {
                                    if (objNode.Tag.ToString() == kvtNode.Value)
                                    {
                                        objNode.Nodes.Add(kvtNode.Key);
                                    }
                                }
                            }
                        }
                    }
                }
            },
                async() =>
            {
                foreach (IPlugin plugin in Program.PluginLoader.MyActivePlugins)
                {
                    List <TreeNode> lstNodes = await Task.Run(() =>
                    {
                        Log.Info("Starting new Task to get CharacterRosterTreeNodes for plugin:" + plugin);
                        var task = plugin.GetCharacterRosterTreeNode(this, blnRefreshPlugins);
                        if (task.Result != null)
                        {
                            return(task.Result.OrderBy(a => a.Text).ToList());
                        }
                        return(new List <TreeNode>());
                    });
                    await Task.Run(() =>
                    {
                        foreach (TreeNode node in lstNodes)
                        {
                            TreeNode objExistingNode = treCharacterList.Nodes.Cast <TreeNode>().FirstOrDefault(x => x.Text == node.Text && x.Tag == node.Tag);
                            Program.MainForm.DoThreadSafe(() =>
                            {
                                try
                                {
                                    if (objExistingNode != null)
                                    {
                                        treCharacterList.Nodes.Remove(objExistingNode);
                                    }

                                    if (node.Nodes.Count > 0 || !string.IsNullOrEmpty(node.ToolTipText) ||
                                        node.Tag != null)
                                    {
                                        if (treCharacterList.IsDisposed)
                                        {
                                            return;
                                        }
                                        if (treCharacterList.Nodes.ContainsKey(node.Name))
                                        {
                                            treCharacterList.Nodes.RemoveByKey(node.Name);
                                        }
                                        treCharacterList.Nodes.Insert(1, node);
                                    }

                                    node.Expand();
                                }
                                catch (ObjectDisposedException e)
                                {
                                    Log.Trace(e);
                                }
                                catch (InvalidAsynchronousStateException e)
                                {
                                    Log.Trace(e);
                                }
                                catch (ArgumentException e)
                                {
                                    Log.Trace(e);
                                }
                                catch (Exception e)
                                {
                                    Log.Warn(e);
                                }
                            });
                        }
                        Log.Info("Task to get and add CharacterRosterTreeNodes for plugin " + plugin + " finished.");
                    });
                }
            });
            Log.Info("Populating CharacterRosterTreeNode (MainThread).");
            if (objFavoriteNode != null)
            {
                if (blnAddFavoriteNode)
                {
                    treCharacterList.Nodes.Add(objFavoriteNode);
                    objFavoriteNode.Expand();
                }
                else
                {
                    objFavoriteNode.Nodes.Clear();
                    foreach (TreeNode objNode in lstFavoritesNodes)
                    {
                        if (objNode != null)
                        {
                            objFavoriteNode.Nodes.Add(objNode);
                        }
                    }
                }
            }

            if (objRecentNode != null)
            {
                if (blnAddRecentNode)
                {
                    treCharacterList.Nodes.Add(objRecentNode);
                    objRecentNode.Expand();
                }
                else
                {
                    try
                    {
                        objRecentNode.Nodes.Clear();
                        foreach (TreeNode objNode in lstRecentsNodes)
                        {
                            if (objNode != null)
                            {
                                objRecentNode.Nodes.Add(objNode);
                            }
                        }
                    }
                    catch (ObjectDisposedException e)
                    {
                        //just swallow this
                        Log.Trace(e, "ObjectDisposedException can be ignored here.");
                    }
                }
            }
            if (objWatchNode != null)
            {
                if (blnAddWatchNode)
                {
                    treCharacterList.Nodes.Add(objWatchNode);
                    objWatchNode.Expand();
                }
                else
                {
                    objWatchNode.Nodes.Clear();
                    foreach (TreeNode objNode in lstWatchNodes)
                    {
                        if (objNode != null)
                        {
                            objWatchNode.Nodes.Add(objNode);
                        }
                    }
                }
            }
            treCharacterList.ExpandAll();
            UpdateCharacter(treCharacterList.SelectedNode?.Tag as CharacterCache);
        }
コード例 #37
0
ファイル: DataAccessProfiler.cs プロジェクト: spspaner/Olive
 public static void Reset() => Watches = new ConcurrentBag <Watch>();
コード例 #38
0
        /// <summary>
        /// Download all docs.
        /// </summary>
        public void DownloadAll()
        {
            ConcurrentBag <ZipArchive> zips   = new ConcurrentBag <ZipArchive>();
            List <ManualResetEvent>    resets = new List <ManualResetEvent>();

            foreach (string src in SourcesToUse)
            {
                ManualResetEvent evt = new ManualResetEvent(false);
                resets.Add(evt);
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        ZipArchive zip = DownloadZip(src);
                        zips.Add(zip);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Zip download exception {ex}");
                        LoadErrors.Add($"Zip download error: {ex.GetType().Name}: {ex.Message}");
                    }
                    evt.Set();
                });
            }
            foreach (ManualResetEvent evt in resets)
            {
                evt.WaitOne();
            }
            foreach (ZipArchive zip in zips)
            {
                try
                {
                    string[] fullLines = ReadLines(zip);
                    LoadDataFromLines(fullLines);
                }
                catch (Exception ex)
                {
                    LoadErrors.Add($"Internal exception - {ex.GetType().FullName} ... see bot console for details.");
                    Console.WriteLine($"Error: {ex}");
                }
            }
            try
            {
                ReadGuides();
            }
            catch (Exception ex)
            {
                LoadErrors.Add($"Internal exception - {ex.GetType().FullName} ... see bot console for details.");
                Console.WriteLine($"Error: {ex}");
            }
            foreach (MetaObject obj in AllMetaObjects())
            {
                try
                {
                    obj.PostCheck(this);
                    obj.Searchable = obj.GetAllSearchableText().ToLowerFast();
                }
                catch (Exception ex)
                {
                    LoadErrors.Add($"Internal exception while checking {obj.Type.Name} '{obj.Name}' - {ex.GetType().FullName} ... see bot console for details.");
                    Console.WriteLine($"Error with {obj.Type.Name} '{obj.Name}': {ex}");
                }
            }
            foreach (string str in LoadErrors)
            {
                Console.WriteLine($"Load error: {str}");
            }
        }
コード例 #39
0
ファイル: ManualScanner.cs プロジェクト: zanzo420/Squalr
        /// <summary>
        /// Begins the manual scan based on the provided snapshot and parameters.
        /// </summary>
        /// <param name="snapshot">The snapshot on which to perfrom the scan.</param>
        /// <param name="constraints">The collection of scan constraints to use in the manual scan.</param>
        /// <param name="taskIdentifier">The unique identifier to prevent duplicate tasks.</param>
        /// <returns></returns>
        public static TrackableTask <Snapshot> Scan(Snapshot snapshot, ConstraintNode constraints, String taskIdentifier = null)
        {
            try
            {
                return(TrackableTask <Snapshot>
                       .Create(ManualScanner.Name, taskIdentifier, out UpdateProgress updateProgress, out CancellationToken cancellationToken)
                       .With(Task <Snapshot> .Run(() =>
                {
                    Snapshot result = null;

                    try
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        Int32 processedPages = 0;
                        ConcurrentBag <IList <SnapshotRegion> > regions = new ConcurrentBag <IList <SnapshotRegion> >();

                        ParallelOptions options = ParallelSettings.ParallelSettingsFastest.Clone();
                        options.CancellationToken = cancellationToken;

                        Parallel.ForEach(
                            snapshot.OptimizedSnapshotRegions,
                            options,
                            (region) =>
                        {
                            // Check for canceled scan
                            cancellationToken.ThrowIfCancellationRequested();

                            if (!region.ReadGroup.CanCompare(constraints.HasRelativeConstraint()))
                            {
                                return;
                            }

                            SnapshotElementVectorComparer vectorComparer = new SnapshotElementVectorComparer(region: region, constraints: constraints);
                            IList <SnapshotRegion> results = vectorComparer.Compare();

                            if (!results.IsNullOrEmpty())
                            {
                                regions.Add(results);
                            }

                            // Update progress every N regions
                            if (Interlocked.Increment(ref processedPages) % 32 == 0)
                            {
                                updateProgress((float)processedPages / (float)snapshot.RegionCount * 100.0f);
                            }
                        });
                        //// End foreach Region

                        // Exit if canceled
                        cancellationToken.ThrowIfCancellationRequested();

                        result = new Snapshot(ManualScanner.Name, regions.SelectMany(region => region));
                        stopwatch.Stop();
                        Logger.Log(LogLevel.Info, "Scan complete in: " + stopwatch.Elapsed);
                    }
                    catch (OperationCanceledException ex)
                    {
                        Logger.Log(LogLevel.Warn, "Scan canceled", ex);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, "Error performing scan", ex);
                    }

                    return result;
                }, cancellationToken)));
            }
            catch (TaskConflictException ex)
            {
                Logger.Log(LogLevel.Warn, "Unable to start scan. Scan is already queued.");
                throw ex;
            }
        }
コード例 #40
0
        public void ImplementBamlInitializer(Builder builder)
        {
            this.LogInfo($"Checking for xaml/baml resources without initializers.");

            var xamlList = builder.ResourceNames?.Where(x => x.EndsWith(".baml")).Select(x => x.Replace(".baml", ".xaml")).ToArray();

            if (xamlList == null || xamlList.Length == 0 || !builder.TypeExists("System.Windows.Application"))
            {
                return;
            }

            var application        = new __Application(builder);
            var extensions         = new __Extensions(builder);
            var resourceDictionary = new __ResourceDictionary(builder);
            var collection         = new __ICollection_1(builder);

            this.LogInfo($"Implementing XAML initializer for baml resources.");

            // First we have to find every InitializeComponent method so that we can remove bamls that are already initialized.
            var allInitializeComponentMethods = builder.FindMethodsByName(SearchContext.Module, "InitializeComponent", 0).Where(x => !x.IsAbstract);
            var ldStrs = new ConcurrentBag <string>();

            Parallel.ForEach(allInitializeComponentMethods, methods =>
            {
                foreach (var str in methods.GetLoadStrings())
                {
                    ldStrs.Add(str);
                }
            });

            var xamlWithInitializers         = ldStrs.Select(x => x.Substring(x.IndexOf("component/") + "component/".Length)).ToArray();
            var xamlThatRequiredInitializers = xamlList.Where(x => !xamlWithInitializers.Contains(x));

            var resourceDictionaryMergerClass = builder.CreateType("XamlGeneratedNamespace", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, "<>_generated_resourceDictionary_Loader");

            resourceDictionaryMergerClass.CustomAttributes.Add(builder.GetType("Cauldron.Activator.ComponentAttribute"), resourceDictionary.Type.Fullname);
            resourceDictionaryMergerClass.CustomAttributes.AddEditorBrowsableAttribute(EditorBrowsableState.Never);
            resourceDictionaryMergerClass.CustomAttributes.AddCompilerGeneratedAttribute();

            //var method = resourceDictionaryMergerClass.CreateMethod(Modifiers.Private, "AddToDictionary", typeof(string));
            resourceDictionaryMergerClass.CreateConstructor().NewCode().Context(x =>
            {
                x.Load(x.This).Call(builder.GetType(typeof(object)).Import().ParameterlessContructor.Import());

                var resourceDick = x.CreateVariable(collection.Type.MakeGeneric(resourceDictionary.Type));
                x.Call(
                    x.NewCode().Call(x.NewCode().Call(application.Current) /* Instance */, application.Resources) /* Instance */, resourceDictionary.MergedDictionaries)
                .StoreLocal(resourceDick);

                var resourceDictionaryInstance = x.CreateVariable(resourceDictionary.Type);

                foreach (var item in xamlThatRequiredInitializers)
                {
                    x.NewObj(resourceDictionary.Ctor).StoreLocal(resourceDictionaryInstance);
                    x.Call(resourceDick, collection.Add.MakeGeneric(resourceDictionary.Type), resourceDictionaryInstance);
                    x.Call(resourceDictionaryInstance, resourceDictionary.SetSource,
                           x.NewCode().Call(extensions.RelativeUri, $"/{Path.GetFileNameWithoutExtension(this.Builder.Name)};component/{item}")); // TODO -Need modification for UWP)
                }
            })
            .Return()
            .Replace();

            resourceDictionaryMergerClass.ParameterlessContructor.CustomAttributes.AddEditorBrowsableAttribute(EditorBrowsableState.Never);
            resourceDictionaryMergerClass.ParameterlessContructor.CustomAttributes.Add(builder.GetType("Cauldron.Activator.ComponentConstructorAttribute"));
        }
コード例 #41
0
        public override void Initialize(AnalysisContext context)
        {
            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);

            context.RegisterCompilationStartAction(compilationStartContext =>
            {
                if (compilationStartContext.Compilation.GetOrCreateTypeByMetadataName(CodeActionMetadataName) == null)
                {
                    // No reference to core Workspaces assembly.
                    return;
                }

                INamedTypeSymbol?diagnosticAnalyzer = compilationStartContext.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftCodeAnalysisDiagnosticsDiagnosticAnalyzer);
                if (diagnosticAnalyzer == null)
                {
                    // Does not contain any diagnostic analyzers.
                    return;
                }

                var hasAccessToTypeFromWorkspaceAssemblies = false;
                var namedTypesToAccessedTypesMap           = new ConcurrentDictionary <INamedTypeSymbol, ImmutableHashSet <INamedTypeSymbol> >();
                var diagnosticAnalyzerTypes = new ConcurrentBag <INamedTypeSymbol>();
                compilationStartContext.RegisterSymbolAction(symbolContext =>
                {
                    var namedType = (INamedTypeSymbol)symbolContext.Symbol;
                    if (namedType.DerivesFrom(diagnosticAnalyzer, baseTypesOnly: true))
                    {
                        diagnosticAnalyzerTypes.Add(namedType);
                    }

                    var usedTypes = GetUsedNamedTypes(namedType, symbolContext.Compilation, symbolContext.CancellationToken, ref hasAccessToTypeFromWorkspaceAssemblies);
                    var added     = namedTypesToAccessedTypesMap.TryAdd(namedType, usedTypes);
                    Debug.Assert(added);
                }, SymbolKind.NamedType);

                compilationStartContext.RegisterCompilationEndAction(compilationEndContext =>
                {
                    if (diagnosticAnalyzerTypes.IsEmpty || !hasAccessToTypeFromWorkspaceAssemblies)
                    {
                        return;
                    }

                    var typesToProcess                = new Queue <INamedTypeSymbol>();
                    var processedTypes                = new HashSet <INamedTypeSymbol>();
                    var violatingTypeNamesBuilder     = new SortedSet <string>();
                    var violatingUsedTypeNamesBuilder = new SortedSet <string>();
                    foreach (INamedTypeSymbol declaredType in namedTypesToAccessedTypesMap.Keys)
                    {
                        if (!diagnosticAnalyzerTypes.Contains(declaredType))
                        {
                            continue;
                        }

                        typesToProcess.Clear();
                        processedTypes.Clear();
                        violatingTypeNamesBuilder.Clear();
                        violatingUsedTypeNamesBuilder.Clear();
                        typesToProcess.Enqueue(declaredType);
                        do
                        {
                            var typeToProcess = typesToProcess.Dequeue();
                            Debug.Assert(typeToProcess.ContainingAssembly.Equals(declaredType.ContainingAssembly));
                            Debug.Assert(namedTypesToAccessedTypesMap.ContainsKey(typeToProcess));

                            foreach (INamedTypeSymbol usedType in namedTypesToAccessedTypesMap[typeToProcess])
                            {
                                if (usedType.ContainingAssembly != null &&
                                    s_WorkspaceAssemblyNames.Contains(usedType.ContainingAssembly.Name))
                                {
                                    violatingTypeNamesBuilder.Add(usedType.ToDisplayString());
                                    violatingUsedTypeNamesBuilder.Add(typeToProcess.ToDisplayString());
                                }

                                if (!processedTypes.Contains(usedType) && namedTypesToAccessedTypesMap.ContainsKey(usedType))
                                {
                                    typesToProcess.Enqueue(usedType);
                                }
                            }

                            processedTypes.Add(typeToProcess);
                        } while (typesToProcess.Count != 0);

                        if (violatingTypeNamesBuilder.Count > 0)
                        {
                            string[] args;
                            DiagnosticDescriptor rule;
                            if (violatingUsedTypeNamesBuilder.Count == 1 && violatingUsedTypeNamesBuilder.Single() == declaredType.ToDisplayString())
                            {
                                // Change diagnostic analyzer type '{0}' to remove all direct accesses to type(s) '{1}'
                                rule = DoNotUseTypesFromAssemblyDirectRule;
                                args = new[]
                                {
                                    declaredType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat),
                                    string.Join(", ", violatingTypeNamesBuilder)
                                };
                            }
                            else
                            {
                                // Change diagnostic analyzer type '{0}' to remove all direct and/or indirect accesses to type(s) '{1}', which access type(s) '{2}'
                                rule = DoNotUseTypesFromAssemblyIndirectRule;
                                args = new[]
                                {
                                    declaredType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat),
                                    string.Join(", ", violatingUsedTypeNamesBuilder),
                                    string.Join(", ", violatingTypeNamesBuilder)
                                };
                            }

                            Diagnostic diagnostic = declaredType.CreateDiagnostic(rule, args);
                            compilationEndContext.ReportDiagnostic(diagnostic);
                        }
                    }
                });
            });
        }
コード例 #42
0
        public static IEnumerable <SouqProductsGrid2> SearchProductsParallel(string categoryId)
        {
            ConcurrentBag <SouqProductsGrid2> tmpList = new ConcurrentBag <SouqProductsGrid2>();
            int requestCount = 0;

retry_peek:
            try
            {
                SyncSouqCookies();
                var peek_url = $"https://egypt.souq.com/eg-en/{categoryId}/s/?page=1&section=1&action=grid";
                var peek_obj = WebScrapper.GetDownloadJson <SouqProductsGrid2>(peek_url,
                                                                               headers: new Dictionary <string, string>()
                {
                    { "X-Requested-With", "XMLHttpRequest" }
                });

                if (!string.IsNullOrEmpty(peek_obj.redirect_url))
                {
                    categoryId = peek_obj.redirect_url.ExtractCategoryId();
                    goto retry_peek;
                }

                requestCount = (int)Math.Ceiling((float)peek_obj.jsonData.meta_data.total / SouqApiConstants.ProductSectionListLimit);
            }
            catch (Exception ex)
            {
                /*Console Write*/
                Console.WriteLine(DateTime.Now + "-" + ex.InnerException?.Message ?? ex.Message);
                goto retry_peek;
            }


            var pagingGenerator = new PagingLinkGenerator(requestCount);

            Parallel.ForEach(pagingGenerator, (pageInfo) =>
            {
                retry:
                SyncSouqCookies();
                var url = $"https://egypt.souq.com/eg-en/{categoryId}/s/?page={pageInfo.Page}&section={pageInfo.Section}&action=grid";
                //Console.WriteLine(url);
                try
                {
                    var obj = WebScrapper.GetDownloadJson <SouqProductsGrid2>(url,
                                                                              headers: new Dictionary <string, string>()
                    {
                        { "X-Requested-With", "XMLHttpRequest" }
                    });

                    /*Under Test Fields*/
                    var is_ags_test = obj.jsonData.units.Where(l => l.is_ags);
                    if (is_ags_test.Any())
                    {
                        Debugger.Break();
                    }

                    var is_price_cbt = obj.jsonData.units.Where(l => l.price_cbt != "");
                    if (is_price_cbt.Any())
                    {
                        Debugger.Break();
                    }

                    var is_coupon = obj.jsonData.units.Where(l => l.coupon != null && l.coupon.ToString() != "[]");
                    if (is_coupon.Any())
                    {
                        Debugger.Break();
                    }

                    var is_InternationalSeller = obj.jsonData.units.Where(l => l.isInternationalSeller);
                    if (is_InternationalSeller.Any())
                    {
                        Debugger.Break();
                    }

                    var is_shipping = obj.jsonData.units.Where(l => l.shipping != false);
                    if (is_shipping.Any())
                    {
                        Debugger.Break();
                    }

                    var is_selling_points = obj.jsonData.units.Where(l => l.selling_points.ToString() != "False");
                    if (is_selling_points.Any())
                    {
                        Debugger.Break();
                    }

                    tmpList.Add(obj);
                }
                catch (Exception ex)
                {
                    /*Console Write*/
                    Console.WriteLine(DateTime.Now + "-" + ex.InnerException?.Message ?? ex.Message);
                    goto retry;
                }
            });

            return(tmpList.ToList());
        }
コード例 #43
0
            public Subscription(MethodInfo methodInfo)
            {
                MethodInfo = methodInfo;

                Adapters = new ConcurrentBag <Func <object, object, bool> >();
            }
コード例 #44
0
        /// <summary>
        /// Retrieves the stream,
        /// including all events in it.
        /// </summary>
        /// <param name="databaseId">The databae to search in.</param>
        /// <param name="streamKey">The key to the specific stream instance, (in format [category]@[guid])</param>
        /// <returns>The entire stream with all events.</returns>
        public async Task <Result <ReadOnlyStream> > GetStreamAsync(string databaseId, string streamKey, int newSinceVersion = -1)
        {
            var(streamName, streamId) = GetKeyParts(streamKey);
            var batches = new List <EventBatch>();

            var database = await GetDataBase(databaseId);

            var dbCategoriesEntries = await GetCategoriesEntries(database); // Get all categories

            var categoryEntry = dbCategoriesEntries.SingleOrDefault(s => s.Item1.ToUtfString() == streamName);

            if (categoryEntry.Item1 == null || categoryEntry.Item2 == null)
            {
                return(Result.Fail <ReadOnlyStream>("Stream does not exist!"));
            }

            // Here we get all streams of the category
            // We get the category md, whose entries contains all streamKeys of the category
            // (up to 998 though, and then the next 998 can be found when following link in key "next")
            (List <byte>, ulong)streamEntry;
            var category_MDataInfo = await _mDataInfo.DeserialiseAsync(categoryEntry.Item2);

            var streamsKeys = await _mData.ListKeysAsync(category_MDataInfo);  // get the entries of this specific category

            // lists all instances of this category (key: streamKey, value: serialized mdata info handle)

            try
            {
                var foundKey = streamsKeys.First(key => key.Val.ToUtfString() == streamKey);
                streamEntry = await _mData.GetValueAsync(category_MDataInfo, foundKey.Val);  // find the instance matching this streamKey
            }
            catch (InvalidOperationException ex)
            {
                return(Result.Fail <ReadOnlyStream>("Stream does not exist!"));
            }

            var stream_MDataInfo = await _mDataInfo.DeserialiseAsync(streamEntry.Item1);

            var streamDataKeys = await _mData.ListKeysAsync(stream_MDataInfo); // get the entries of this specific stream instance

            var bag    = new ConcurrentBag <EventBatch>();
            var tasks1 = streamDataKeys.Select(async key =>  // foreach event batch in stream
            {
                var eventBatchEntry = await _mData.GetValueAsync(stream_MDataInfo, key.Val);
                var stringKey       = key.Val.ToUtfString();
                if (METADATA_KEY == stringKey || VERSION_KEY == stringKey)
                {
                    return;
                }

                // only fetch events more recent than version passed as argument
                var versionRange = stringKey.Split('@');
                if (newSinceVersion >= int.Parse(versionRange.Last()))
                {
                    return; // this will speed up retrieval when we have a cached version of the stream locally (as we only request new events since last version)
                }
                var jsonBatch    = eventBatchEntry.Item1.ToUtfString();
                var batch        = JsonConvert.DeserializeObject <StoredEventBatch>(jsonBatch);
                var eventDataBag = new ConcurrentBag <EventData>();

                var tasks2 = batch.Select(async stored => // foreach event in event batch
                {
                    var eventData = await GetEventDataFromAddress(stored);
                    eventDataBag.Add(eventData);
                });

                await Task.WhenAll(tasks2);

                bag.Add(new EventBatch(streamKey, eventDataBag.First().MetaData.CausationId, eventDataBag.OrderBy(c => c.MetaData.SequenceNumber).ToList()));
            });

            await Task.WhenAll(tasks1);

            batches.AddRange(bag.OrderBy(c => c.Body.First().MetaData.SequenceNumber));

            if (batches.Count == 0)
            {
                return(Result.OK((ReadOnlyStream) new EmptyStream(streamName, streamId)));
            }
            else
            {
                return(Result.OK((ReadOnlyStream) new PopulatedStream(streamName, streamId, batches))); // also checks integrity of data structure (with regards to sequence nr)
            }
        }
コード例 #45
0
        private static void SolveDay24()
        {
            var input = File.ReadAllLines("Test.txt");

            HashSet <Tuple <int, int> > flipped = new HashSet <Tuple <int, int> >();

            foreach (var item in input)
            {
                int currentX = 0;
                int currentY = 0;

                for (int i = 0; i < item.Length; i++)
                {
                    if (item[i] == 'e')
                    {
                        currentX++;
                    }
                    else if (item[i] == 'w')
                    {
                        currentX--;
                    }
                    else
                    {
                        var strNextDirection = item.Substring(i, 2);
                        if (strNextDirection == "nw")
                        {
                            currentY++;
                        }
                        else if (strNextDirection == "ne")
                        {
                            currentX++;
                            currentY++;
                        }
                        else if (strNextDirection == "se")
                        {
                            currentY--;
                        }
                        else if (strNextDirection == "sw")
                        {
                            currentX--;
                            currentY--;
                        }

                        i++;
                    }
                }

                var tileToFlip = new Tuple <int, int>(currentX, currentY);
                if (flipped.Contains(tileToFlip))
                {
                    flipped.Remove(tileToFlip);
                }
                else
                {
                    flipped.Add(tileToFlip);
                }
            }

            Console.WriteLine("Day 24 - Part 1: " + flipped.Count);

            for (int i = 1; i <= 100; i++)
            {
                var newFlipped = new HashSet <Tuple <int, int> >(flipped);

                foreach (var item in flipped)
                {
                    var containedCount = 0;
                    if (flipped.Contains(new Tuple <int, int>(item.Item1 - 1, item.Item2)))
                    {
                        containedCount++;
                    }

                    if (flipped.Contains(new Tuple <int, int>(item.Item1, item.Item2 - 1)))
                    {
                        containedCount++;
                    }

                    if (flipped.Contains(new Tuple <int, int>(item.Item1 - 1, item.Item2 - 1)))
                    {
                        containedCount++;
                    }

                    if (containedCount > 2)
                    {
                        newFlipped.Remove(item);
                        continue;
                    }

                    if (flipped.Contains(new Tuple <int, int>(item.Item1 + 1, item.Item2)))
                    {
                        containedCount++;
                    }

                    if (containedCount > 2)
                    {
                        newFlipped.Remove(item);
                        continue;
                    }

                    if (flipped.Contains(new Tuple <int, int>(item.Item1, item.Item2 + 1)))
                    {
                        containedCount++;
                    }

                    if (containedCount == 1)
                    {
                        continue;
                    }
                    else if (containedCount > 2)
                    {
                        newFlipped.Remove(item);
                        continue;
                    }

                    if (flipped.Contains(new Tuple <int, int>(item.Item1 + 1, item.Item2 + 1)))
                    {
                        containedCount++;
                    }

                    if (containedCount == 0 || containedCount > 2)
                    {
                        newFlipped.Remove(item);
                        continue;
                    }
                }

                int minX = flipped.Select(x => x.Item1).Min();
                int maxX = flipped.Select(x => x.Item1).Max();
                int minY = flipped.Select(x => x.Item2).Min();
                int maxY = flipped.Select(x => x.Item2).Max();

                var cb = new ConcurrentBag <Tuple <int, int> >();

                Parallel.For((minX - 1), (maxX + 2), x =>
                {
                    for (int y = (minY - 1); y <= (maxY + 1); y++)
                    {
                        var current = new Tuple <int, int>(x, y);

                        var nextToTile = new List <Tuple <int, int> >()
                        {
                            new Tuple <int, int>((x - 1), y),
                            new Tuple <int, int>(x, (y - 1)),
                            new Tuple <int, int>((x - 1), (y - 1)),
                            new Tuple <int, int>((x + 1), y),
                            new Tuple <int, int>(x, (y + 1)),
                            new Tuple <int, int>((x + 1), (y + 1)),
                        };

                        int containedCount = nextToTile.Where(x => flipped.Contains(x)).Count();
                        if (containedCount == 2)
                        {
                            cb.Add(current);
                        }
                    }
                });

                foreach (var item in cb)
                {
                    newFlipped.Add(item);
                }

                flipped = new HashSet <Tuple <int, int> >(newFlipped);
                //if (i % 25 == 0)
                //	Console.WriteLine(i + " : " + flipped.Count);
            }

            Console.WriteLine("Day 24 - Part 2: " + flipped.Count);
        }
コード例 #46
0
ファイル: LocalNode.cs プロジェクト: tonybobpublic/neo
        private void AddTransactionLoop()
        {
            while (!cancellationTokenSource.IsCancellationRequested)
            {
                new_tx_event.WaitOne();
                Transaction[] transactions;
                lock (temp_pool)
                {
                    if (temp_pool.Count == 0)
                    {
                        continue;
                    }
                    transactions = temp_pool.ToArray();
                    temp_pool.Clear();
                }
                ConcurrentBag <Transaction> verified = new ConcurrentBag <Transaction>();
                lock (Blockchain.Default.PersistLock)
                {
                    lock (mem_pool)
                    {
                        transactions = transactions.Where(p => !mem_pool.ContainsKey(p.Hash) && !Blockchain.Default.ContainsTransaction(p.Hash)).ToArray();

                        if (transactions.Length == 0)
                        {
                            continue;
                        }

                        Transaction[] tmpool = mem_pool.Values.Concat(transactions).ToArray();

                        ParallelOptions po = new ParallelOptions();
                        po.CancellationToken      = Blockchain.Default.VerificationCancellationToken.Token;
                        po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;

                        try
                        {
                            Parallel.ForEach(transactions.AsParallel(), po, tx =>
                            {
                                if (tx.Verify(tmpool))
                                {
                                    verified.Add(tx);
                                }
                            });
                        }
                        catch (OperationCanceledException)
                        {
                            lock (temp_pool)
                            {
                                foreach (Transaction tx in transactions)
                                {
                                    temp_pool.Add(tx);
                                }
                            }

                            continue;
                        }

                        if (verified.Count == 0)
                        {
                            continue;
                        }

                        foreach (Transaction tx in verified)
                        {
                            mem_pool.Add(tx.Hash, tx);
                        }

                        CheckMemPool();
                    }
                }
                RelayDirectly(verified);
                if (InventoryReceived != null)
                {
                    foreach (Transaction tx in verified)
                    {
                        InventoryReceived(this, tx);
                    }
                }
            }
        }
コード例 #47
0
        /// <summary>
        /// Splits order knight armor leveled list and adds new parts to npcs
        /// </summary>
        internal static void SELL0NPCOrderKnightArmor100()
        {
            LEVI leveled = ConvertedRecords["LEVI"].Find(x => x.EditorId.Equals("SELL0NPCOrderKnightArmor100\0")).Record as LEVI;

            if (IsNull(leveled))
            {
                throw new Exception("No such LEVI");
            }

            var itemReferencesBag = new ConcurrentBag <ARMO>();

            Parallel.ForEach(leveled.ITEM, (item) =>
            {
                itemReferencesBag.Add(ConvertedRecords["ARMO"].Find(x => x.EditorId.Equals(item.INAM.ItemEditorId)).Record as ARMO);
            });
            var itemReferences = itemReferencesBag.ToList();

            if (itemReferences.Count == 0)
            {
                throw new Exception("No records found");
            }

            var partitions    = new Dictionary <string, LEVI>();
            var names         = new Dictionary <string, string>();
            var processingMap = new Dictionary <string, Action <IEquipement> >();

            {
                processingMap.Add("Helm", EquipementProcessing.SEOrderKnightHelm);
                processingMap.Add("PauldL", EquipementProcessing.SEOrderKnightPauldronLeft);
                processingMap.Add("PauldR", EquipementProcessing.SEOrderKnightPauldronRight);
                processingMap.Add("GloveL", EquipementProcessing.SEOrderKnightGloveLeft);
                processingMap.Add("GloveR", EquipementProcessing.SEOrderKnightGloveRight);
                processingMap.Add("Greaves", EquipementProcessing.SEOrderKnightGreaves);
                processingMap.Add("Boots", EquipementProcessing.SEOrderKnightBoots);

                partitions.Add("Helm", CloneLeveledList(leveled));
                partitions.Add("PauldL", CloneLeveledList(leveled));
                partitions.Add("PauldR", CloneLeveledList(leveled));
                partitions.Add("GloveL", CloneLeveledList(leveled));
                partitions.Add("GloveR", CloneLeveledList(leveled));
                partitions.Add("Greaves", CloneLeveledList(leveled));
                partitions.Add("Boots", CloneLeveledList(leveled));


                names.Add("Helm", "Helm");
                names.Add("PauldL", "Left Pauldron");
                names.Add("PauldR", "Right Pauldron");
                names.Add("GloveL", "Left Glove");
                names.Add("GloveR", "Right Glove");
                names.Add("Greaves", "Greaves");
                names.Add("Boots", "Boots");
            }

            foreach (var key in partitions.Keys)
            {
                partitions[key].NAME.EditorId = partitions[key].NAME.EditorId.Replace("Armor", key);
                foreach (var item in partitions[key].ITEM)
                {
                    ARMO referenceItem = itemReferences.ToList().Find(x => x.NAME.EditorId.Equals(item.INAM.ItemEditorId));
                    var  newItem       = CloneArmor(referenceItem);

                    newItem.NAME.EditorId = newItem.NAME.EditorId.Replace("Armor", key);
                    newItem.FNAM.Name     = newItem.FNAM.Name.Replace("Armor", names[key]);
                    processingMap[key].Invoke(newItem);

                    ConvertedRecords["ARMO"].Add(new ConvertedRecordData("generated", "ARMO", newItem.NAME.EditorId, newItem));
                    item.INAM.ItemEditorId = newItem.NAME.EditorId;
                }
                ConvertedRecords["LEVI"].Add(new ConvertedRecordData("generated", "LEVI", partitions[key].NAME.EditorId, partitions[key]));
            }

            foreach (var item in itemReferences)
            {
                EquipementProcessing.SEOrderKnightCuirass(item);
            }


            Parallel.ForEach(ConvertedRecords["NPC_"], (npc) =>
            {
                bool itemPresent = false;
                foreach (var item in (npc.Record as NPC_).NPCO)
                {
                    if (item.ItemId.Contains(leveled.NAME.EditorId))
                    {
                        itemPresent = true;
                        break;
                    }
                }
                if (itemPresent)
                {
                    (npc.Record as NPC_).NPCO.AddRange(partitions.Values.Select(x => new NPCO {
                        Count = 1, ItemId = x.NAME.EditorId
                    }));
                }
            });
        }
コード例 #48
0
 public void ResetClonedDocuments()
 {
     Parallel.ForEach(_clonedDocuments, x => x.Dispose());
     _clonedDocuments = new ConcurrentBag <IDocument>();
 }
コード例 #49
0
 public TpProfiler()
 {
     _threadId      = Thread.CurrentThread.ManagedThreadId;
     _timeIntervals = new ConcurrentBag <TimeInterval>();
 }
コード例 #50
0
        private IDictionary <string, object> ScopeToDictionary(Exception errorOrNull)
        {
            // If we have an exception with Data, add that data to the scope.
            if (errorOrNull?.Data?.Count > 0)
            {
                if (scopeOrNull == null)
                {
                    this.scopeOrNull = new ConcurrentBag <RavenStructuredLogScope>();
                }

                foreach (var dictionaryEntry in errorOrNull.Data)
                {
                    this.scopeOrNull.Add(new RavenStructuredLogScope(dictionaryEntry));
                }
            }

            if (this.scopeOrNull == null)
            {
                return(null);
            }

            var dictionary    = new Dictionary <string, object>(this.scopeOrNull.Count * 3);
            var unnamedValues = 0;

            string GetNextNoNameKey()
            {
                for (var i = unnamedValues; i < 50; i++)
                {
                    var key = i.ToString();
                    if (!dictionary.ContainsKey(key))
                    {
                        unnamedValues++;
                        return(key);
                    }
                }

                return(Guid.NewGuid().ToString());
            }

            foreach (var scopeObj in this.scopeOrNull)
            {
                switch (scopeObj.Value)
                {
                case IEnumerable <KeyValuePair <string, object> > pairs:
                    {
                        foreach (var pair in pairs)
                        {
                            dictionary.Add(GetUniqueDictionaryKey(dictionary, pair.Key), pair.Value);
                        }

                        break;
                    }

                case System.Collections.DictionaryEntry dictionaryEntry:
                    // This handles Exception.Data and other legacy non-generic dictionary data.
                    dictionary.Add(GetUniqueDictionaryKey(dictionary, dictionaryEntry.Key?.ToString()), dictionaryEntry.Value);
                    break;

                case KeyValuePair <string, object> pair:
                    dictionary.Add(GetUniqueDictionaryKey(dictionary, pair.Key), pair.Value);
                    break;

                default:
                    dictionary.Add(GetNextNoNameKey(), scopeObj.Value);
                    break;
                }
            }

            return(dictionary);
        }
コード例 #51
0
        public async Task <PortfolioPerformanceReport> GeneratePortfolioPerformanceReport(Accounts account,
                                                                                          List <Positions> positions, DataRequestType.RequestType requestType)
        {
            var    currentBalance    = account.Balance;
            double totalCurrentValue = 0;
            double investedBalance   = 0;

            var positionPerformanceBag = new ConcurrentBag <PositionPerformance>();

            var calcTasks = positions.Select(async position =>
            {
                var positionInvestedValue = position.Value;
                investedBalance          += positionInvestedValue;
                var symbol           = position.Symbol;
                var positionQuantity = position.Quantity;

                var originalCostPerShare = positionInvestedValue / positionQuantity;

                double currentPrice = 0;

                switch (requestType)
                {
                case DataRequestType.RequestType.CurrentMarketDataRequest:
                    var marketData = await _marketDataServiceClient.GetLatestPrice(symbol);
                    currentPrice   = marketData.price;
                    break;

                case DataRequestType.RequestType.HistoricalMarketDataRequest:
                    var historicalData =
                        await _marketDataServiceClient.GetHistoricalData(symbol, DateTime.Today.ToString("yyyyMMdd"));
                    currentPrice = historicalData.ClosingPrice;
                    break;

                default:
                    currentPrice = 0;
                    break;
                }

                var positionCurrentValue = currentPrice * positionQuantity;
                var positionUgl          = (positionCurrentValue) - (positionInvestedValue);

                double positionUglPercentage = 0;
                if (positionUgl > 0)
                {
                    positionUglPercentage = (positionUgl / positionInvestedValue) * 100;
                }
                else
                {
                    positionUglPercentage = (-1 * (positionUgl / positionInvestedValue)) * 100;
                }

                positionPerformanceBag.Add(new PositionPerformance()
                {
                    CurrentPrice          = currentPrice,
                    CurrentValue          = positionCurrentValue,
                    PositionUgl           = positionUgl,
                    PositionUglPercentage = positionUglPercentage,
                    PurchasedPrice        = originalCostPerShare,
                    PurchasedQuantity     = positionQuantity,
                    PurchasedValue        = positionInvestedValue,
                    Symbol = symbol
                });
            });

            await Task.WhenAll(calcTasks);

            totalCurrentValue = positionPerformanceBag.Select(x => x.CurrentValue).Sum();

            var    portfolioUgl           = totalCurrentValue - investedBalance;
            double portfolioUglPercentage = 0;

            if (portfolioUgl > 0)
            {
                portfolioUglPercentage = (portfolioUgl / investedBalance) * 100;
            }
            else
            {
                portfolioUglPercentage = (-1 * (portfolioUglPercentage / investedBalance)) * 100;
            }

            var totalAccountValue = totalCurrentValue + currentBalance;

            // Generate Performance Report object
            return(new PortfolioPerformanceReport()
            {
                AccountId = account.Id,
                CurrentCashBalance = currentBalance,
                CurrentInvestmentValue = totalCurrentValue,
                PortfolioPerformance = new List <PositionPerformance>(positionPerformanceBag),
                CurrentTotalAccountValue = totalAccountValue,
                ReportDate = DateTime.Today,
                PortfolioUgl = portfolioUgl,
                PortfolioUglPercentage = portfolioUglPercentage
            });
        }
コード例 #52
0
        public async Task <OperationResult <Artist> > PerformMetaDataProvidersArtistSearch(AudioMetaData metaData)
        {
            SimpleContract.Requires <ArgumentNullException>(metaData != null, "Invalid MetaData");
            SimpleContract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(metaData.Artist), "Invalid MetaData, Missing Artist");

            var sw = new Stopwatch();

            sw.Start();
            var result = new Artist
            {
                Name = metaData.Artist.ToTitleCase(false)
            };
            var resultsExceptions = new List <Exception>();
            var artistGenres      = new List <string>();
            var artistImageUrls   = new List <string>();
            var artistName        = metaData.Artist;

            try
            {
                if (ITunesArtistSearchEngine.IsEnabled)
                {
                    var sw2          = Stopwatch.StartNew();
                    var iTunesResult = await ITunesArtistSearchEngine.PerformArtistSearch(artistName, 1).ConfigureAwait(false);

                    if (iTunesResult.IsSuccess)
                    {
                        var i = iTunesResult.Data.First();
                        if (i.AlternateNames != null)
                        {
                            result.AlternateNames = result.AlternateNames.AddToDelimitedList(i.AlternateNames);
                        }
                        if (i.Tags != null)
                        {
                            result.Tags = result.Tags.AddToDelimitedList(i.Tags);
                        }
                        if (i.Urls != null)
                        {
                            result.URLs = result.URLs.AddToDelimitedList(i.Urls);
                        }
                        if (i.ISNIs != null)
                        {
                            result.ISNI = result.ISNI.AddToDelimitedList(i.ISNIs);
                        }
                        if (i.ImageUrls != null)
                        {
                            artistImageUrls.AddRange(i.ImageUrls);
                        }
                        if (i.ArtistGenres != null)
                        {
                            artistGenres.AddRange(i.ArtistGenres);
                        }
                        result.CopyTo(new Artist
                        {
                            EndDate    = i.EndDate,
                            BioContext = i.Bio,
                            Profile    = i.Profile,
                            ITunesId   = i.iTunesId,
                            BeginDate  = i.BeginDate,
                            Name       = result.Name ?? i.ArtistName,
                            SortName   = result.SortName ?? i.ArtistSortName,
                            ArtistType = result.ArtistType ?? i.ArtistType
                        });
                    }

                    if (iTunesResult.Errors != null)
                    {
                        resultsExceptions.AddRange(iTunesResult.Errors);
                    }

                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: ITunesArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "iTunesArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (MusicBrainzArtistSearchEngine.IsEnabled)
                {
                    var sw2      = Stopwatch.StartNew();
                    var mbResult = await MusicBrainzArtistSearchEngine.PerformArtistSearch(result.Name, 1).ConfigureAwait(false);

                    if (mbResult.IsSuccess)
                    {
                        var mb = mbResult.Data.First();
                        if (mb.AlternateNames != null)
                        {
                            result.AlternateNames = result.AlternateNames.AddToDelimitedList(mb.AlternateNames);
                        }
                        if (mb.Tags != null)
                        {
                            result.Tags = result.Tags.AddToDelimitedList(mb.Tags);
                        }
                        if (mb.Urls != null)
                        {
                            result.URLs = result.URLs.AddToDelimitedList(mb.Urls);
                        }
                        if (mb.ISNIs != null)
                        {
                            result.ISNI = result.ISNI.AddToDelimitedList(mb.ISNIs);
                        }
                        if (mb.ImageUrls != null)
                        {
                            artistImageUrls.AddRange(mb.ImageUrls);
                        }
                        if (mb.ArtistGenres != null)
                        {
                            artistGenres.AddRange(mb.ArtistGenres);
                        }
                        if (!string.IsNullOrEmpty(mb.ArtistName) &&
                            !mb.ArtistName.Equals(result.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            result.AlternateNames.AddToDelimitedList(new[] { mb.ArtistName });
                        }
                        result.CopyTo(new Artist
                        {
                            EndDate       = mb.EndDate,
                            BioContext    = mb.Bio,
                            Profile       = mb.Profile,
                            MusicBrainzId = mb.MusicBrainzId,
                            DiscogsId     = mb.DiscogsId,
                            SpotifyId     = mb.SpotifyId,
                            ITunesId      = mb.iTunesId,
                            AmgId         = mb.AmgId,
                            BeginDate     = mb.BeginDate,
                            Name          = result.Name ?? mb.ArtistName,
                            SortName      = result.SortName ?? mb.ArtistSortName,
                            ArtistType    = mb.ArtistType
                        });
                    }

                    if (mbResult.Errors != null)
                    {
                        resultsExceptions.AddRange(mbResult.Errors);
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: MusicBrainzArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "MusicBrainzArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (LastFmArtistSearchEngine.IsEnabled)
                {
                    var sw2          = Stopwatch.StartNew();
                    var lastFmResult = await LastFmArtistSearchEngine.PerformArtistSearch(result.Name, 1).ConfigureAwait(false);

                    if (lastFmResult.IsSuccess)
                    {
                        var l = lastFmResult.Data.First();
                        if (l.AlternateNames != null)
                        {
                            result.AlternateNames = result.AlternateNames.AddToDelimitedList(l.AlternateNames);
                        }
                        if (l.Tags != null)
                        {
                            result.Tags = result.Tags.AddToDelimitedList(l.Tags);
                        }
                        if (l.Urls != null)
                        {
                            result.URLs = result.URLs.AddToDelimitedList(l.Urls);
                        }
                        if (l.ISNIs != null)
                        {
                            result.ISNI = result.ISNI.AddToDelimitedList(l.ISNIs);
                        }
                        if (l.ImageUrls != null)
                        {
                            artistImageUrls.AddRange(l.ImageUrls);
                        }
                        if (l.ArtistGenres != null)
                        {
                            artistGenres.AddRange(l.ArtistGenres);
                        }
                        if (!string.IsNullOrEmpty(l.ArtistName) &&
                            !l.ArtistName.Equals(result.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            result.AlternateNames.AddToDelimitedList(new[] { l.ArtistName });
                        }
                        result.CopyTo(new Artist
                        {
                            EndDate       = l.EndDate,
                            BioContext    = HttpEncoder.HtmlEncode(l.Bio),
                            Profile       = HttpEncoder.HtmlEncode(l.Profile),
                            MusicBrainzId = l.MusicBrainzId,
                            BeginDate     = l.BeginDate,
                            Name          = result.Name ?? l.ArtistName,
                            SortName      = result.SortName ?? l.ArtistSortName,
                            ArtistType    = result.ArtistType ?? l.ArtistType
                        });
                    }

                    if (lastFmResult.Errors != null)
                    {
                        resultsExceptions.AddRange(lastFmResult.Errors);
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: LastFmArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "LastFMArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (SpotifyArtistSearchEngine.IsEnabled)
                {
                    var sw2           = Stopwatch.StartNew();
                    var spotifyResult = await SpotifyArtistSearchEngine.PerformArtistSearch(result.Name, 1).ConfigureAwait(false);

                    if (spotifyResult.IsSuccess)
                    {
                        var s = spotifyResult.Data.First();
                        if (s.Tags != null)
                        {
                            result.Tags = result.Tags.AddToDelimitedList(s.Tags);
                        }
                        if (s.Urls != null)
                        {
                            result.URLs = result.URLs.AddToDelimitedList(s.Urls);
                        }
                        if (s.ImageUrls != null)
                        {
                            artistImageUrls.AddRange(s.ImageUrls);
                        }
                        if (s.ArtistGenres != null)
                        {
                            artistGenres.AddRange(s.ArtistGenres);
                        }
                        if (!string.IsNullOrEmpty(s.ArtistName) &&
                            !s.ArtistName.Equals(result.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            result.AlternateNames.AddToDelimitedList(new[] { s.ArtistName });
                        }
                        result.CopyTo(new Artist
                        {
                            EndDate       = s.EndDate,
                            BioContext    = s.Bio,
                            Profile       = HttpEncoder.HtmlEncode(s.Profile),
                            MusicBrainzId = s.MusicBrainzId,
                            SpotifyId     = s.SpotifyId,
                            BeginDate     = s.BeginDate,
                            Name          = result.Name ?? s.ArtistName,
                            SortName      = result.SortName ?? s.ArtistSortName,
                            ArtistType    = result.ArtistType ?? s.ArtistType
                        });
                    }

                    if (spotifyResult.Errors != null)
                    {
                        resultsExceptions.AddRange(spotifyResult.Errors);
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: SpotifyArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "SpotifyArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (DiscogsArtistSearchEngine.IsEnabled)
                {
                    var sw2           = Stopwatch.StartNew();
                    var discogsResult = await DiscogsArtistSearchEngine.PerformArtistSearch(result.Name, 1).ConfigureAwait(false);

                    if (discogsResult.IsSuccess)
                    {
                        var d = discogsResult?.Data?.FirstOrDefault();
                        if (d != null)
                        {
                            if (d.Urls != null)
                            {
                                result.URLs = result.URLs.AddToDelimitedList(d.Urls);
                            }
                            if (d.ImageUrls != null)
                            {
                                artistImageUrls.AddRange(d.ImageUrls);
                            }
                            if (d.AlternateNames != null)
                            {
                                result.AlternateNames = result.AlternateNames.AddToDelimitedList(d.AlternateNames);
                            }
                            if (!string.IsNullOrEmpty(d.ArtistName) &&
                                !d.ArtistName.Equals(result.Name, StringComparison.OrdinalIgnoreCase))
                            {
                                result.AlternateNames.AddToDelimitedList(new[] { d.ArtistName });
                            }
                            result.CopyTo(new Artist
                            {
                                Profile    = HttpEncoder.HtmlEncode(d.Profile),
                                DiscogsId  = d.DiscogsId,
                                Name       = result.Name ?? d.ArtistName,
                                RealName   = result.RealName ?? d.ArtistRealName,
                                ArtistType = result.ArtistType ?? d.ArtistType
                            });
                        }
                    }
                    if (discogsResult.Errors != null)
                    {
                        resultsExceptions.AddRange(discogsResult.Errors);
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: DiscogsArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "DiscogsArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (WikipediaArtistSearchEngine.IsEnabled)
                {
                    var sw2      = Stopwatch.StartNew();
                    var wikiName = result?.Name;
                    // Help get better results for bands with proper nouns (e.g. "Poison" vs "Poison Band")
                    if (!(result?.ArtistType ?? string.Empty).Equals("Person", StringComparison.OrdinalIgnoreCase))
                    {
                        wikiName += " band";
                    }
                    var wikipediaResult = await WikipediaArtistSearchEngine.PerformArtistSearch(wikiName, 1).ConfigureAwait(false);

                    if (wikipediaResult?.Data != null)
                    {
                        if (wikipediaResult.IsSuccess)
                        {
                            var w = wikipediaResult?.Data?.FirstOrDefault();
                            if (!string.IsNullOrEmpty(w?.Bio))
                            {
                                result.CopyTo(new Artist
                                {
                                    BioContext = HttpEncoder.HtmlEncode(w.Bio)
                                });
                            }
                        }

                        if (wikipediaResult.Errors != null)
                        {
                            resultsExceptions.AddRange(wikipediaResult.Errors);
                        }
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: WikipediaArtistSearchEngine Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "WikipediaArtistSearch: " + ex.Serialize());
            }

            try
            {
                if (result.AlternateNames != null)
                {
                    result.AlternateNames = string.Join("|", result.AlternateNames.ToListFromDelimited().Distinct().OrderBy(x => x));
                }
                if (result.URLs != null)
                {
                    result.URLs = string.Join("|", result.URLs.ToListFromDelimited().Distinct().OrderBy(x => x));
                }
                if (result.Tags != null)
                {
                    result.Tags = string.Join("|", result.Tags.ToListFromDelimited().Distinct().OrderBy(x => x));
                }
                if (artistGenres.Count > 0)
                {
                    var sw2        = Stopwatch.StartNew();
                    var genreInfos = from ag in artistGenres
                                     join g in DbContext.Genres on ag equals g.Name into gg
                                     from g in gg.DefaultIfEmpty()
                                     select new
                    {
                        newGenre      = ag.ToTitleCase(),
                        existingGenre = g
                    };
                    result.Genres = new List <ArtistGenre>();
                    foreach (var genreInfo in genreInfos)
                    {
                        var ag = new ArtistGenre
                        {
                            Genre = genreInfo.existingGenre ?? new Genre
                            {
                                Name           = genreInfo.newGenre,
                                NormalizedName = genreInfo.newGenre.ToAlphanumericName()
                            }
                        };
                        if (!result.Genres.Any(x => x.Genre.NormalizedName == ag.Genre.NormalizedName))
                        {
                            result.Genres.Add(ag);
                        }
                    }
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: Artist Genre Processing Complete [{ sw2.ElapsedMilliseconds }]");
                }

                if (artistImageUrls.Count > 0)
                {
                    var sw2      = Stopwatch.StartNew();
                    var imageBag = new ConcurrentBag <IImage>();
                    var i        = artistImageUrls.Select(async url => imageBag.Add(await WebHelper.GetImageFromUrlAsync(url).ConfigureAwait(false)));
                    await Task.WhenAll(i).ConfigureAwait(false);

                    result.Images = imageBag.Where(x => x?.Bytes != null)
                                    .GroupBy(x => x.Signature)
                                    .Select(x => x.First())
                                    .Take(Configuration.Processing.MaximumArtistImagesToAdd)
                                    .ToList();
                    sw2.Stop();
                    Logger.LogTrace($"PerformMetaDataProvidersArtistSearch: Image Url Processing Complete [{ sw2.ElapsedMilliseconds }]");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "CombiningResults: " + ex.Serialize());
            }

            result.SortName = result.SortName.ToTitleCase();
            if (!string.IsNullOrEmpty(result.ArtistType))
            {
                switch (result.ArtistType.ToLower().Replace('-', ' '))
                {
                case "artist":
                case "artists":
                case "one man band":
                case "one woman band":
                case "solo":
                case "person":
                    result.ArtistType = "Person";
                    break;

                case "band":
                case "big band":
                case "duet":
                case "jug band":
                case "quartet":
                case "quartette":
                case "sextet":
                case "trio":
                case "group":
                    result.ArtistType = "Group";
                    break;

                case "orchestra":
                    result.ArtistType = "Orchestra";
                    break;

                case "choir band":
                case "choir":
                    result.ArtistType = "Choir";
                    break;

                case "movie part":
                case "movie role":
                case "role":
                case "character":
                    result.ArtistType = "Character";
                    break;

                default:
                    Logger.LogWarning(string.Format("Unknown Artist Type [{0}]", result.ArtistType));
                    result.ArtistType = "Other";
                    break;
                }
            }

            sw.Stop();
            return(new OperationResult <Artist>
            {
                Data = result,
                IsSuccess = result != null,
                Errors = resultsExceptions,
                OperationTime = sw.ElapsedMilliseconds
            });
        }
コード例 #53
0
 public KetamaLocator(string hashName = null, int resurectFreq = 1000)
 {
     _hashAlgoPool  = new ConcurrentBag <HashAlgorithm>();
     _hashFactory   = () => HashAlgorithm.Create(hashName = hashName ?? DefaultHashName);
     _resurrectFreq = resurectFreq;
 }
コード例 #54
0
        private async Task <bool> CompleteTaskAsync(Task <JObject> task, ReferenceCache cache, ConcurrentBag <CatalogEntry> entries)
        {
            var json = await task;

            foreach (var item in json["items"])
            {
                // Store the url in pieces so it can be cached.
                // Split on /
                var urlParts = item["@id"]
                               .ToObject <string>().Split('/')
                               .Select(s => cache.GetString(s))
                               .ToArray();

                var entry = new CatalogEntry(
                    urlParts,
                    cache.GetString(item["@type"].ToObject <string>()),
                    cache.GetString(item["commitId"].ToObject <string>()),
                    cache.GetDate(item["commitTimeStamp"].ToObject <string>()),
                    cache.GetString(item["nuget:id"].ToObject <string>()),
                    cache.GetVersion(item["nuget:version"].ToObject <string>()),
                    _serviceIndex,
                    GetJson,
                    GetNuspec,
                    GetNupkg);

                entries.Add(entry);
            }

            return(true);
        }
コード例 #55
0
 public static void AddRange <T>(this ConcurrentBag <T> bag, IEnumerable <T> list)
 {
     Parallel.ForEach(list, element =>
                      { bag.Add(element); });
 }
コード例 #56
0
        private async Task SyncClanUsersAsync()
        {
            _logger.LogInformation($"{DateTime.Now} Syncing Users");

            using var scope = _scopeFactory.CreateScope();

            var apiClient = scope.ServiceProvider.GetRequiredService <BungieApiClient>();

            var membersTasks = _clanIDs.Select(x => apiClient.Api.GroupV2_GetMembersOfGroup(0, x));

            var activitiesDB = scope.ServiceProvider.GetRequiredService <IClanActivitiesDB>();

            var dbUsers = await activitiesDB.GetUsersWithCharactersAsync();

            var dbUsersDict = dbUsers.ToDictionary(x => x.UserID, x => x);

            await Task.WhenAll(membersTasks);

            var groupMembers     = membersTasks.SelectMany(x => x.Result.Results);
            var groupMembersDict = groupMembers.ToDictionary(x => x.DestinyUserInfo.MembershipId, x => x);

            var usersToDelete = dbUsersDict.Where(x => !groupMembersDict.ContainsKey(x.Key)).Select(x => x.Value);
            var usersToUpdate = new ConcurrentBag <User>();
            var usersToAdd    = new ConcurrentBag <User>();

            var components = new DestinyComponentType[]
            {
                DestinyComponentType.Profiles,
                DestinyComponentType.Characters
            };

            var chunks = groupMembersDict.Chunk(groupMembersDict.Count() / 8 + 1);

            var tasks = chunks.Select(x => Task.Run(async() =>
            {
                foreach (var member in x)
                {
                    var profile = await apiClient.Api.Destiny2_GetProfile(member.Key, member.Value.DestinyUserInfo.MembershipType, components);

                    var dbUser = dbUsersDict.GetValueOrDefault(member.Key);

                    if (dbUser is null)
                    {
                        usersToAdd.Add(new User
                        {
                            UserID         = member.Key,
                            ClanID         = member.Value.GroupId,
                            ClanJoinDate   = member.Value.JoinDate,
                            UserName       = $"{member.Value.DestinyUserInfo.BungieGlobalDisplayName}#{member.Value.DestinyUserInfo.BungieGlobalDisplayNameCode}",
                            MembershipType = (int)member.Value.DestinyUserInfo.MembershipType,
                            DateLastPlayed = profile.Profile.Data.DateLastPlayed,
                            Characters     = profile.Characters.Data
                                             .Select(y => new Character
                            {
                                UserID         = member.Key,
                                CharacterID    = y.Key,
                                DateLastPlayed = y.Value.DateLastPlayed,
                                Class          = (int)y.Value.ClassType,
                                Race           = (int)y.Value.RaceType,
                                Gender         = (int)y.Value.GenderType
                            }).ToList()
                        });
                    }
                    else if (dbUser.DateLastPlayed < profile.Profile.Data.DateLastPlayed)
                    {
                        var profileChars = profile.Characters.Data;

                        foreach (var diffChr in dbUser.Characters.Where(y => !profileChars.ContainsKey(y.CharacterID)))
                        {
                            dbUser.Characters.Remove(diffChr);
                        }

                        foreach (var chr in profileChars)
                        {
                            var dbChr = dbUser.Characters.FirstOrDefault(y => y.CharacterID == chr.Key);

                            if (dbChr is null)
                            {
                                dbUser.Characters.Add(new Character
                                {
                                    UserID         = member.Key,
                                    CharacterID    = chr.Key,
                                    DateLastPlayed = chr.Value.DateLastPlayed,
                                    Class          = (int)chr.Value.ClassType,
                                    Race           = (int)chr.Value.RaceType,
                                    Gender         = (int)chr.Value.GenderType
                                });
                            }
                            else if (dbChr.DateLastPlayed < chr.Value.DateLastPlayed)
                            {
                                dbChr.DateLastPlayed = chr.Value.DateLastPlayed;
                                dbChr.Class          = (int)chr.Value.ClassType;
                                dbChr.Race           = (int)chr.Value.RaceType;
                                dbChr.Gender         = (int)chr.Value.GenderType;
                            }
                        }

                        dbUser.ClanID         = member.Value.GroupId;
                        dbUser.UserName       = $"{member.Value.DestinyUserInfo.BungieGlobalDisplayName}#{member.Value.DestinyUserInfo.BungieGlobalDisplayNameCode}";
                        dbUser.MembershipType = (int)member.Value.DestinyUserInfo.MembershipType;
                        dbUser.DateLastPlayed = profile.Profile.Data.DateLastPlayed;

                        usersToUpdate.Add(dbUser);
                    }
                }
            }));

            await Task.WhenAll(tasks);

            await activitiesDB.SyncUsersAsync(usersToDelete, usersToUpdate, usersToAdd);

            _logger.LogInformation($"{DateTime.Now} Users synced");
        }
コード例 #57
0
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            if (context.Command is BulkUpdateAssets bulkUpdates)
            {
                if (bulkUpdates.Jobs?.Length > 0)
                {
                    var executionOptions = new ExecutionDataflowBlockOptions
                    {
                        MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount / 2)
                    };

                    var createCommandsBlock = new TransformBlock <BulkTask, BulkTaskCommand?>(task =>
                    {
                        return(CreateCommand(task));
                    }, executionOptions);

                    var executeCommandBlock = new ActionBlock <BulkTaskCommand?>(async command =>
                    {
                        if (command != null)
                        {
                            await ExecuteCommandAsync(command);
                        }
                    }, executionOptions);

                    createCommandsBlock.LinkTo(executeCommandBlock, new DataflowLinkOptions
                    {
                        PropagateCompletion = true
                    });

                    contextProvider.Context.Change(b => b
                                                   .WithoutAssetEnrichment()
                                                   .WithoutCleanup()
                                                   .WithUnpublished(true)
                                                   .WithoutTotal());

                    var results = new ConcurrentBag <BulkUpdateResultItem>();

                    for (var i = 0; i < bulkUpdates.Jobs.Length; i++)
                    {
                        var task = new BulkTask(
                            context.CommandBus,
                            i,
                            bulkUpdates.Jobs[i],
                            bulkUpdates,
                            results);

                        await createCommandsBlock.SendAsync(task);
                    }

                    createCommandsBlock.Complete();

                    await executeCommandBlock.Completion;

                    context.Complete(new BulkUpdateResult(results));
                }
                else
                {
                    context.Complete(new BulkUpdateResult());
                }
            }
            else
            {
                await next(context);
            }
        }
コード例 #58
0
        public void StartImport()
        {
            setCurrentAction("Preparing to import");
            FailedTickets = new ConcurrentBag <IFailedTicket>();
            passedTickets = new ConcurrentBag <Ticket>();
            var okToImport = ticketTarget.StartImport(ticketSource.Source);

            if (okToImport)
            {
                ticketSource.PreferHtml = ticketTarget.SupportsHtml;

                setCurrentAction(String.Format("Validating {0} tickets against {1}", ticketSource.Source, ticketTarget.Target));
                Parallel.ForEach(ticketSource.Tickets(ticketTarget.GetAvailableTicketTypes()),
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                },
                                 sourceTicket =>
                {
                    IFailedTicket failedTicket;
                    if (ticketTarget.CheckTicket(sourceTicket, out failedTicket))
                    {
                        passedTickets.Add(sourceTicket);
                    }
                    else
                    {
                        FailedTickets.Add(failedTicket);
                    }
                });

                if (FailedTickets.Count == 0)
                {
                    setCurrentAction(String.Format("Creating {0} tickets", ticketTarget.Target));
                    var progressNotifer = new ProgressNotifier(onPercentComplete, passedTickets.Count);
                    Parallel.ForEach(passedTickets,
                                     new ParallelOptions {
                        MaxDegreeOfParallelism = Environment.ProcessorCount
                    },
                                     passedTicket =>
                    {
                        if (includeAttachments)
                        {
                            var ticketDownloadFolder = makeDownloadFolder(passedTicket.ID);
                            ticketSource.DownloadAttachments(passedTicket, ticketDownloadFolder);
                        }
                        else
                        {
                            passedTicket.Attachments.Clear();
                        }
                        ticketTarget.AddTicket(passedTicket);
                        progressNotifer.UpdateProgress();
                    });

                    setCurrentAction(String.Format("Updating {0} tickets", ticketTarget.Target));
                    ticketTarget.EndImport();

                    if (includeAttachments)
                    {
                        deleteFolder(new DirectoryInfo(downloadFolder));
                    }

                    setCurrentAction("Import complete.");
                }
            }
        }
コード例 #59
0
        public void Parallel_Test1_1()
        {
            ConcurrentBag <string> hexs = new ConcurrentBag <string>();

            Parallel.For(1, 20, new ParallelOptions
            {
                MaxDegreeOfParallelism = 5
            }, (i) =>
            {
                JT808Package jT808Package = new JT808Package();
                jT808Package.Header       = new JT808Header
                {
                    MsgId           = Enums.JT808MsgId.位置信息汇报.ToUInt16Value(),
                    MsgNum          = (ushort)i,
                    TerminalPhoneNo = "1122334455" + i.ToString(),
                };
                JT808_0x0200 jT808UploadLocationRequest = new JT808_0x0200
                {
                    AlarmFlag  = 1,
                    Altitude   = 40,
                    GPSTime    = DateTime.Parse("2018-07-15 10:10:10").AddSeconds(i),
                    Lat        = 12222222,
                    Lng        = 132444444,
                    Speed      = 60,
                    Direction  = 0,
                    StatusFlag = 2,
                    JT808LocationAttachData = new Dictionary <byte, JT808_0x0200_BodyBase>()
                };
                jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x01, new JT808_0x0200_0x01
                {
                    Mileage = 100
                });
                jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x02, new JT808_0x0200_0x02
                {
                    Oil = 55
                });
                jT808Package.Bodies = jT808UploadLocationRequest;
                var hex             = JT808Serializer.Serialize(jT808Package).ToHexString();
                hexs.Add(hex);
            });
            //7E02000026 112233445519 0013 000000010000000200BA7F0E07E4F11C0028003C0000 180715101029 01040000006402020037987E,
            //7E02000026 112233445518 0012 000000010000000200BA7F0E07E4F11C0028003C0000 180715101028 01040000006402020037997E,
            //7E02000026 112233445517 0011 000000010000000200BA7F0E07E4F11C0028003C0000 180715101027 010400000064020200379A7E,
            //7E02000026 112233445516 0010 000000010000000200BA7F0E07E4F11C0028003C0000 180715101026 010400000064020200379B7E,
            //7E02000026 112233445515 000F 000000010000000200BA7F0E07E4F11C0028003C0000 180715101025 01040000006402020037847E,
            //7E02000026 112233445514 000E 000000010000000200BA7F0E07E4F11C0028003C0000 180715101024 01040000006402020037857E,
            //7E02000026 112233445513 000D 000000010000000200BA7F0E07E4F11C0028003C0000 180715101023 01040000006402020037867E,
            //7E02000026 112233445512 000C 000000010000000200BA7F0E07E4F11C0028003C0000 180715101022 01040000006402020037877E,
            //7E02000026 112233445511 000B 000000010000000200BA7F0E07E4F11C0028003C0000 180715101021 01040000006402020037807E,
            //7E02000026 112233445510 000A 000000010000000200BA7F0E07E4F11C0028003C0000 180715101020 01040000006402020037817E,
            //7E02000026 011223344559 0009 000000010000000200BA7F0E07E4F11C0028003C0000 180715101019 01040000006402020037A27E,
            //7E02000026 011223344558 0008 000000010000000200BA7F0E07E4F11C0028003C0000 180715101018 01040000006402020037A37E,
            //7E02000026 011223344557 0007 000000010000000200BA7F0E07E4F11C0028003C0000 180715101017 01040000006402020037AC7E,
            //7E02000026 011223344556 0006 000000010000000200BA7F0E07E4F11C0028003C0000 180715101016 01040000006402020037AD7E,
            //7E02000026 011223344555 0005 000000010000000200BA7F0E07E4F11C0028003C0000 180715101015 01040000006402020037AE7E,
            //7E02000026 011223344554 0004 000000010000000200BA7F0E07E4F11C0028003C0000 180715101014 01040000006402020037AF7E,
            //7E02000026 011223344553 0003 000000010000000200BA7F0E07E4F11C0028003C0000 180715101013 01040000006402020037A87E,
            //7E02000026 011223344552 0002 000000010000000200BA7F0E07E4F11C0028003C0000 180715101012 01040000006402020037A97E,
            //7E02000026 011223344551 0001 000000010000000200BA7F0E07E4F11C0028003C0000 180715101011 01040000006402020037AA7E,
            //7E02000026 011223344550 0001 000000010000000200BA7F0E07E4F11C0028003C0000 180715101010 01040000006402020037AA7E
            //7E02000026 011223344550 0000 000000010000000200BA7F0E07E4F11C0028003C0000 180715101010 01040000006402020037AB7E,
            string result = "7E020000260112233445500000000000010000000200BA7F0E07E4F11C0028003C000018071510101001040000006402020037AB7E,7E020000260112233445570007000000010000000200BA7F0E07E4F11C0028003C000018071510101701040000006402020037AC7E,7E020000260112233445560006000000010000000200BA7F0E07E4F11C0028003C000018071510101601040000006402020037AD7E,7E020000260112233445550005000000010000000200BA7F0E07E4F11C0028003C000018071510101501040000006402020037AE7E,7E020000260112233445540004000000010000000200BA7F0E07E4F11C0028003C000018071510101401040000006402020037AF7E,7E020000261122334455190013000000010000000200BA7F0E07E4F11C0028003C000018071510102901040000006402020037987E,7E020000261122334455180012000000010000000200BA7F0E07E4F11C0028003C000018071510102801040000006402020037997E,7E020000261122334455170011000000010000000200BA7F0E07E4F11C0028003C0000180715101027010400000064020200379A7E,7E020000261122334455160010000000010000000200BA7F0E07E4F11C0028003C0000180715101026010400000064020200379B7E,7E02000026112233445515000F000000010000000200BA7F0E07E4F11C0028003C000018071510102501040000006402020037847E,7E02000026112233445514000E000000010000000200BA7F0E07E4F11C0028003C000018071510102401040000006402020037857E,7E02000026112233445513000D000000010000000200BA7F0E07E4F11C0028003C000018071510102301040000006402020037867E,7E02000026112233445512000C000000010000000200BA7F0E07E4F11C0028003C000018071510102201040000006402020037877E,7E020000260112233445530003000000010000000200BA7F0E07E4F11C0028003C000018071510101301040000006402020037A87E,7E020000260112233445520002000000010000000200BA7F0E07E4F11C0028003C000018071510101201040000006402020037A97E,7E020000260112233445510001000000010000000200BA7F0E07E4F11C0028003C000018071510101101040000006402020037AA7E,7E02000026112233445511000B000000010000000200BA7F0E07E4F11C0028003C000018071510102101040000006402020037807E,7E02000026112233445510000A000000010000000200BA7F0E07E4F11C0028003C000018071510102001040000006402020037817E,7E020000260112233445590009000000010000000200BA7F0E07E4F11C0028003C000018071510101901040000006402020037A27E,7E020000260112233445580008000000010000000200BA7F0E07E4F11C0028003C000018071510101801040000006402020037A37E";
            //7E020000260112233445500001000000010000000200BA7F0E07E4F11C0028003C000018071510101001040000006402020037AA7E
            List <string> resultHexs = result.Split(',').ToList();
            string        hexStr     = string.Join(',', hexs);

            foreach (var item in hexs)
            {
                Assert.Contains(item, resultHexs);
            }
        }
コード例 #60
0
 /// <summary>
 /// This constructor takes a collection of errors occurred during construction
 /// time.
 /// </summary>
 /// <param name="loadErrors">
 /// The errors that occured
 /// </param>
 internal FormatTableLoadException(ConcurrentBag <string> loadErrors) :
     base(StringUtil.Format(FormatAndOutXmlLoadingStrings.FormatTableLoadErrors))
 {
     _errors = new Collection <string>(loadErrors.ToArray());
     SetDefaultErrorRecord();
 }