Exemplo n.º 1
0
        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);

            if (protocol.Equals(Address.InProcProtocol))
            {
                var endpoint = new Ctx.Endpoint(this, m_options);

                bool addressRegistered = RegisterEndpoint(addr, endpoint);

                if (!addressRegistered)
                {
                    string xMsg = string.Format("Cannot bind address ( {0} ) - already in use.", addr);
                    throw new AddressAlreadyInUseException(xMsg);
                }

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }
            if ((protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) && (
                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;
            }

            //  Remaining transports require to be run in an I/O thread, so at this
            //  point we'll choose one.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);
            if (ioThread == null)
            {
                throw NetMQException.Create(ErrorCode.EmptyThread);
            }

            if (protocol.Equals(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
                    var host = address.Substring(0, address.IndexOf(':'));
                    addr = "tcp://" + host + ":" + m_port;
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(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);

                return;
            }

            if (protocol.Equals(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;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);
                return;
            }

            Debug.Assert(false);
            throw new FaultException(string.Format("SocketBase.Bind({0}) failure.", addr));
        }
Exemplo n.º 2
0
        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);

            if (protocol.Equals(Address.InProcProtocol))
            {
                var endpoint = new Ctx.Endpoint(this, m_options);

                bool addressRegistered = RegisterEndpoint(addr, endpoint);

                if (!addressRegistered)
                {
                    string xMsg = String.Format("Cannot bind address ( {0} ) - already in use.", addr);
                    throw new AddressAlreadyInUseException(xMsg);
                }

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }
            if ((protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) && (
                    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;
            }

            //  Remaining transports require to be run in an I/O thread, so at this
            //  point we'll choose one.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
            {
                throw NetMQException.Create(ErrorCode.EmptyThread);
            }

            if (protocol.Equals(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
                    var host = address.Substring(0, address.IndexOf(':'));
                    addr = "tcp://" + host + ":" + m_port;
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(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);

                return;
            }

            if (protocol.Equals(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;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);
                return;
            }

            Debug.Assert(false);
            throw new FaultException(String.Format("SocketBase.Bind({0}) failure.", addr));
        }
Exemplo n.º 3
0
        public void Bind(String addr)
        {
            CheckContextTerminated();

            //  Process pending commands, if any.
            ProcessCommands(0, false);

            string protocol;
            string address;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol.Equals(Address.InProcProtocol))
            {
                Ctx.Endpoint endpoint = new Ctx.Endpoint(this, m_options);

                bool addressRegistered = RegisterEndpoint(addr, endpoint);

                if (!addressRegistered)
                {
                    throw new AddressAlreadyInUseException("Cannot bind address, address already in use");
                }

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }
            if ((protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) && (
                    m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub))
            {
                //  For convenience's sake, bind can be used interchageable with
                //  connect for PGM and EPGM transports.
                Connect(addr);
                return;
            }

            //  Remaining trasnports require to be run in an I/O thread, so at this
            //  point we'll choose one.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);

            if (ioThread == null)
            {
                throw NetMQException.Create(ErrorCode.EmptyThread);
            }

            if (protocol.Equals(Address.TcpProtocol))
            {
                TcpListener listener = new TcpListener(
                    ioThread, this, m_options);

                try
                {
                    listener.SetAddress(address);
                    m_port = listener.Port;
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol))
            {
                PgmListener 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);

                return;
            }

            if (protocol.Equals(Address.IpcProtocol))
            {
                IpcListener 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;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);
                return;
            }

            Debug.Assert(false);
            throw new FaultException();
        }