PeerSocket.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using NetMQ.Core;
  3. namespace NetMQ.Sockets
  4. {
  5. /// <summary>
  6. /// Peer socket, the first message is always the identity of the sender
  7. /// </summary>
  8. public class PeerSocket : NetMQSocket
  9. {
  10. /// <summary>
  11. /// Create a new PeerSocket and attach socket to zero or more endpoints.
  12. /// </summary>
  13. /// <param name="connectionString">List of NetMQ endpoints, separated by commas and prefixed by '@' (to bind the socket) or '>' (to connect the socket).
  14. /// Default action is connect (if endpoint doesn't start with '@' or '>')</param>
  15. /// <example><code>var socket = new PeerSocket(">tcp://127.0.0.1:5555,>tcp://127.0.0.1:55556");</code></example>
  16. public PeerSocket(string? connectionString = null) : base(ZmqSocketType.Peer, connectionString, DefaultAction.Connect)
  17. {
  18. }
  19. /// <summary>
  20. /// Create a new PeerSocket based upon the given SocketBase.
  21. /// </summary>
  22. /// <param name="socketHandle">the SocketBase to create the new socket from</param>
  23. internal PeerSocket(SocketBase socketHandle) : base(socketHandle)
  24. {
  25. }
  26. /// <summary>
  27. /// Connect the peer socket to <paramref name="address"/>.
  28. /// </summary>
  29. /// <param name="address">a string denoting the address to connect this socket to</param>
  30. /// <returns>The peer allocated routing id</returns>
  31. /// <exception cref="ObjectDisposedException">thrown if the socket was already disposed</exception>
  32. /// <exception cref="TerminatingException">The socket has been stopped.</exception>
  33. /// <exception cref="NetMQException">No IO thread was found.</exception>
  34. /// <exception cref="AddressAlreadyInUseException">The specified address is already in use.</exception>
  35. public byte[] ConnectPeer(string address)
  36. {
  37. Connect(address);
  38. byte[]? routingId = GetSocketOptionX<byte[]>(ZmqSocketOption.LastPeerRoutingId);
  39. Assumes.NotNull(routingId);
  40. return routingId;
  41. }
  42. }
  43. }