NetMQConfig.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using NetMQ.Core;
  3. namespace NetMQ
  4. {
  5. /// <summary>
  6. /// Global configuration class for NetMQ
  7. /// </summary>
  8. public static class NetMQConfig
  9. {
  10. private static TimeSpan s_linger;
  11. private static Ctx? s_ctx;
  12. private static int s_threadPoolSize = Ctx.DefaultIOThreads;
  13. private static int s_maxSockets = Ctx.DefaultMaxSockets;
  14. private static readonly object s_sync;
  15. static NetMQConfig()
  16. {
  17. s_sync = new object();
  18. s_linger = TimeSpan.Zero;
  19. }
  20. internal static Ctx Context
  21. {
  22. get
  23. {
  24. // Optimise for the case where the value is non-null, and we don't need to acquire the lock
  25. var c = s_ctx;
  26. if (c != null)
  27. return c;
  28. lock (s_sync)
  29. {
  30. // Check again whether it's null now that we have the lock
  31. return s_ctx ??= new Ctx
  32. {
  33. IOThreadCount = s_threadPoolSize,
  34. MaxSockets = s_maxSockets
  35. };
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Cleanup library resources, call this method when your process is shutting-down.
  41. /// </summary>
  42. /// <param name="block">Set to true when you want to make sure sockets send all pending messages</param>
  43. public static void Cleanup(bool block = true)
  44. {
  45. lock (s_sync)
  46. {
  47. if (s_ctx != null)
  48. {
  49. s_ctx.Terminate(block);
  50. s_ctx = null;
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Get or set the default linger period for the all sockets,
  56. /// which determines how long pending messages which have yet to be sent to a peer
  57. /// shall linger in memory after a socket is closed.
  58. /// </summary>
  59. /// <remarks>
  60. /// This also affects the termination of the socket's context.
  61. /// <para />
  62. /// -1: Specifies infinite linger period. Pending messages shall not be discarded after the socket is closed;
  63. /// attempting to terminate the socket's context shall block until all pending messages have been sent to a peer.
  64. /// <para />
  65. /// 0: The default value of <see cref="TimeSpan.Zero"/> specifies no linger period. Pending messages shall be discarded immediately when the socket is closed.
  66. /// Positive values specify an upper bound for the linger period. Pending messages shall not be discarded after the socket is closed;
  67. /// attempting to terminate the socket's context shall block until either all pending messages have been sent to a peer,
  68. /// or the linger period expires, after which any pending messages shall be discarded.
  69. /// </remarks>
  70. public static TimeSpan Linger
  71. {
  72. get
  73. {
  74. lock (s_sync)
  75. {
  76. return s_linger;
  77. }
  78. }
  79. set
  80. {
  81. lock (s_sync)
  82. {
  83. s_linger = value;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// Get or set the number of IO Threads NetMQ will create, default is 1.
  89. /// 1 is good for most cases.
  90. /// </summary>
  91. public static int ThreadPoolSize
  92. {
  93. get
  94. {
  95. lock (s_sync)
  96. return s_threadPoolSize;
  97. }
  98. set
  99. {
  100. lock (s_sync)
  101. {
  102. s_threadPoolSize = value;
  103. if (s_ctx != null)
  104. s_ctx.IOThreadCount = value;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Get or set the maximum number of sockets.
  110. /// </summary>
  111. public static int MaxSockets
  112. {
  113. get
  114. {
  115. lock (s_sync)
  116. return s_maxSockets;
  117. }
  118. set
  119. {
  120. lock (s_sync)
  121. {
  122. s_maxSockets = value;
  123. if (s_ctx != null)
  124. s_ctx.MaxSockets = value;
  125. }
  126. }
  127. }
  128. #region Obsolete
  129. /// <summary>
  130. /// Method is obsolete, call Cleanup instead
  131. /// </summary>
  132. [Obsolete("Use Cleanup method")]
  133. public static void ManualTerminationTakeOver()
  134. {
  135. }
  136. /// <summary>
  137. /// Method is obsolete, call Cleanup instead
  138. /// </summary>
  139. [Obsolete("Use Cleanup method")]
  140. internal static void DisableManualTermination()
  141. {
  142. }
  143. /// <summary>
  144. /// Method is obsolete, call Cleanup instead
  145. /// </summary>
  146. /// <param name="block">Should the context block the thread while terminating.</param>
  147. [Obsolete("Use Cleanup method")]
  148. public static void ContextTerminate(bool block = true)
  149. {
  150. }
  151. /// <summary>
  152. /// Method is obsolete, context created automatically
  153. /// </summary>
  154. [Obsolete("Context is created automatically")]
  155. public static void ContextCreate(bool block = false)
  156. {
  157. Cleanup(block);
  158. }
  159. #endregion
  160. }
  161. }