SubscriberSocket.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Text;
  3. using NetMQ.Core;
  4. namespace NetMQ.Sockets
  5. {
  6. /// <summary>
  7. /// A SubscriberSocket is a NetMQSocket intended to be used as the "Sub" in the PubSub pattern.
  8. /// The intended usage is to receive messages from the publisher socket.
  9. /// </summary>
  10. public class SubscriberSocket : NetMQSocket
  11. {
  12. /// <summary>
  13. /// Create a new SubscriberSocket 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 connect (if endpoint doesn't start with '@' or '>')</param>
  17. /// <example><code>var socket = new SubscriberSocket(">tcp://127.0.0.1:5555,@tcp://127.0.0.1:55556");</code></example>
  18. public SubscriberSocket(string? connectionString = null) : base(ZmqSocketType.Sub, connectionString, DefaultAction.Connect)
  19. {
  20. }
  21. /// <summary>
  22. /// Create a new SubscriberSocket based upon the given SocketBase.
  23. /// </summary>
  24. /// <param name="socketHandle">the SocketBase to create the new socket from</param>
  25. internal SubscriberSocket(SocketBase socketHandle)
  26. : base(socketHandle)
  27. {
  28. }
  29. /// <inheritdoc />
  30. public override bool TrySend(ref Msg msg, TimeSpan timeout, bool more)
  31. {
  32. throw new NotSupportedException("Subscriber socket doesn't support sending");
  33. }
  34. /// <summary>
  35. /// Subscribe this socket to the given 'topic' - which means enable this socket to receive
  36. /// messages that begin with this string prefix.
  37. /// You can set topic to an empty string to subscribe to everything.
  38. /// </summary>
  39. /// <param name="topic">this specifies what text-prefix to subscribe to, or may be an empty-string to specify ALL</param>
  40. public virtual void Subscribe(string topic)
  41. {
  42. SetSocketOption(ZmqSocketOption.Subscribe, topic);
  43. }
  44. /// <summary>
  45. /// Subscribe this socket to the given 'topic' - which means enable this socket to receive
  46. /// messages that begin with this string prefix, using the given Encoding.
  47. /// You can set topic to an empty string to subscribe to everything.
  48. /// </summary>
  49. /// <param name="topic">this specifies what text-prefix to subscribe to, or may be an empty-string to specify ALL</param>
  50. /// <param name="encoding">the character-Encoding to use when converting the topic string internally into a byte-array</param>
  51. public virtual void Subscribe(string topic, Encoding encoding)
  52. {
  53. Subscribe(encoding.GetBytes(topic));
  54. }
  55. /// <summary>
  56. /// Subscribe this socket to the given 'topic' - which means enable this socket to receive
  57. /// messages that begin with this array of bytes.
  58. /// </summary>
  59. /// <param name="topic">this specifies what byte-array prefix to subscribe to</param>
  60. public virtual void Subscribe(byte[] topic)
  61. {
  62. SetSocketOption(ZmqSocketOption.Subscribe, topic);
  63. }
  64. /// <summary>
  65. /// Subscribe this socket to all topics - which means enable this socket to receive
  66. /// all messages regardless of what the string prefix is.
  67. /// This is the same as calling Subscribe with an empty-string for the topic.
  68. /// </summary>
  69. public virtual void SubscribeToAnyTopic()
  70. {
  71. Subscribe(string.Empty);
  72. }
  73. /// <summary>
  74. /// Remove this socket's subscription to the given topic.
  75. /// </summary>
  76. /// <param name="topic">a string denoting which the topic to stop receiving</param>
  77. public virtual void Unsubscribe(string topic)
  78. {
  79. SetSocketOption(ZmqSocketOption.Unsubscribe, topic);
  80. }
  81. /// <summary>
  82. /// Remove this socket's subscription to the given topic.
  83. /// </summary>
  84. /// <param name="topic">a string denoting which the topic to stop receiving</param>
  85. /// <param name="encoding">the Encoding to use when converting the topic string internally into a byte-array</param>
  86. public virtual void Unsubscribe(string topic, Encoding encoding)
  87. {
  88. Unsubscribe(encoding.GetBytes(topic));
  89. }
  90. /// <summary>
  91. /// Remove this socket's subscription to the given topic.
  92. /// </summary>
  93. /// <param name="topic">a byte-array denoting which the topic to stop receiving</param>
  94. public virtual void Unsubscribe(byte[] topic)
  95. {
  96. SetSocketOption(ZmqSocketOption.Unsubscribe, topic);
  97. }
  98. }
  99. }