Esempio n. 1
0
        /// <summary>
        /// Connect dials and bootstraps the nsqd connection
        /// (including IDENTIFY) and returns the IdentifyResponse
        /// </summary>
        public IdentifyResponse Connect()
        {
            var conn = Net.DialTimeout("tcp", _addr, _config.DialTimeout);

            _conn = (ITcpConn)conn;
            if (_conn == null)
            {
                throw new Exception("Net.DialTimeout returned null");
            }
            _r = conn;
            _w = conn;

            _conn.ReadTimeout  = _config.ReadTimeout;
            _conn.WriteTimeout = _config.WriteTimeout;

            try
            {
                Write(Protocol.MagicV2, 0, Protocol.MagicV2.Length);
            }
            catch (Exception ex)
            {
                _conn.Close();
                throw new Exception(string.Format("[{0}] failed to write magic - {1}", _addr, ex.Message), ex);
            }

            IdentifyResponse resp;

            try
            {
                resp = identify();
            }
            catch (ErrIdentify ex)
            {
                if (_addr.Contains(":4151"))
                {
                    throw new ErrIdentify("Error connecting to nsqd. It looks like you tried to connect to the HTTP port " +
                                          "(4151), use the TCP port (4150) instead.", ex);
                }
                else if (_addr.Contains(":4160") || _addr.Contains(":4161"))
                {
                    throw new ErrIdentify("Error connecting to nsqd. It looks like you tried to connect to nsqlookupd. " +
                                          "Producers must connect to nsqd over TCP (4150). Consumers can connect to nsqd over TCP (4150) using " +
                                          "Consumer.ConnectToNsqd or to nsqlookupd (4161) using Consumer.ConnectToNsqLookupd.", ex);
                }
                throw;
            }

            if (resp != null && resp.AuthRequired)
            {
                if (string.IsNullOrEmpty(_config.AuthSecret))
                {
                    log(LogLevel.Error, "Auth Required");
                    throw new Exception("Auth Required");
                }
                auth(_config.AuthSecret);
            }

            _wg.Add(2);
            _readLoopRunning = 1;
            GoFunc.Run(readLoop, "Conn:readLoop");
            GoFunc.Run(writeLoop, "Conn:writeLoop");
            return(resp);
        }
Esempio n. 2
0
        /// <summary>
        /// Connect dials and bootstraps the nsqd connection
        /// (including IDENTIFY) and returns the IdentifyResponse
        /// </summary>
        public IdentifyResponse Connect()
        {
            var conn = Net.DialTimeout("tcp", _addr, _config.DialTimeout);
            _conn = (ITcpConn)conn;
            if (_conn == null)
                throw new Exception("Net.DialTimeout returned null");
            _r = conn;
            _w = conn;

            _conn.ReadTimeout = _config.ReadTimeout;
            _conn.WriteTimeout = _config.WriteTimeout;

            try
            {
                Write(Protocol.MagicV2, 0, Protocol.MagicV2.Length);
            }
            catch (Exception ex)
            {
                _conn.Close();
                throw new Exception(string.Format("[{0}] failed to write magic - {1}", _addr, ex.Message), ex);
            }

            IdentifyResponse resp;
            try
            {
                resp = identify();
            }
            catch (ErrIdentify ex)
            {
                if (_addr.Contains(":4151"))
                {
                    throw new ErrIdentify("Error connecting to nsqd. It looks like you tried to connect to the HTTP port " +
                        "(4151), use the TCP port (4150) instead.", ex);
                }
                else if (_addr.Contains(":4160") || _addr.Contains(":4161"))
                {
                    throw new ErrIdentify("Error connecting to nsqd. It looks like you tried to connect to nsqlookupd. " +
                        "Producers must connect to nsqd over TCP (4150). Consumers can connect to nsqd over TCP (4150) using " +
                        "Consumer.ConnectToNsqd or to nsqlookupd (4161) using Consumer.ConnectToNsqLookupd.", ex);
                }
                throw;
            }

            if (resp != null && resp.AuthRequired)
            {
                if (string.IsNullOrEmpty(_config.AuthSecret))
                {
                    log(LogLevel.Error, "Auth Required");
                    throw new Exception("Auth Required");
                }
                auth(_config.AuthSecret);
            }

            _wg.Add(2);
            _readLoopRunning = 1;
            GoFunc.Run(readLoop, "Conn:readLoop");
            GoFunc.Run(writeLoop, "Conn:writeLoop");
            return resp;
        }