NetMQ.Core.SocketBase.Bind C# (CSharp) Method

Bind() public method

Bind this socket to the given address.
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.
the address specified to bind to must not be already in use The requested protocol is not supported. the socket bind failed No IO thread was found, or the protocol's listener encountered an /// error during initialisation. the specified protocol is not supported the socket type and protocol do not match The socket has been stopped.
public Bind ( [ addr ) : void
addr [ a string denoting the endpoint-address to bind to
return void
        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(string.Format("Cannot bind address ( {0} ) - already in use.", addr));

                        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 = string.Format("tcp://{0}:{1}",
                                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(string.Format("Address {0} has unsupported protocol: {1}", addr, protocol), "addr");
                    }
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Register the given events to monitor on the given endpoint.
        /// </summary>
        /// <param name="addr">a string denoting the endpoint to monitor. If this is null - monitoring is stopped.</param>
        /// <param name="events">the SocketEvent to monitor for</param>
        /// <exception cref="NetMQException">Maximum number of sockets reached.</exception>
        /// <exception cref="ProtocolNotSupportedException">The protocol of <paramref name="addr"/> is not supported.</exception>
        /// <exception cref="TerminatingException">The socket has been stopped.</exception>
        public void Monitor([CanBeNull] string addr, SocketEvents events)
        {
            CheckContextTerminated();

            // Support de-registering monitoring endpoints as well
            if (addr == null)
            {
                StopMonitor();
                return;
            }

            string address;
            string protocol;
            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            // Event notification only supported over inproc://
            if (protocol != Address.InProcProtocol)
                throw new ProtocolNotSupportedException($"In SocketBase.Monitor({addr},), protocol must be inproc");

            // Register events to monitor
            m_monitorEvents = events;

            m_monitorSocket = Ctx.CreateSocket(ZmqSocketType.Pair);

            // Never block context termination on pending event messages
            const int linger = 0;

            try
            {
                m_monitorSocket.SetSocketOption(ZmqSocketOption.Linger, linger);
            }
            catch (NetMQException)
            {
                StopMonitor();
                throw;
            }

            // Spawn the monitor socket endpoint
            try
            {
                m_monitorSocket.Bind(addr);
            }
            catch (NetMQException)
            {
                StopMonitor();
                throw;
            }
        }