Example #1
1
        public static List<Builder> LoadBuildersWithTasks(int numberOfBuilders)
        {
            BlockingCollection<Builder> buildersToLoad = new BlockingCollection<Builder>();
            BlockingCollection<Builder> loadedBuilders = new BlockingCollection<Builder>();

            for (int i = 0; i < numberOfBuilders; i++)
            {
                buildersToLoad.Add(new Builder { Name = "Builder" + i, Status = "Status" + i });
            }
            buildersToLoad.CompleteAdding();

            Task loader1 = Task.Factory.StartNew(() =>
                {
                    foreach (Builder item in buildersToLoad.GetConsumingEnumerable())
                    {
                        Thread.Sleep(1000);
                        loadedBuilders.Add(item);
                    }
                }, TaskCreationOptions.LongRunning);

            Task loader2 = Task.Factory.StartNew(() =>
            {
                foreach (Builder item in buildersToLoad.GetConsumingEnumerable())
                {
                    Thread.Sleep(1000);
                    loadedBuilders.Add(item);
                }
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(loader1, loader2);
            return loadedBuilders.ToList();
        }
Example #2
0
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.Take()
    //      BlockingCollection<T>.CompleteAdding()
    public static void BC_AddTakeCompleteAdding()
    {
        using (BlockingCollection<int> bc = new BlockingCollection<int>()) {

            // Spin up a Task to populate the BlockingCollection
            using (Task t1 = Task.Factory.StartNew(() => {
                bc.Add(1);
                bc.Add(2);
                bc.Add(3);
                bc.CompleteAdding();
            })) {

                // Spin up a Task to consume the BlockingCollection
                using (Task t2 = Task.Factory.StartNew(() => {
                    try {
                        // Consume the BlockingCollection
                        while (true) Console.WriteLine(bc.Take());
                    }
                    catch (InvalidOperationException) {
                        // An InvalidOperationException means that Take() was called on a completed collection
                        Console.WriteLine("That's All!");
                    }
                }))

                Task.WaitAll(t1, t2);
            }
        }
    }
Example #3
0
        public void when_consuming_enumerable_then_succeeds()
        {
            var bytes = new BlockingCollection<byte>();

            var incoming = bytes.GetConsumingEnumerable().ToObservable(TaskPoolScheduler.Default);

            var messages = from header in incoming.Buffer(4)
                           let length = BitConverter.ToInt32(header.ToArray(), 0)
                           let body = incoming.Take(length)
                           select Encoding.UTF8.GetString(body.ToEnumerable().ToArray());

            messages.Subscribe(s => Console.Write(s));

            var message = "hello";

            BitConverter.GetBytes(message.Length).Concat(Encoding.UTF8.GetBytes(message)).ToList().ForEach(b => bytes.Add(b));

            message = "world";

            BitConverter.GetBytes(message.Length).Concat(Encoding.UTF8.GetBytes(message)).ToList().ForEach(b => bytes.Add(b));

            Thread.Sleep(2000);

            Console.WriteLine(bytes.Count);
        }
Example #4
0
 public void NotifyBlock(string poolId, long blockHeight)
 {
     queue?.Add(new QueuedNotification
     {
         Category = NotificationCategory.Block,
         PoolId   = poolId,
         Subject  = "Block Notification",
         Msg      = $"Pool {poolId} found block candidate {blockHeight}"
     });
 }
        /// <param name="autoLoadHeadDeploymentIntervalMs">AutoLoad is disabled if set to Timeout.Infinite</param>
        public Host(IHostContext context, int autoLoadHeadDeploymentIntervalMs = 30000)
        {
            _hostContext = context;
            _cells = new Dictionary<string, Cell>();
            _commandQueue = new BlockingCollection<IHostCommand>();

            _autoLoadHeadDeploymentIntervalMs = autoLoadHeadDeploymentIntervalMs;
            _autoLoadHeadDeploymentTimer = new Timer(o => _commandQueue.Add(new LoadCurrentHeadDeploymentCommand()), null, Timeout.Infinite, _autoLoadHeadDeploymentIntervalMs);
            _watchdogTimer = new Timer(o => _commandQueue.Add(new EnsureAllCellsAreRunningUnlessCancelledCommand()), null, Timeout.Infinite, _watchdogIntervalMs);
        }
 public void NotifyPaymentSuccess(string poolId, decimal amount, int recpientsCount, string txInfo, decimal?txFee)
 {
     queue?.Add(new QueuedNotification
     {
         Category = NotificationCategory.PaymentSuccess,
         PoolId   = poolId,
         Subject  = "Payout Success Notification",
         Msg      = $"Paid {FormatAmount(amount, poolId)} from pool {poolId} to {recpientsCount} recipients in Transaction(s) {txInfo}."
     });
 }
Example #7
0
 public TerminalTester(TimeSpan globalTimeout, string prompt = @"/ # ")
 {
     timeoutSync = new object();
     eventCollection = new BlockingCollection<Event>(new ConcurrentQueue<Event>());
     reportCollection = new ConcurrentQueue<Event>();
     reportEndingLock = new object();
     reportCollection.Enqueue(new ReportSeparator());
     terminal = new PromptTerminal(x => eventCollection.Add(new Line { Content = x }), () => eventCollection.Add( new Prompt() ), prompt);
     defaultPrompt = prompt;
     this.globalTimeout = globalTimeout;
 }
        static void FileReader(BlockingCollection<int> input, BlockingCollection<string> output)
        {
            StreamReader file = new StreamReader("input.txt"); // TODO: check exceptions
            string line;
            while ((line = file.ReadLine()) != null)
            {
                output.Add(line);

            }
            output.Add(null); // EOF
            Console.WriteLine("line count: " + input.Take());
        }
Example #9
0
        /// <summary>
        /// Process domain MX query, start new tasks for each of received MX entry
        /// </summary>
        /// <param name="domainMx"></param>
        /// <returns></returns>
        private async Task ProcessDomainAsync(DomainMx domainMx)
        {
            if (_lookup == null)
            {
                return;
            }
            int mxCount      = 0;
            var wasException = false;

            try
            {
                var result = await _lookup.QueryAsync(domainMx.Domain, QueryType.MX, QueryClass.IN, _cancellation.Token);

                domainMx.Error = result.HasError ? result.ErrorMessage : null;
                // result.Answers.Count may be different than real MX records count in the case of redirection
                var mxs = result.Answers.MxRecords();
                mxCount = mxs.Count();
                if (mxCount != result.Answers.Count)
                {
                    domainMx.Error =
                        $@"Bad number ({mxCount}\{result.Answers.Count}) of received MX records. Probably redirects occurred.";
                }
                domainMx.mxCount = mxCount;
                domainMx.MxArray = new DomainMx.MxEntry[mxCount];
                if (Settings.Sort)
                {
                    mxs = mxs.OrderBy(mx => mx.Preference);
                }
                var i = 0;
                foreach (var mx in mxs)
                {
                    var entry = new DomainMx.MxEntry(domainMx, mx.Exchange, mx.Preference);
                    ProcessExchange(entry);
                    domainMx.MxArray[i++] = entry;
                }
            }
            catch (Exception e)
            {
                domainMx.Error = e.Message;
                wasException   = true;
            }
            finally
            {
                // log info about Domain even without MX (for reporting purposes)
                if (mxCount == 0 || wasException)
                {
                    _dataStorage?.Add(domainMx);
                }
            }
        }
        public static IEnumerable<CycleCompletedArgs> GetCycles(this RemoteViewClient client, Action<RemoteViewClient> attachAction)
        {
            using (var resultQueue = new BlockingCollection<object>(new ConcurrentQueue<object>()))
            using (var otherQueue = new BlockingCollection<object>(new ConcurrentQueue<object>()))
            {
                var resultListener = new EventViewResultListener();
                resultListener.CycleCompleted += (sender, e) => resultQueue.Add(e);

                resultListener.CycleExecutionFailed += (s, e) => otherQueue.Add(e);
                resultListener.ProcessTerminated += (s, e) => otherQueue.Add(e);
                resultListener.ViewDefinitionCompilationFailed += (s, e) => otherQueue.Add(e);

                client.SetResultListener(resultListener);

                attachAction(client);

                TimeSpan timeout = TimeSpan.FromMinutes(1);

                try
                {
                    while (true)
                    {
                        object next;
                        var index = BlockingCollection<object>.TryTakeFromAny(new[] { resultQueue, otherQueue }, out next, timeout);
                        if (index == 0)
                        {
                            yield return (CycleCompletedArgs)next;
                        }
                        else
                        {
                            var detailMessage = string.Format("for {0} after {1}\n state {2} is completed {3}", client.GetViewDefinition().Name, timeout, client.GetState(), client.IsCompleted);
                            switch (index)
                            {
                                case 0:
                                    throw new ArgumentException("index");
                                case 1:
                                    throw new Exception(string.Format("Error occured whilst getting results {0}\n{1}", next, detailMessage));
                                default:

                                    throw new TimeoutException("No results received " + detailMessage);
                            }
                        }
                    }
                }
                finally
                {
                    client.RemoveResultListener();
                }
            }
        }
Example #11
0
        private void MsgWindow_ClipboardContentChanged(object sender, EventArgs e)
        {
            string text = WinClipboard.ReadText();

            if (text == null)
            {
                return;
            }

            ISLogger.Write("INPUTMANAGER->OnClipboardCopied");
            inputQueue?.Add(new Win32Msg {
                wParam = INPUTSHARE_CLIPBOARDTEXTCOPY, cbText = text
            });
        }
	public static void Main ()
	{
		int gcCount = 0;
		int joinCount = 0;
		targetTime = DateTime.UtcNow.AddSeconds(30);

		Thread gcThread = new Thread (() => {
			while (!finished()) {
				GC.Collect ();
				gcCount++;
				Thread.Sleep (1);
			}
		});

		gcThread.Start ();

		// Create threads then join them for 30 seconds nonstop while GCs occur once per ms
		while (!finished()) {
			BlockingCollection<Thread> threads = new BlockingCollection<Thread> (new ConcurrentQueue<Thread> (), 128);

			Thread joinThread = new Thread (() => {
				for (int i = 0; ; ++i) {
					Thread t = threads.Take ();
					if (t == null)
						break;
					t.Join ();

					// Uncomment this and run with MONO_LOG_LEVEL=info MONO_LOG_MASK=gc
					// to see GC/join balance in real time
					//Console.Write ("*");
				}
			});
			joinThread.Start ();
			
			const int makeThreads = 10*1000;
			for (int i = 0; i < makeThreads; ++i) {
				Thread t = new Thread (() => { Thread.Yield (); });
				t.Start ();

				threads.Add (t);
			}

			threads.Add (null);
			joinThread.Join ();

			joinCount += makeThreads;
			Console.WriteLine("Performed {0} GCs, created {1} threads. Finished? {2}", gcCount, joinCount, finished());
		}
		gcThread.Join ();
	}
Example #13
0
        public void DetachedParentChild()
        {
            //Arrange
              var sequence = new BlockingCollection<int>();
              var child1 = new Action(() => { sequence.Add(2); Thread.Sleep(5000); sequence.Add(4); });
              var parent = new Action(() => { sequence.Add(1); Task.Factory.StartNew(child1); Thread.Sleep(2500); sequence.Add(3); });

              //Act
              var parent_task = Task.Factory.StartNew(parent);
              parent_task.Wait();

              //Assert
              Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 1, 2, 3 }, sequence), sequence.Aggregate(new StringBuilder(), (whole, next) => whole.AppendFormat("{0}|", next)).ToString());
              CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, sequence.ToList());
        }
        public void TestCollectionContainsElement2()
        {
            string item = "abc";
            BlockingCollection<string> collection = new BlockingCollection<string>();
            collection.Add("cat");
            collection.Add(item);
            collection.Add("dog");

            Assert.Equal(item, collection.Take(item));

            // Remove remaining items, check that item is not there
            Assert.NotEqual(item, collection.Take());
            Assert.NotEqual(item, collection.Take());
            Assert.Equal(0, collection.Count);
        }
Example #15
0
 public void Send(IrcMessage payload)
 {
     if (!_runCts.IsCancellationRequested)
     {
         _sendQueue?.Add(payload);
     }
 }
        public void TestStreamingTransportServer()
        {
            BlockingCollection<string> queue = new BlockingCollection<string>();
            List<string> events = new List<string>();
            IStreamingCodec<string> stringCodec = _injector.GetInstance<StringStreamingCodec>();

            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
            var remoteHandler = Observer.Create<TransportEvent<string>>(tEvent => queue.Add(tEvent.Data));

            using (var server = new StreamingTransportServer<string>(endpoint.Address, remoteHandler, _tcpPortProvider, stringCodec))
            {
                server.Run();

                IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.LocalEndpoint.Port);
                using (var client = new StreamingTransportClient<string>(remoteEndpoint, stringCodec))
                {
                    client.Send("Hello");
                    client.Send(", ");
                    client.Send("World!");

                    events.Add(queue.Take());
                    events.Add(queue.Take());
                    events.Add(queue.Take());
                } 
            }

            Assert.Equal(3, events.Count);
            Assert.Equal(events[0], "Hello");
            Assert.Equal(events[1], ", ");
            Assert.Equal(events[2], "World!");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("=== In Line Execution ===");

            Task<string>[] tasks = GetTasks();

            string[] all = Task.WhenAll(tasks).Result;

            Console.WriteLine(string.Join("", all));

            Console.WriteLine("=== Standard Live Execution ===");

            tasks = GetTasks();

            BlockingCollection<string> results = new BlockingCollection<string>();

            Task.WaitAll(tasks.Select(async t =>
                {
                    results.Add(await t);
                    Console.WriteLine("[{0:hh:mm:ss.fff}] Current result: [{1}]", DateTime.Now, string.Join("", results));
                }).ToArray());

            Console.WriteLine("=== Pragmateek Live Execution ===");

            results = new BlockingCollection<string>();

            tasks = GetTasks();

            Task.WaitAll(tasks.Select(t => t.ContinueWith(p =>
                {
                    results.Add(p.Result);
                    Console.WriteLine("[{0:hh:mm:ss.fff}] Current result: [{1}]", DateTime.Now, string.Join("", results));
                })).ToArray());
        }
        public void Run()
        {
            BlockingCollection<string> col = new BlockingCollection<string>();
            Task read = Task.Run(() =>
            {
                foreach (string v in col.GetConsumingEnumerable())
                    Console.WriteLine(v);

                Console.WriteLine("End of read task.");
            });

            Task write = Task.Run(() =>
            {
                while (true)
                {
                    string s = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(s))
                    {
                        col.CompleteAdding();
                        break;
                    }
                    col.Add(s);
                }
            });
            write.Wait();
            Thread.Sleep(1000);
        }
Example #19
0
		public void Publish_Where_Channel_Publication_Fails_Results_In_A_Non_Completed_Publication_Task_Which_Is_Placed_Back_On_The_Publication_Queue()
		{
			var _connection = Substitute.For<IConnection>();
			var _channel = Substitute.For<IModel>();
			var _publicationQueue = new BlockingCollection<Publication>();

			_connection.CreateModel().Returns(_channel);
			_channel
				.When(channel => channel.BasicPublish(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<IBasicProperties>(), Arg.Any<byte[]>()))
				.Do(callInfo => { throw new ApplicationException("Bang !"); });

			var _messageDelivery = new MessageDelivery("EXCHANGE", typeof(MyEvent).Name, MessageDeliveryMode.Persistent, message => "ARoutingKey");
			var _myEvent = new MyEvent(Guid.NewGuid(), "CorrlationId_1", "Detail", 1);
			var _taskCompletionSource = new TaskCompletionSource<PublicationResult>();
			var _publication = new Publication(_messageDelivery, _myEvent, _taskCompletionSource);

			var _SUT = new Publisher(_connection, _publicationQueue, CancellationToken.None);
			var _publisherTask = _SUT.Start();

			_publicationQueue.Add(_publication);
			try { _publisherTask.Wait(); } catch { }

			Assert.IsFalse(_publication.ResultTask.IsCompleted);
			Assert.AreSame(_publication, _publicationQueue.First());
		}
Example #20
0
 internal void SendEvent(Event e)
 {
     if (Connected && !(_sendQueue?.IsAddingCompleted ?? true))
     {
         _sendQueue?.Add(e);
     }
 }
        public static void InternalCancellation_WakingUp()
        {
            for (int test = 0; test < 2; test++)
            {
                BlockingCollection<int> coll1 = new BlockingCollection<int>(1);
                coll1.Add(1); //fills the collection.
                Assert.False(coll1.IsAddingCompleted,
                   "InternalCancellation_WakingUp:  At this point CompleteAdding should not have occurred.");

                // This is racy on what we want to test, in that it's possible this queued work could execute
                // so quickly that CompleteAdding happens before the tested method gets invoked, but the test
                // should still pass in such cases, we're just testing something other than we'd planned.
                Task t = Task.Run(() => coll1.CompleteAdding());

                // Try different methods that should wake up once CompleteAdding has been called
                int item = coll1.Take(); // remove the existing item in the collection
                switch (test)
                {
                    case 0:
                        Assert.Throws<InvalidOperationException>(() => coll1.Take());
                        break;
                    case 1:
                        Assert.False(coll1.TryTake(out item));
                        break;
                }

                t.Wait();

                Assert.True(coll1.IsAddingCompleted,
                   "InternalCancellation_WakingUp:  At this point CompleteAdding should have occurred.");
            }
        }
Example #22
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            BlockingCollection<String> col = new BlockingCollection<string>();
            Task read = Task.Run(() =>
            {
                foreach (String v in col.GetConsumingEnumerable())
                    Console.WriteLine(v);
            });

            Task write = Task.Run(() =>
            {
                while (true)
                {
                    String s = Console.ReadLine();
                    if (String.IsNullOrWhiteSpace(s)) break;
                    col.Add(s);
                }
            });

            write.Wait();

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
Example #23
0
 private SubstManager()
 {
     FindAvailableDrives();
     _freeDriveQ = new BlockingCollection<char>(_driveSet.Length);
     foreach (char drive in _driveSet)
         _freeDriveQ.Add(drive);
 }
Example #24
0
 public void Send(RpcPayload payload)
 {
     if (!_runCts.IsCancellationRequested)
     {
         _sendQueue?.Add(payload);
     }
 }
        public static void Run()
        {
            int size = 10;
            BlockingCollection<int> col = new BlockingCollection<int>(size/3);

            Task read = Task.Run(() =>
            {
                foreach(var item in col.GetConsumingEnumerable())
                {
                    Console.WriteLine("Read " + item);
                }
            });

            Task write = Task.Run(() =>
            {
                foreach(int i in Enumerable.Range(1, size))
                {
                    Console.WriteLine("adding " + i);
                    col.Add(i);
                }

                col.CompleteAdding();
            });

            write.Wait();
            read.Wait();
        }
        public override IEnumerable<Row> Execute(IEnumerable<Row> rows) {
            var blockingCollection = new BlockingCollection<Row>();
            var count = _operations.Count;
            if (count == 0) {
                yield break;
            }

            Debug("Creating tasks for {0} operations.", count);

            var tasks = _operations.Select(currentOp =>
            Task.Factory.StartNew(() => {
                try {
                    foreach (var row in currentOp.Execute(null)) {
                        blockingCollection.Add(row);
                    }
                }
                finally {
                    if (Interlocked.Decrement(ref count) == 0) {
                        blockingCollection.CompleteAdding();
                    }
                }
            })).ToArray();

            foreach (var row in blockingCollection.GetConsumingEnumerable()) {
                yield return row;
            }
            Task.WaitAll(tasks); // raise any exception that were raised during execution
        }
        public void AddTryTake(int producerThreads, int consumerThreads)
        {
            var stack = new BlockingCollection<int>();
            var startEvent = new ManualResetEventSlim(false);
            var finished = 0;
            var stop = false;
            var producerTasks = Enumerable.Range(0, producerThreads).Select(i => Task.Factory.StartNew(() =>
                {
                    var count = iterations/producerThreads;
                    startEvent.Wait();
                    for (var j = 0; j < count; j++)
                        stack.Add(0);
                    Interlocked.Increment(ref finished);
                    if (finished >= producerThreads) stop = true;
                }, TaskCreationOptions.LongRunning)).ToArray();
            var consumerTasks = Enumerable.Range(0, consumerThreads).Select(i => Task.Factory.StartNew(() =>
                {
                    int num;
                    startEvent.Wait();
                    while (!stop) stack.TryTake(out num);
                }, TaskCreationOptions.LongRunning)).ToArray();

            var stopwatch = Stopwatch.StartNew();
            startEvent.Set();
            stop = true;
            Task.WaitAll(producerTasks);
            Task.WaitAll(consumerTasks);
            stopwatch.StopAndLog(iterations);
        }
Example #28
0
        static void Main(string[] args)
        {
            BlockingCollection<String> col = new BlockingCollection<string>();

            Task read = Task.Run(() =>
            {
                while (true)
                {
                    Console.WriteLine(col.Take());
                }
            });

            Task write = Task.Run(() =>
            {
                while (true)
                {
                    string s = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(s))
                    {
                        break;
                    }
                    col.Add(s);
                }
            });

            write.Wait();

        }
Example #29
0
        private static void Decrypt(IHashAlgorithm algo)
        {
            // IProducerConsumerCollection
            using (var producerConsumerCollection = new BlockingCollection<string>(50000))
            {
                // Consumer.
                var tasks = new List<Task>();
                for (int workingThread = 0; workingThread < Environment.ProcessorCount; workingThread++)
                {
                    tasks.Add(Task.Factory.StartNew(() => HashThread(algo, producerConsumerCollection)));
                }

                // Producer.
                while (true)
                {
                    var line = Console.ReadLine();
                    if (line == null)
                    {
                        producerConsumerCollection.CompleteAdding();
                        break;
                    }
                    producerConsumerCollection.Add(line);
                }

                // Wait until processing is done.
                foreach (Task task in tasks)
                {
                    task.Wait();
                }
            }
        }
        public static void Main(string[] args)
        {
            BlockingCollection<string> collection = new BlockingCollection<string>();

            Task read = Task.Run(() =>
            {
                foreach (string v in collection.GetConsumingEnumerable())
                {
                    Console.WriteLine(v);
                }
            });

            Task write = Task.Run(() =>
            {
                while (true)
                {
                    string s = Console.ReadLine();

                    if (string.IsNullOrWhiteSpace(s))
                    {
                        collection.CompleteAdding();
                        break;
                    }

                    collection.Add(s);
                }
            });

            write.Wait();
        }
Example #31
0
        public Manifest Build(string path, IPackageMetaData packageMetaData)
        {
            var files = this.artefactProcessor.RetrieveFiles(path);

            var manifest = new Manifest
                {
                    Author = packageMetaData.Author,
                    Id = Guid.NewGuid(),
                    Name = packageMetaData.Name,
                    Path = path,
                    Tokens = packageMetaData.Tokens,
                    Version = packageMetaData.Version,
                };

            int progress = 0;

            var fileCount = files.Count();
            var manifestFiles = new BlockingCollection<ManifestFile>();

            Parallel.ForEach(
                files,
                file =>
                    {
                        manifestFiles.Add(new ManifestFile { File = StripParentPath(path, file) });
                        this.progressNotifier.UpdateProgress(ProgressStage.BuildManifest, fileCount, progress);
                        progress++;
                    });

            manifest.Files.AddRange(manifestFiles);

            return manifest;
        }
Example #32
0
        /// <summary>	
        /// 	<para>관리되는 대상 목록의 조건에 만족하는 타입을 반환합니다. </para>
        ///		<para>.NET Framework 4.0 빌드는 병렬적으로 작업을 처리합니다.</para>
        /// </summary>
        /// <returns>	
        /// 	조건에 만족하는 타입 목록입니다. 
        /// </returns>
        public override IEnumerable<Type> GetMatchingTypes()
        {
            #if NET40

            BlockingCollection<Type> blockedTypeCollection = new BlockingCollection<Type>();
            Parallel.ForEach( this.types, (type, loop) =>
            {
                if( type.IsDependencyAttribute() )
                    blockedTypeCollection.Add(type);
            });

            return blockedTypeCollection.AsEnumerable();

            #else
            foreach (var type in this.types)
            {
                if (type.IsDependencyAttribute())
                {
                    yield return type;
                }
                else if ( type.IsDynamicAttribute() )
                {
                    yield return type;
                }
            }
            #endif
        }
        public void TestWritableTransportServer()
        {
            BlockingCollection<WritableString> queue = new BlockingCollection<WritableString>();
            List<string> events = new List<string>();

            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
            var remoteHandler = Observer.Create<TransportEvent<WritableString>>(tEvent => queue.Add(tEvent.Data));

            using (var server = new WritableTransportServer<WritableString>(endpoint, remoteHandler, _tcpPortProvider, _injector))
            {
                server.Run();

                IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.LocalEndpoint.Port);
                using (var client = new WritableTransportClient<WritableString>(remoteEndpoint, _injector))
                {
                    client.Send(new WritableString("Hello"));
                    client.Send(new WritableString(", "));
                    client.Send(new WritableString("World!"));

                    events.Add(queue.Take().Data);
                    events.Add(queue.Take().Data);
                    events.Add(queue.Take().Data);
                } 
            }

            Assert.AreEqual(3, events.Count);
            Assert.AreEqual(events[0], "Hello");
            Assert.AreEqual(events[1], ", ");
            Assert.AreEqual(events[2], "World!");
        }
Example #34
0
        public object Read(Newtonsoft.Json.JsonReader reader)
        {
            if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject)
                throw new Exception();

            int w = ReadIntProperty(reader, "Width");
            int h = ReadIntProperty(reader, "Height");
            int d = ReadIntProperty(reader, "Depth");

            var grid = new TileData[d, h, w];

            reader.Read();
            if (reader.TokenType != Newtonsoft.Json.JsonToken.PropertyName || (string)reader.Value != "TileData")
                throw new Exception();

            ReadAndValidate(reader, Newtonsoft.Json.JsonToken.StartArray);

            var queue = new BlockingCollection<Tuple<int, byte[]>>();

            var readerTask = Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < d; ++i)
                {
                    reader.Read();
                    int z = (int)(long)reader.Value;

                    byte[] buf = reader.ReadAsBytes();

                    queue.Add(new Tuple<int, byte[]>(z, buf));
                }

                queue.CompleteAdding();
            });

            Parallel.For(0, d, i =>
            {
                var tuple = queue.Take();

                int z = tuple.Item1;
                byte[] arr = tuple.Item2;

                using (var memStream = new MemoryStream(arr))
                {
                    using (var decompressStream = new DeflateStream(memStream, CompressionMode.Decompress))
                    using (var streamReader = new BinaryReader(decompressStream))
                    {
                        for (int y = 0; y < h; ++y)
                            for (int x = 0; x < w; ++x)
                                grid[z, y, x].Raw = streamReader.ReadUInt64();
                    }
                }
            });

            readerTask.Wait();

            ReadAndValidate(reader, Newtonsoft.Json.JsonToken.EndArray);
            ReadAndValidate(reader, Newtonsoft.Json.JsonToken.EndObject);

            return grid;
        }
        public async Task CopyContainer(CloudBlobContainer sourceContainer, string destination)
        {
            var uri = new Uri(sourceContainer.Uri.AbsoluteUri.TrimEnd('/') + '/');
            destination = Path.Combine(destination, sourceContainer.Name);

            BlobContinuationToken continuationToken = null;
            do
            {
                var segments = await sourceContainer.ListBlobsSegmentedAsync(prefix: null, useFlatBlobListing: true,
                                                blobListingDetails: BlobListingDetails.Metadata, maxResults: MaxParallelDownloads,
                                                currentToken: continuationToken, options: null, operationContext: null);
                
                var tasks = new BlockingCollection<Task>(MaxParallelDownloads);

                Parallel.ForEach(segments.Results.Cast<CloudBlockBlob>(), srcFile =>
                {
                    var relativePath = uri.MakeRelativeUri(srcFile.Uri);
                    var destLocation = Path.Combine(destination, relativePath.OriginalString);
                    
                    if (File.Exists(destLocation) && File.GetLastWriteTimeUtc(destLocation) == srcFile.Properties.LastModified)
                    {
                        // If the file looks unchanged, skip it.
                        return;
                    }
                    Directory.CreateDirectory(Path.GetDirectoryName(destLocation));
                    tasks.Add(srcFile.DownloadToFileAsync(destLocation, FileMode.Create));
                });

                await Task.WhenAll(tasks);
                continuationToken = segments.ContinuationToken;
            } while (continuationToken != null);
        }
        static void Main(string[] args)
        {
            // create a blocking collection
            BlockingCollection<int> blockingCollection
                = new BlockingCollection<int>();

            // create and start a producer
            Task.Factory.StartNew(() => {
                // put the producer to sleep
                System.Threading.Thread.Sleep(500);
                for (int i = 0; i < 100; i++) {
                    // add the item to the collection
                    blockingCollection.Add(i);
                }
                // mark the collection as finished
                blockingCollection.CompleteAdding();
            });

            // create and start a consumer
            Task consumer = Task.Factory.StartNew(() => {
                // use a foreach loop to consume the blocking collection
                foreach (int i in blockingCollection) {
                    Console.WriteLine("Item {0}", i);
                }
                Console.WriteLine("Collection is fully consumed");
            });

            // wait for the consumer to finish
            consumer.Wait();

            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
 public void Write(byte[] data)
 {
     try {
         _writeQueue?.Add(data);
     } catch (InvalidOperationException) {
         Log.To.Sync.I(Tag, "Attempt to write after closing socket, ignore...");
     }
 }
Example #38
0
        /// <summary>
        /// Process customer's order request.
        /// </summary>
        /// <param name="orderRequest">Order request quantum</param>
        /// <returns></returns>
        public void ExecuteOrder(EffectProcessorsContainer effectsContainer)
        {
            RequestQuantum orderRequestQuantum = (RequestQuantum)effectsContainer.Envelope.Message;
            var            orderRequest        = (OrderRequest)orderRequestQuantum.RequestEnvelope.Message;
            var            updates             = new OrderMatcher(orderRequest, effectsContainer).Match();

            awaitedUpdates?.Add(updates);
        }
Example #39
0
        /// <inheritdoc />
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            Flush();

            if (!_synchronous)
            {
                _flushTimer?.Dispose();
                _requests?.Add(new ShutdownRequest());
                _writerTask?.Wait();
                _requests?.Dispose();
                _requests = null;
            }

            _disposed = true;
        }
Example #40
0
 public void AddTask(T task)
 {
     try
     {
         _blockingCollection?.Add(task);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Example #41
0
        public Job(int id, Process process, JobCallbackInfo callback = null)
        {
            logger.Info("Spawning new job id {0}; will call {1}", id, process.StartInfo.FileName);
            jobId        = id;
            this.process = process;

            // if a progress port was specified in callback info, prepare for streaming
            if (callback != null && callback.ProgressPort > 0)
            {
                var streamEp = new IPEndPoint(callback.Address, callback.ProgressPort);
                CreateProgressStream(streamEp);

                streamingCollection = new BlockingCollection <string>();

                process.OutputDataReceived += (s, a) => streamingCollection.Add(a.Data);
                process.ErrorDataReceived  += (s, a) => streamingCollection.Add(a.Data);

                streamingLoopTask = Task.Factory.StartNew(StreamingLoopHandler);
            }
            else
            {
                output = new HelperFunctions.ThreadSafeStringBuilder();
                process.OutputDataReceived += (s, a) => output.AppendLine(a.Data);
                process.ErrorDataReceived  += (s, a) => output.AppendLine(a.Data);
            }

            // always log output
            process.OutputDataReceived += (s, a) => logger.Debug($"Job {this.jobId} std output: {a.Data}");
            process.ErrorDataReceived  += (s, a) => logger.Debug($"Job {this.jobId} std error: {a.Data}");

            // add a logging message when a job finishes and ensure that streaming task stops if needed
            process.EnableRaisingEvents = true;
            process.Exited += (o, e) => logger.Info($"Job {id} finished executing.");
            process.Exited += (o, e) => streamingCollection?.Add(null);

            if (callback != null)
            {
                logger.Info("Registering job {0:d} for callbacks", id);

                // if callbacks are specified, we need to register for process events, and setup a handler
                callbackInfo = callback;

                // fire the callback handler in another thread (otherwise the callback might take a while and block other events waiting on exit)
                process.Exited += (o, e) => Task.Factory.StartNew(FireCompletionCallback);
            }

            process.Start();

            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
        }
Example #42
0
 private void Log(Guid CorrelationId, string Message, LogLevel LogLevel, bool SendToConsole)
 {
     if (LogLevel <= _settings.TraceLogLevel)
     {
         var message  = _settings.MaxTraceLogSize.HasValue ? Message.Truncate(_settings.MaxTraceLogSize.Value) : Message;
         var traceLog = new TraceLog(CorrelationId, message, LogLevel);
         FormatMessage(traceLog);
         _traceQueue?.Add(traceLog);
         if (SendToConsole)
         {
             Console.WriteLine(traceLog.Message);
         }
     }
 }
Example #43
0
        /// <summary>
        /// Queue the job for processing.
        /// </summary>
        /// <returns><c>true</c>, if job was queued, <c>false</c> otherwise.</returns>
        /// <param name="job">The job</param>
        /// </param>
        public bool Enqueue(object o)
        {
            if (!m_isRunning)
            {
                return(false);
            }

            m_jobQueue?.Add(o);

            lock (m_mainLock)
            {
                if (m_numberThreads < m_concurrency && m_numberThreads < m_jobQueue.Count)
                {
                    Util.FireAndForget(ProcessRequests, null, m_name, false);
                    ++m_numberThreads;
                }
            }
            return(true);
        }
Example #44
0
        public NotificationService(
            ClusterConfig clusterConfig,
            JsonSerializerSettings serializerSettings,
            IMessageBus messageBus)
        {
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));
            Contract.RequiresNonNull(messageBus, nameof(messageBus));

            this.clusterConfig      = clusterConfig;
            this.serializerSettings = serializerSettings;

            poolConfigs = clusterConfig.Pools.ToDictionary(x => x.Id, x => x);

            adminEmail = clusterConfig.Notifications?.Admin?.EmailAddress;
            //adminPhone = null;

            if (clusterConfig.Notifications?.Enabled == true)
            {
                queue = new BlockingCollection <QueuedNotification>();

                queueSub = queue.GetConsumingEnumerable()
                           .ToObservable(TaskPoolScheduler.Default)
                           .Select(notification => Observable.FromAsync(() => SendNotificationAsync(notification)))
                           .Concat()
                           .Subscribe();

                messageBus.Listen <BlockNotification>()
                .Subscribe(x =>
                {
                    queue?.Add(new QueuedNotification
                    {
                        Category = NotificationCategory.Block,
                        PoolId   = x.PoolId,
                        Subject  = "Block Notification",
                        Msg      = $"Pool {x.PoolId} found block candidate {x.BlockHeight}"
                    });
                });
            }
        }
Example #45
0
        /// <summary> Starts capturing and processing video frames. </summary>
        /// <param name="frameGrabDelay"> The frame grab delay. </param>
        /// <param name="timestampFn">    Function to generate the timestamp for each frame. This
        ///     function will get called once per frame. </param>
        protected void StartProcessing(TimeSpan frameGrabDelay, Func <DateTime> timestampFn)
        {
            OnProcessingStarting();

            ResetTrigger = true;
            FrameGrabTimer.Reset();
            AnalysisTaskQueue = new BlockingCollection <Task <NewResultEventArgs> >();

            var timerIterations = 0;

            // Create a background thread that will grab frames in a loop.
            ProducerTask = Task.Factory.StartNew(async() =>
            {
                var frameCount = 0;
                while (!Stopping)
                {
                    LogMessage("Producer: waiting for timer to trigger frame-grab");

                    // Wait to get released by the timer.
                    FrameGrabTimer.WaitOne();
                    LogMessage("Producer: grabbing frame...");

                    DateTime startTime;

                    // Grab single frame.
                    var timestamp = timestampFn();
                    var image     = new Mat();
                    var success   = Reader.Read(image);

                    LogMessage("Producer: frame-grab took {0} ms", (DateTime.Now - startTime).Milliseconds);

                    if (!success)
                    {
                        // If we've reached the end of the video, stop here.
                        if (Reader.CaptureType == CaptureType.File)
                        {
                            LogMessage("Producer: null frame from video file, stop!");
                            // This will call StopProcessing on a new thread.
                            await StopProcessingAsync();
                            // Break out of the loop to make sure we don't try grabbing more
                            // frames.
                            break;
                        }

                        // If failed on live camera, try again.
                        LogMessage("Producer: null frame from live camera, continue!");
                        continue;
                    }

                    // Package the image for submission.
                    VideoFrameMetadata meta;
                    meta.Index     = frameCount;
                    meta.Timestamp = timestamp;
                    var vframe     = new VideoFrame(image, meta);

                    // Raise the new frame event
                    LogMessage("Producer: new frame provided, should analyze? Frame num: {0}", meta.Index);
                    OnNewFrameProvided(vframe);

                    if (AnalysisPredicate(vframe))
                    {
                        LogMessage("Producer: analyzing frame");

                        // Call the analysis function on a threadpool thread
                        var analysisTask = DoAnalyzeFrame(vframe);

                        LogMessage("Producer: adding analysis task to queue {0}", analysisTask.Id);

                        // Push the frame onto the queue
                        AnalysisTaskQueue.Add(analysisTask);
                    }
                    else
                    {
                        LogMessage("Producer: not analyzing frame");
                    }

                    LogMessage("Producer: iteration took {0} ms", (DateTime.Now - startTime).Milliseconds);

                    ++frameCount;
                }

                LogMessage("Producer: stopping, destroy reader and timer");
                AnalysisTaskQueue.CompleteAdding();

                // We reach this point by breaking out of the while loop. So we must be stopping.
                Reader.Dispose();
                Reader = null;

                // Make sure the timer stops, then get rid of it.
                var h = new ManualResetEvent(false);
                Timer.Dispose(h);
                h.WaitOne();
                Timer = null;

                LogMessage("Producer: stopped");
            }, TaskCreationOptions.LongRunning);

            ConsumerTask = Task.Factory.StartNew(async() =>
            {
                while (!AnalysisTaskQueue.IsCompleted)
                {
                    LogMessage("Consumer: waiting for task to get added");

                    // Get the next processing task.
                    Task <NewResultEventArgs> nextTask = null;

                    // Blocks if m_analysisTaskQueue.Count == 0
                    // IOE means that Take() was called on a completed collection.
                    // Some other thread can call CompleteAdding after we pass the
                    // IsCompleted check but before we call Take.
                    // In this example, we can simply catch the exception since the
                    // loop will break on the next iteration.
                    // See https://msdn.microsoft.com/en-us/library/dd997371(v=vs.110).aspx
                    try
                    {
                        nextTask = AnalysisTaskQueue.Take();
                    }
                    catch (InvalidOperationException) { }

                    if (nextTask == null)
                    {
                        continue;
                    }
                    // Block until the result becomes available.
                    LogMessage("Consumer: waiting for next result to arrive for task {0}", nextTask.Id);
                    var result = await nextTask;

                    // Raise the new result event.
                    LogMessage("Consumer: got result for frame {0}. {1} tasks in queue", result.Frame.Metadata.Index, AnalysisTaskQueue.Count);
                    OnNewResultAvailable(result);
                }

                LogMessage("Consumer: stopped");
            }, TaskCreationOptions.LongRunning);

            // Set up a timer object that will trigger the frame-grab at a regular interval.
            Timer = new Timer(async s /* state */ =>
            {
                await TimerMutex.WaitAsync();
                try
                {
                    // If the handle was not reset by the producer, then the frame-grab was missed.
                    bool missed = FrameGrabTimer.WaitOne(0);

                    FrameGrabTimer.Set();

                    if (missed)
                    {
                        LogMessage("Timer: missed frame-grab {0}", timerIterations - 1);
                    }
                    LogMessage("Timer: grab frame num {0}", timerIterations);
                    ++timerIterations;
                }
                finally
                {
                    TimerMutex.Release();
                }
            }, null, TimeSpan.Zero, frameGrabDelay);

            OnProcessingStarted();
        }
Example #46
0
 public override void OnNext(byte value)
 {
     _fifo.Add(value);
 }
        public void ReadAll(Func <int, Release, bool> processor)
        {
            TaskCreationOptions taskOptions = TaskCreationOptions.LongRunning;

            Task[] consumers = new Task[this.ThreadCount];
            for (int i = 0; i < consumers.Length; ++i)
            {
                consumers[i] = Task.Factory.StartNew(delegate(object state)
                {
                    int threadNumber = (int)state;
                    int count        = 0;
                    try
                    {
                        string[] stringBatch;
                        while (true)
                        {
                            BlockingCollection <string[]> .TakeFromAny(stringCollection, out stringBatch, cancellationToken);
                            foreach (string releaseStringOriginal in stringBatch)
                            {
                                // Remove <releases>...</releases>
                                string releaseString = Utility.TrimString(releaseStringOriginal, "<releases>");
                                releaseString        = Utility.TrimString(releaseString, "</releases>");

                                var fixedText = Utility.FixXmlText(releaseString);
                                XDocument doc = XDocument.Parse(fixedText);
                                var release   = DataReader.ReadRelease(doc.Root);
                                ++count;
                                if (!processor(threadNumber, release))
                                {
                                    cancellationSource.Cancel();
                                    throw new OperationCanceledException();
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                    }
                    catch (InvalidOperationException)
                    {
                    }
                    catch (ArgumentException)
                    {
                    }
                }, i, taskOptions);
            }
            Task stringProducer = Task.Factory.StartNew(delegate
            {
                try
                {
                    string[] stringBatch;
                    while (ReadBatch(BatchSize, out stringBatch))
                    {
                        BlockingCollection <string[]> .AddToAny(stringCollection, stringBatch, cancellationToken);
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (InvalidOperationException)
                {
                }
                finally
                {
                    foreach (var coll in stringCollection)
                    {
                        coll.CompleteAdding();
                    }
                }
            }, taskOptions);
            Task dataReader = Task.Factory.StartNew(delegate
            {
                try
                {
                    int read         = 0;
                    char[] NewBuffer = new char[BufferSize];
                    while ((read = reader.ReadBlock(NewBuffer, 0, BufferSize)) > 0)
                    {
                        bufferCollection.Add(new CharBuffer(NewBuffer, read), cancellationToken);
                        NewBuffer = new char[BufferSize];
                    }
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    bufferCollection.CompleteAdding();
                }
            }, taskOptions);

            Task.WaitAll(consumers);
            stringProducer.Wait();
            dataReader.Wait();
        }
Example #48
0
        public NotificationService(
            ClusterConfig clusterConfig,
            JsonSerializerSettings serializerSettings,
            IMessageBus messageBus)
        {
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));
            Contract.RequiresNonNull(messageBus, nameof(messageBus));

            this.clusterConfig      = clusterConfig;
            this.serializerSettings = serializerSettings;

            poolConfigs = clusterConfig.Pools.ToDictionary(x => x.Id, x => x);

            adminEmail = clusterConfig.Notifications?.Admin?.EmailAddress;
            //adminPhone = null;

            if (clusterConfig.Notifications?.Enabled == true)
            {
                queue = new BlockingCollection <QueuedNotification>();

                queueSub = queue.GetConsumingEnumerable()
                           .ToObservable(TaskPoolScheduler.Default)
                           .Select(notification => Observable.FromAsync(() => SendNotificationAsync(notification)))
                           .Concat()
                           .Subscribe();

                messageBus.Listen <AdminNotification>()
                .Subscribe(x =>
                {
                    queue?.Add(new QueuedNotification
                    {
                        Category = NotificationCategory.Admin,
                        Subject  = x.Subject,
                        Msg      = x.Message
                    });
                });

                messageBus.Listen <BlockNotification>()
                .Subscribe(x =>
                {
                    queue?.Add(new QueuedNotification
                    {
                        Category = NotificationCategory.Block,
                        PoolId   = x.PoolId,
                        Subject  = "Block Notification",
                        Msg      = $"Pool {x.PoolId} found block candidate {x.BlockHeight}"
                    });
                });

                messageBus.Listen <PaymentNotification>()
                .Subscribe(x =>
                {
                    if (string.IsNullOrEmpty(x.Error))
                    {
                        queue?.Add(new QueuedNotification
                        {
                            Category = NotificationCategory.PaymentSuccess,
                            PoolId   = x.PoolId,
                            Subject  = "Payout Success Notification",
                            Msg      = $"Paid {FormatAmount(x.Amount, x.PoolId)} from pool {x.PoolId} to {x.RecpientsCount} recipients in Transaction(s) {x.TxInfo}."
                        });
                    }

                    else
                    {
                        queue?.Add(new QueuedNotification
                        {
                            Category = NotificationCategory.PaymentFailure,
                            PoolId   = x.PoolId,
                            Subject  = "Payout Failure Notification",
                            Msg      = $"Failed to pay out {x.Amount} {poolConfigs[x.PoolId].Coin.Type} from pool {x.PoolId}: {x.Error}"
                        });
                    }
                });
            }
        }
        void CalculateIntoGroup()
        {
            List <Guid>    leftRecords    = new List <Guid>();
            HashSet <Guid> filesProcessed = new HashSet <Guid>();

            groupedFiles = new Dictionary <int, Dictionary <Guid, List <Guid> > >();

            Dictionary <Guid, List <Guid> > disconnectedFiles = null;

            if (IncludesDisconnected)
            {
                disconnectedFiles = new Dictionary <Guid, List <Guid> >();
                foreach (var record in allRecords)
                {
                    if (record.Value.IgnoredMode == IgnoredMode.HiddenAndDisconnected)
                    {
                        if (!disconnectedFiles.TryGetValue(record.Value.File1Id, out var list))
                        {
                            list = new List <Guid>();
                            disconnectedFiles.Add(record.Value.File1Id, list);
                        }
                        list.Add(record.Key);
                        if (!disconnectedFiles.TryGetValue(record.Value.File2Id, out list))
                        {
                            list = new List <Guid>();
                            disconnectedFiles.Add(record.Value.File2Id, list);
                        }
                        list.Add(record.Key);
                    }
                    else
                    {
                        leftRecords.Add(record.Key);
                    }
                }
            }
            else
            {
                foreach (var record in allRecords)
                {
                    if (record.Value.IgnoredMode != IgnoredMode.HiddenAndDisconnected)
                    {
                        leftRecords.Add(record.Key);
                    }
                }
            }

            if (leftRecords.Count != 0)
            {
                //FileId, SimilarRecordID
                var unsortedGroup = new List <Dictionary <Guid, List <Guid> > >();

                BlockingCollection <Guid> needToPrepareThumbprints = null;
                Thread preparingFileThumbprints = null;

                needToPrepareThumbprints = new BlockingCollection <Guid>();
                preparingFileThumbprints = new Thread(PreparingFileThumbprints);
                preparingFileThumbprints.Start(needToPrepareThumbprints);

                while (leftRecords.Count > 0)
                {
                    var firstRecord = allRecords[leftRecords[0]];
                    leftRecords.RemoveAt(0);

                    Dictionary <Guid, List <Guid> > currentFilesGroup = new Dictionary <Guid, List <Guid> >();
                    unsortedGroup.Add(currentFilesGroup);

                    Queue <Guid> filesToProcess = new Queue <Guid>();
                    if (!filesProcessed.Contains(firstRecord.File1Id))
                    {
                        filesToProcess.Enqueue(firstRecord.File1Id);
                    }
                    if (!filesProcessed.Contains(firstRecord.File1Id))
                    {
                        filesToProcess.Enqueue(firstRecord.File2Id);
                    }
                    currentFilesGroup.Add(firstRecord.File1Id, new List <Guid>()
                    {
                        firstRecord.Id
                    });
                    needToPrepareThumbprints?.Add(firstRecord.File1Id);
                    currentFilesGroup.Add(firstRecord.File2Id, new List <Guid>()
                    {
                        firstRecord.Id
                    });
                    needToPrepareThumbprints?.Add(firstRecord.File2Id);

                    while (filesToProcess.Count > 0)
                    {
                        var fileToProcess = filesToProcess.Dequeue();
                        filesProcessed.Add(fileToProcess);

                        var records = fileToRecords[fileToProcess];

                        foreach (var recordId in records)
                        {
                            if (leftRecords.Remove(recordId))
                            {
                                var record = allRecords[recordId];
                                if (!filesProcessed.Contains(record.File1Id))
                                {
                                    filesToProcess.Enqueue(record.File1Id);
                                }
                                if (!filesProcessed.Contains(record.File2Id))
                                {
                                    filesToProcess.Enqueue(record.File2Id);
                                }
                                if (!currentFilesGroup.TryGetValue(record.File1Id, out var list))
                                {
                                    list = new List <Guid>();
                                    currentFilesGroup.Add(record.File1Id, list);
                                    needToPrepareThumbprints?.Add(record.File1Id);
                                }
                                list.Add(record.Id);
                                if (!currentFilesGroup.TryGetValue(record.File2Id, out list))
                                {
                                    list = new List <Guid>();
                                    currentFilesGroup.Add(record.File2Id, list);
                                    needToPrepareThumbprints?.Add(record.File2Id);
                                }
                                list.Add(record.Id);
                            }
                        }
                    }
                }

                var sortedGroup = unsortedGroup.OrderByDescending(i => i.Values.Sum(j => j.Count(k => allRecords[k].IgnoredMode == IgnoredMode.Effective))); //order by descending: total effective records in group.
                var groupIndex  = 0;
                foreach (var item in sortedGroup)
                {
                    groupedFiles.Add(groupIndex++, item);
                }

                needToPrepareThumbprints.CompleteAdding();

                preparingFileThumbprints.Join();
                needToPrepareThumbprints.Dispose();
            }

            if (disconnectedFiles?.Count > 0)
            {
                groupedFiles.Add(-1, disconnectedFiles);
            }
        }
Example #50
0
        void EnumerateGPOAdmin(string DomainName, BlockingCollection <LocalAdminInfo> output)
        {
            string targetsid = "S-1-5-32-544__Members";

            Console.WriteLine("Starting GPO Correlation");

            DirectorySearcher gposearcher = helpers.GetDomainSearcher(DomainName);

            gposearcher.Filter = "(&(objectCategory=groupPolicyContainer)(name=*)(gpcfilesyspath=*))";
            gposearcher.PropertiesToLoad.AddRange(new string[] { "displayname", "name", "gpcfilesyspath" });

            ConcurrentQueue <string> INIResults = new ConcurrentQueue <string>();

            Parallel.ForEach(gposearcher.FindAll().Cast <SearchResult>().ToArray(), (result) =>
            {
                string display = result.GetProp("displayname");
                string name    = result.GetProp("name");
                string path    = result.GetProp("gpcfilesyspath");

                if (display == null || name == null || path == null)
                {
                    return;
                }

                string template = String.Format("{0}\\{1}", path, "MACHINE\\Microsoft\\Windows NT\\SecEdit\\GptTmpl.inf");

                using (StreamReader sr = new StreamReader(template))
                {
                    string line        = String.Empty;
                    string currsection = String.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Match section = Regex.Match(line, @"^\[(.+)\]");
                        if (section.Success)
                        {
                            currsection = section.Captures[0].Value.Trim();
                        }

                        if (!currsection.Equals("[Group Membership]"))
                        {
                            continue;
                        }

                        Match key = Regex.Match(line, @"(.+?)\s*=(.*)");
                        if (key.Success)
                        {
                            string n = key.Groups[1].Value;
                            string v = key.Groups[2].Value;
                            if (n.Contains(targetsid))
                            {
                                v = v.Trim();
                                List <String> members    = v.Split(',').ToList();
                                List <DBObject> resolved = new List <DBObject>();
                                for (int i = 0; i < members.Count; i++)
                                {
                                    string m = members[i];
                                    m        = m.Trim('*');

                                    string sid;
                                    if (!m.StartsWith("S-1-", StringComparison.CurrentCulture))
                                    {
                                        try
                                        {
                                            sid = new NTAccount(DomainName, m).Translate(typeof(SecurityIdentifier)).Value;
                                        }
                                        catch
                                        {
                                            sid = null;
                                        }
                                    }
                                    else
                                    {
                                        sid = m;
                                    }
                                    if (sid == null)
                                    {
                                        continue;
                                    }

                                    string user = null;

                                    if (manager.FindBySID(sid, CurrentDomain, out DBObject obj))
                                    {
                                        user = obj.BloodHoundDisplayName;
                                    }
                                    else
                                    {
                                        try
                                        {
                                            DirectoryEntry entry = new DirectoryEntry($"LDAP://<SID={sid}");
                                            obj = entry.ConvertToDB();
                                            manager.InsertRecord(obj);
                                        }
                                        catch
                                        {
                                            obj = null;
                                        }
                                    }

                                    if (obj != null)
                                    {
                                        resolved.Add(obj);
                                    }
                                }
                                DirectorySearcher OUSearch = helpers.GetDomainSearcher(DomainName);

                                OUSearch.Filter = $"(&(objectCategory=organizationalUnit)(name=*)(gplink=*{name}*))";
                                foreach (SearchResult r in OUSearch.FindAll())
                                {
                                    DirectorySearcher compsearcher = helpers.GetDomainSearcher(DomainName, ADSPath: r.GetProp("adspath"));
                                    foreach (SearchResult ra in compsearcher.FindAll())
                                    {
                                        string sat = ra.GetProp("samaccounttype");
                                        if (sat == null)
                                        {
                                            continue;
                                        }

                                        DBObject resultdb = ra.ConvertToDB();

                                        if (sat.Equals("805306369"))
                                        {
                                            foreach (DBObject obj in resolved)
                                            {
                                                output.Add(new LocalAdminInfo
                                                {
                                                    ObjectName = obj.BloodHoundDisplayName,
                                                    ObjectType = obj.Type,
                                                    Server     = resultdb.BloodHoundDisplayName
                                                });
                                            }
                                        }
                                    }
                                    compsearcher.Dispose();
                                }
                                OUSearch.Dispose();
                            }
                        }
                    }
                }
            });

            gposearcher.Dispose();

            output.CompleteAdding();

            Console.WriteLine("Done GPO Correlation");
        }
Example #51
0
 protected override void QueueTask(Task task)
 {
     mainThreadQueue.Add(task);
 }
Example #52
0
 public static void Log(string data, LogLevel level)
 {
     LogQueue.Add(new LogItem(data, level));
 }
Example #53
0
        public void StartEnumeration()
        {
            Console.WriteLine("\nStarting Local Admin Enumeration");
            List <string> Domains   = helpers.GetDomainList();
            Stopwatch     watch     = Stopwatch.StartNew();
            Stopwatch     overwatch = Stopwatch.StartNew();

            foreach (string DomainName in Domains)
            {
                Console.WriteLine($"Started local admin enumeration for {DomainName}");
                CurrentDomain = DomainName;

                if (options.Stealth)
                {
                    BlockingCollection <LocalAdminInfo> coll = new BlockingCollection <LocalAdminInfo>();
                    Task gpowriter = StartWriter(coll, Task.Factory);
                    EnumerateGPOAdmin(DomainName, coll);
                    gpowriter.Wait();
                    continue;
                }

                BlockingCollection <Computer>       input  = new BlockingCollection <Computer>();
                BlockingCollection <LocalAdminInfo> output = new BlockingCollection <LocalAdminInfo>();

                LimitedConcurrencyLevelTaskScheduler scheduler = new LimitedConcurrencyLevelTaskScheduler(options.Threads);
                TaskFactory factory = new TaskFactory(scheduler);

                Task        writer      = StartWriter(output, factory);
                List <Task> taskhandles = new List <Task>();

                for (int i = 0; i < options.Threads; i++)
                {
                    taskhandles.Add(StartConsumer(input, output, factory));
                }

                System.Timers.Timer t = new System.Timers.Timer();

                if (options.NoDB)
                {
                    total = -1;


                    DirectorySearcher searcher = helpers.GetDomainSearcher(DomainName);
                    searcher.Filter = "(&(sAMAccountType=805306369)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))";
                    searcher.PropertiesToLoad.AddRange(new string[] { "dnshostname", "samaccounttype", "distinguishedname", "primarygroupid", "samaccountname", "objectsid" });

                    t.Elapsed += Timer_Tick;

                    t.Interval = options.Interval;
                    t.Enabled  = true;

                    PrintStatus();

                    foreach (SearchResult r in searcher.FindAll())
                    {
                        input.Add(r.ConvertToDB() as Computer);
                    }

                    searcher.Dispose();
                }
                else
                {
                    var computers =
                        manager.GetComputers().Find(x => x.Domain.Equals(DomainName));

                    total = computers.Count();

                    t.Elapsed += Timer_Tick;

                    t.Interval = options.Interval;
                    t.Enabled  = true;

                    PrintStatus();

                    foreach (Computer c in computers)
                    {
                        input.Add(c);
                    }
                }

                input.CompleteAdding();
                options.WriteVerbose("Waiting for enumeration threads to finish...");
                Task.WaitAll(taskhandles.ToArray());
                output.CompleteAdding();
                options.WriteVerbose("Waiting for writer thread to finish...");
                writer.Wait();
                PrintStatus();
                t.Dispose();
                Console.WriteLine($"Enumeration for {CurrentDomain} done in {watch.Elapsed}");
                watch.Reset();
            }
            Console.WriteLine($"Local Admin Enumeration done in {overwatch.Elapsed}\n");
            watch.Stop();
            overwatch.Stop();
        }
Example #54
0
        /// <summary>
        /// Enumerates GCRoots of a given object.  Similar to !gcroot.
        /// </summary>
        /// <param name="target">The target object to search for GC rooting.</param>
        /// <param name="returnOnlyFullyUniquePaths">Whether to only return fully unique paths.</param>
        /// <param name="maxDegreeOfParallelism">The number of threads this class is allowed to use to calculate the result.
        /// Setting this to 1 will cause the algorithm to run on the current thread.</param>
        /// <param name="roots">The roots to consider.  You can pass ClrMD.</param>
        /// <param name="cancellationToken">A cancellation token to stop enumeration.</param>
        /// <returns>An enumeration of all GC roots found for target.</returns>
        public IEnumerable <GCRootPath> EnumerateGCRoots(ulong target, bool returnOnlyFullyUniquePaths, int maxDegreeOfParallelism, IEnumerable <IClrRoot> roots, CancellationToken cancellationToken = default)
        {
            if (roots is null)
            {
                throw new ArgumentNullException(nameof(roots));
            }

            bool parallel = Heap.Runtime.IsThreadSafe && maxDegreeOfParallelism > 1;

            Dictionary <ulong, LinkedListNode <ClrObject> > knownEndPoints = new Dictionary <ulong, LinkedListNode <ClrObject> >()
            {
                { target, new LinkedListNode <ClrObject>(Heap.GetObject(target)) }
            };

            if (!parallel)
            {
                int count = 0;

                ObjectSet processedObjects = new ObjectSet(Heap);
                foreach (IClrRoot root in roots)
                {
                    LinkedList <ClrObject> path = PathsTo(processedObjects, knownEndPoints, root.Object, target, returnOnlyFullyUniquePaths, cancellationToken).FirstOrDefault();
                    if (path != null)
                    {
                        yield return(new GCRootPath(root, path.ToImmutableArray()));
                    }

                    if (count != processedObjects.Count)
                    {
                        count = processedObjects.Count;
                        ProgressUpdated?.Invoke(this, count);
                    }
                }
            }
            else
            {
                ParallelObjectSet            processedObjects = new ParallelObjectSet(Heap);
                ConcurrentQueue <GCRootPath> results          = new ConcurrentQueue <GCRootPath>();
                using BlockingCollection <IClrRoot?> queue    = new BlockingCollection <IClrRoot?>();

                Thread[] threads = new Thread[Math.Min(maxDegreeOfParallelism, Environment.ProcessorCount)];
                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i] = new Thread(() => WorkerThread(queue, results, processedObjects, knownEndPoints, target, all: true, returnOnlyFullyUniquePaths, cancellationToken))
                    {
                        Name = "GCRoot Worker Thread"
                    };
                    threads[i].Start();
                }

                foreach (IClrRoot root in roots)
                {
                    queue.Add(root);
                }

                // Add one sentinal value for every thread
                for (int i = 0; i < threads.Length; i++)
                {
                    queue.Add(null);
                }

                int count = 0;

                // Worker threads end when they have run out of roots to process.  While we are waiting for them to exit, yield return
                // any results they've found.  We'll use a 100 msec timeout because processing roots is slooooow and finding a root is
                // rare.  There's no reason to check these results super quickly and starve worker threads.
                for (int i = 0; i < threads.Length; i++)
                {
                    while (!threads[i].Join(100))
                    {
                        while (results.TryDequeue(out GCRootPath result))
                        {
                            yield return(result);
                        }
                    }

                    if (count != processedObjects.Count)
                    {
                        count = processedObjects.Count;
                        ProgressUpdated?.Invoke(this, count);
                    }
                }

                // We could have raced to put an object in the results queue while joining the last thread, so we need to drain the
                // results queue one last time.
                while (results.TryDequeue(out GCRootPath result))
                {
                    yield return(result);
                }

                if (count != processedObjects.Count)
                {
                    count = processedObjects.Count;
                    ProgressUpdated?.Invoke(this, count);
                }
            }
        }
        public Task StartAsync(CancellationToken ct)
        {
            queue = new BlockingCollection <QueuedNotification>();

            queueSub = queue.GetConsumingEnumerable()
                       .ToObservable(TaskPoolScheduler.Default)
                       .Select(notification => Observable.FromAsync(() => SendNotificationAsync(notification)))
                       .Concat()
                       .Subscribe();

            messageBus.Listen <AdminNotification>()
            .Subscribe(x =>
            {
                queue?.Add(new QueuedNotification
                {
                    Category = NotificationCategory.Admin,
                    Subject  = x.Subject,
                    Msg      = x.Message
                });
            });

            messageBus.Listen <BlockFoundNotification>()
            .Subscribe(x =>
            {
                queue?.Add(new QueuedNotification
                {
                    Category = NotificationCategory.Block,
                    PoolId   = x.PoolId,
                    Subject  = "Block Notification",
                    Msg      = $"Pool {x.PoolId} found block candidate {x.BlockHeight}"
                });
            });

            messageBus.Listen <PaymentNotification>()
            .Subscribe(x =>
            {
                if (string.IsNullOrEmpty(x.Error))
                {
                    var coin = poolConfigs[x.PoolId].Template;

                    // prepare tx links
                    string[] txLinks = null;

                    if (!string.IsNullOrEmpty(coin.ExplorerTxLink))
                    {
                        txLinks = x.TxIds.Select(txHash => string.Format(coin.ExplorerTxLink, txHash)).ToArray();
                    }

                    queue?.Add(new QueuedNotification
                    {
                        Category = NotificationCategory.PaymentSuccess,
                        PoolId   = x.PoolId,
                        Subject  = "Payout Success Notification",
                        Msg      = $"Paid {FormatAmount(x.Amount, x.PoolId)} from pool {x.PoolId} to {x.RecpientsCount} recipients in Transaction(s) {txLinks}."
                    });
                }

                else
                {
                    queue?.Add(new QueuedNotification
                    {
                        Category = NotificationCategory.PaymentFailure,
                        PoolId   = x.PoolId,
                        Subject  = "Payout Failure Notification",
                        Msg      = $"Failed to pay out {x.Amount} {poolConfigs[x.PoolId].Template.Symbol} from pool {x.PoolId}: {x.Error}"
                    });
                }
            });

            logger.Info(() => "Online");

            return(Task.CompletedTask);
        }
Example #56
0
 public static void SendAsync(HarmonyPacket p)
 {
     _toSend?.Add(p);
 }
Example #57
0
 public void AsyncAdd(Action action)
 {
     lock (SyncRoot)
         queue?.Add(new Tuple <Action, ManualResetEvent>(action, null));
 }
Example #58
0
 public void LogPerformance(Guid CorrelationId, string OperationName, TimeSpan OperationDuration) => _performanceQueue?.Add(new PerformanceLog(CorrelationId, OperationName, OperationDuration));
Example #59
0
 private void LogMetric(Guid CorrelationId, string ItemId, string MetricName, DateTime?DateTimeValue, int?IntValue, string TextValue) =>
 _metricQueue?.Add(new MetricLog(CorrelationId, ItemId, MetricName, DateTimeValue, IntValue, TextValue));
Example #60
-1
        public static void Main()
        {
            BlockingCollection<string> col = new BlockingCollection<string>();
            Task read = Task.Run(() =>
            {
                while (true)
                {
                    //Console.WriteLine("Taking");
                    col.Add("Removing");
                }
            });

            Task write = Task.Run(() =>
            {
                while (true)
                {
                    string s = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(s)) break;
                    Console.WriteLine("Adding");
                    col.Add(s);
                    col.CompleteAdding();

                }
            });

            write.Wait();
        }