XPublisherSocket.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Text;
  2. using NetMQ.Core;
  3. namespace NetMQ.Sockets
  4. {
  5. /// <summary>
  6. /// An XPublisherSocket is a NetMQSocket intended to be used as the XPub in the XPub/XSub pattern.
  7. /// The intended usage is for serving, together with a matching XSubscriberSocket,
  8. /// as a stable intermediary between a PublisherSocket and it's SubscriberSockets.
  9. /// </summary>
  10. public class XPublisherSocket : NetMQSocket
  11. {
  12. /// <summary>
  13. /// Create a new XPublisherSocket and attach socket to zero or more endpoints.
  14. /// </summary>
  15. /// <param name="connectionString">List of NetMQ endpoints, separated by commas and prefixed by '@' (to bind the socket) or '>' (to connect the socket).
  16. /// Default action is bind (if endpoint doesn't start with '@' or '>')</param>
  17. /// <example><code>var socket = new XPublisherSocket(">tcp://127.0.0.1:5555,>tcp://127.0.0.1:55556");</code></example>
  18. public XPublisherSocket(string? connectionString = null) : base(ZmqSocketType.Xpub, connectionString, DefaultAction.Bind)
  19. {
  20. }
  21. /// <summary>
  22. /// Create a new XPublisherSocket based upon the given <see cref="SocketBase"/>.
  23. /// </summary>
  24. /// <param name="socketHandle">the SocketBase to create the new socket from</param>
  25. internal XPublisherSocket(SocketBase socketHandle)
  26. : base(socketHandle)
  27. {
  28. }
  29. /// <summary>
  30. /// In case of socket set to manual mode will subscribe the last subscriber to the topic
  31. /// </summary>
  32. /// <param name="topic">a string specifying the Topic to subscribe to</param>
  33. public virtual void Subscribe(string topic)
  34. {
  35. SetSocketOption(ZmqSocketOption.Subscribe, topic);
  36. }
  37. /// <summary>
  38. /// In case of socket set to manual mode will subscribe the last subscriber to the topic
  39. /// </summary>
  40. /// <param name="topic">a string specifying the Topic to subscribe to</param>
  41. /// <param name="encoding">the character-Encoding to use when converting the topic string internally into a byte-array</param>
  42. public virtual void Subscribe(string topic, Encoding encoding)
  43. {
  44. Subscribe(encoding.GetBytes(topic));
  45. }
  46. /// <summary>
  47. /// In case of socket set to manual mode will subscribe the last subscriber to the topic
  48. /// </summary>
  49. /// <param name="topic">a byte-array specifying the Topic to subscribe to</param>
  50. public virtual void Subscribe(byte[] topic)
  51. {
  52. SetSocketOption(ZmqSocketOption.Subscribe, topic);
  53. }
  54. /// <summary>
  55. /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic
  56. /// </summary>
  57. /// <param name="topic">a string specifying the Topic to unsubscribe from</param>
  58. public virtual void Unsubscribe(string topic)
  59. {
  60. SetSocketOption(ZmqSocketOption.Unsubscribe, topic);
  61. }
  62. /// <summary>
  63. /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic
  64. /// </summary>
  65. /// <param name="topic">a string specifying the Topic to unsubscribe from</param>
  66. /// <param name="encoding">the character-Encoding to use when converting the topic string internally into a byte-array</param>
  67. public virtual void Unsubscribe(string topic, Encoding encoding)
  68. {
  69. Unsubscribe(encoding.GetBytes(topic));
  70. }
  71. /// <summary>
  72. /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic
  73. /// </summary>
  74. /// <param name="topic">a byte-array specifying the Topic to unsubscribe from</param>
  75. public virtual void Unsubscribe(byte[] topic)
  76. {
  77. SetSocketOption(ZmqSocketOption.Unsubscribe, topic);
  78. }
  79. /// <summary>
  80. /// Publisher sockets generally send a welcome-message to subscribers to give an indication that they have successful subscribed.
  81. /// This method clears that message, such that none is sent.
  82. /// </summary>
  83. public void ClearWelcomeMessage()
  84. {
  85. SetSocketOption(ZmqSocketOption.XPublisherWelcomeMessage, null);
  86. }
  87. /// <summary>
  88. /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed.
  89. /// This method is how you set the text of that welcome-message.
  90. /// </summary>
  91. /// <param name="welcomeMessage">a string denoting the new value for the welcome-message</param>
  92. /// <param name="encoding">the character-Encoding to use when converting the topic string internally into a byte-array</param>
  93. public void SetWelcomeMessage(string welcomeMessage, Encoding encoding)
  94. {
  95. SetWelcomeMessage(encoding.GetBytes(welcomeMessage));
  96. }
  97. /// <summary>
  98. /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed.
  99. /// This method is how you set the text of that welcome-message. The Encoding is assumed to be ASCII.
  100. /// </summary>
  101. /// <param name="welcomeMessage">a string denoting the new value for the welcome-message</param>
  102. public void SetWelcomeMessage(string welcomeMessage)
  103. {
  104. SetWelcomeMessage(Encoding.ASCII.GetBytes(welcomeMessage));
  105. }
  106. /// <summary>
  107. /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed.
  108. /// This method is how you set the text of that welcome-message. The Encoding is assumed to be ASCII.
  109. /// </summary>
  110. /// <param name="welcomeMessage">a byte-array denoting the new value for the welcome-message</param>
  111. public void SetWelcomeMessage(byte[] welcomeMessage)
  112. {
  113. SetSocketOption(ZmqSocketOption.XPublisherWelcomeMessage, welcomeMessage);
  114. }
  115. }
  116. }