예제 #1
0
파일: SocketBase.cs 프로젝트: knocte/netmq
        public bool Bind(String addr)
        {
            if (m_ctxTerminated)
            {
                ZError.ErrorNumber = (ErrorNumber.ETERM);
                return false;
            }

            //  Process pending commands, if any.
            bool brc = ProcessCommands(0, false);
            if (!brc)
                return false;

            //  Parse addr_ string.
            Uri uri;
            try
            {
                uri = new Uri(addr);
            }
            catch (Exception e)
            {
                throw new ArgumentException(addr, e);
            }
            String protocol = uri.Scheme;
            String address = uri.Authority;
            String path = uri.AbsolutePath;
            if (string.IsNullOrEmpty(address))
                address = path;

            CheckProtocol(protocol);

            if (protocol.Equals("inproc"))
            {
                Ctx.Endpoint endpoint = new Ctx.Endpoint(this, m_options);
                bool rc = RegisterEndpoint(addr, endpoint);
                if (rc)
                {
                    // Save last endpoint URI
                    m_options.LastEndpoint = addr;
                }
                return rc;
            }
            if (protocol.Equals("pgm") || protocol.Equals("epgm"))
            {
                //  For convenience's sake, bind can be used interchageable with
                //  connect for PGM and EPGM transports.
                return Connect(addr);
            }

            //  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)
            {
                ZError.ErrorNumber = (ErrorNumber.EMTHREAD);
                return false;
            }

            if (protocol.Equals("tcp"))
            {
                TcpListener listener = new TcpListener(
                    ioThread, this, m_options);
                bool rc = listener.SetAddress(address);
                if (!rc)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ZError.ErrorNumber);
                    //LOG.error("Failed to Bind", ZError.exc());
                    return false;
                }

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

                AddEndpoint(addr, listener);
                return true;
            }

            if (protocol.Equals("ipc"))
            {
                IpcListener listener = new IpcListener(
                    ioThread, this, m_options);
                bool rc = listener.SetAddress(address);
                if (!rc)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ZError.ErrorNumber);
                    return false;
                }

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

                AddEndpoint(addr, listener);
                return true;
            }

            Debug.Assert(false);
            return false;
        }