NetMQRuntime.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. namespace NetMQ
  7. {
  8. /// <summary>
  9. /// NetMQRuntime enable using NetMQSocket receive async methods.
  10. /// You need to create an instance before calling any async methods.
  11. /// To continue and process the Tasks call <see cref="Run(Task[])" /> and <see cref="Run(CancellationToken, Task[])"/>
  12. /// </summary>
  13. public class NetMQRuntime : IDisposable
  14. {
  15. private NetMQPoller m_poller;
  16. private readonly NetMQSynchronizationContext m_synchronizationContext;
  17. private readonly SynchronizationContext m_oldSynchronizationContext;
  18. private static readonly ThreadLocal<NetMQRuntime> s_current = new ThreadLocal<NetMQRuntime>();
  19. private readonly List<NetMQSocket> m_sockets;
  20. /// <summary>
  21. /// Create a new NetMQRuntime, you can start calling async method after creating a runtime.
  22. /// </summary>
  23. public NetMQRuntime()
  24. {
  25. m_poller = new NetMQPoller();
  26. m_sockets = new List<NetMQSocket>();
  27. m_synchronizationContext = new NetMQSynchronizationContext(m_poller);
  28. m_oldSynchronizationContext = SynchronizationContext.Current;
  29. SynchronizationContext.SetSynchronizationContext(m_synchronizationContext);
  30. s_current.Value = this;
  31. }
  32. /// <summary>
  33. /// The current thread NetMQRuntime
  34. /// </summary>
  35. public static NetMQRuntime Current
  36. {
  37. get { return s_current.Value; }
  38. }
  39. internal static NetMQPoller Poller
  40. {
  41. get { return Current.m_poller; }
  42. }
  43. /// <summary>
  44. /// Run the tasks to completion
  45. /// </summary>
  46. /// <param name="tasks">The list of tasks to run</param>
  47. public void Run(params Task[] tasks)
  48. {
  49. Run(CancellationToken.None, tasks);
  50. }
  51. internal void Add(NetMQSocket socket)
  52. {
  53. m_poller.Add(socket);
  54. m_sockets.Add(socket);
  55. }
  56. internal void Remove(NetMQSocket socket)
  57. {
  58. m_poller.Remove(socket);
  59. m_sockets.Remove(socket);
  60. }
  61. /// <summary>
  62. /// Run the tasks to completion
  63. /// </summary>
  64. /// <param name="cancellationToken">Cancellation token to cancel the run operation before all tasks run to completion</param>
  65. /// <param name="tasks">The list of tasks to run</param>
  66. public void Run(CancellationToken cancellationToken, params Task[] tasks)
  67. {
  68. var registration = cancellationToken.Register(() => m_poller.StopAsync(), false);
  69. Task.WhenAll(tasks).ContinueWith(t => m_poller.Stop(), cancellationToken);
  70. m_poller.Run(m_synchronizationContext);
  71. registration.Dispose();
  72. }
  73. /// <summary>
  74. /// Dispose the runtime, don't call Async method after disposing
  75. /// </summary>
  76. public void Dispose()
  77. {
  78. foreach (var socket in m_sockets)
  79. {
  80. socket.DetachFromRuntime();
  81. }
  82. m_poller.Dispose();
  83. SynchronizationContext.SetSynchronizationContext(m_oldSynchronizationContext);
  84. }
  85. }
  86. }
  87. #endif