/// <summary> /// Bind this socket to the given address. /// </summary> /// <param name="addr">a string denoting the endpoint-address to bind to</param> /// <exception cref="AddressAlreadyInUseException">the address specified to bind to must not be already in use</exception> /// <exception cref="ArgumentException">The requested protocol is not supported.</exception> /// <exception cref="FaultException">the socket bind failed</exception> /// <exception cref="NetMQException">No IO thread was found, or the protocol's listener encountered an /// error during initialisation.</exception> /// <exception cref="ProtocolNotSupportedException">the specified protocol is not supported</exception> /// <exception cref="ProtocolNotSupportedException">the socket type and protocol do not match</exception> /// <exception cref="TerminatingException">The socket has been stopped.</exception> /// <remarks> /// The supported protocols are "inproc", "ipc", "tcp", "pgm", and "epgm". /// If the protocol is either "pgm" or "epgm", then this socket must be of type Pub, Sub, XPub, or XSub. /// If the protocol is "inproc", you cannot bind to the same address more than once. /// </remarks> public void Bind([NotNull] string addr) { CheckContextTerminated(); // Process pending commands, if any. ProcessCommands(0, false); string protocol; string address; DecodeAddress(addr, out address, out protocol); CheckProtocol(protocol); switch (protocol) { case Address.InProcProtocol: { var endpoint = new Ctx.Endpoint(this, m_options); bool addressRegistered = RegisterEndpoint(addr, endpoint); if (!addressRegistered) throw new AddressAlreadyInUseException($"Cannot bind address ( {addr} ) - already in use."); m_options.LastEndpoint = addr; return; } case Address.PgmProtocol: case Address.EpgmProtocol: { if (m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub) { // For convenience's sake, bind can be used interchangeable with // connect for PGM and EPGM transports. Connect(addr); return; } break; } } // Remaining transports require to be run in an I/O thread, so at this // point we'll choose one. var ioThread = ChooseIOThread(m_options.Affinity); if (ioThread == null) throw NetMQException.Create(ErrorCode.EmptyThread); switch (protocol) { case Address.TcpProtocol: { var listener = new TcpListener(ioThread, this, m_options); try { listener.SetAddress(address); m_port = listener.Port; // Recreate the address string (localhost:1234) in case the port was system-assigned addr = $"tcp://{address.Substring(0, address.IndexOf(':'))}:{m_port}"; } catch (NetMQException ex) { listener.Destroy(); EventBindFailed(addr, ex.ErrorCode); throw; } m_options.LastEndpoint = listener.Address; AddEndpoint(addr, listener, null); break; } case Address.PgmProtocol: case Address.EpgmProtocol: { var listener = new PgmListener(ioThread, this, m_options); try { listener.Init(address); } catch (NetMQException ex) { listener.Destroy(); EventBindFailed(addr, ex.ErrorCode); throw; } m_options.LastEndpoint = addr; AddEndpoint(addr, listener, null); break; } case Address.IpcProtocol: { var listener = new IpcListener(ioThread, this, m_options); try { listener.SetAddress(address); m_port = listener.Port; } catch (NetMQException ex) { listener.Destroy(); EventBindFailed(addr, ex.ErrorCode); throw; } m_options.LastEndpoint = listener.Address; AddEndpoint(addr, listener,null); break; } default: { throw new ArgumentException($"Address {addr} has unsupported protocol: {protocol}", nameof(addr)); } } }
protected bool RegisterEndpoint([NotNull] string addr, [NotNull] Ctx.Endpoint endpoint) { return(m_ctx.RegisterEndpoint(addr, endpoint)); }
protected bool RegisterEndpoint(string addr, Ctx.Endpoint endpoint) { return(m_ctx.RegisterEndpoint(addr, endpoint)); }