ConcurrentOutputListener.cs 1018 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace sccdlib
  5. {
  6. public class ConcurrentOutputListener : IOutputListener
  7. {
  8. ConcurrentQueue<Event> queue = new ConcurrentQueue<Event>();
  9. List<string> ports = new List<string>();
  10. public ConcurrentOutputListener (string[] port_names)
  11. {
  12. foreach (string port_name in port_names)
  13. {
  14. this.ports.Add (port_name);
  15. }
  16. }
  17. public void add (Event output_event)
  18. {
  19. if (this.ports.Count == 0 || this.ports.Contains (output_event.getPort ())) {
  20. this.queue.Enqueue (output_event);
  21. }
  22. }
  23. public Event fetch ()
  24. {
  25. Event fetched_event;
  26. bool success = this.queue.TryDequeue (out fetched_event);
  27. if (success) {
  28. return fetched_event;
  29. }
  30. return null;
  31. }
  32. }
  33. }