internal void Connect(
            bool slaveOk
            )
        {
            if (state != MongoServerState.Disconnected)
            {
                var message = string.Format("MongoServerInstance.Connect called when state is: {0}", state);
                throw new InvalidOperationException(message);
            }

            State            = MongoServerState.Connecting;
            connectException = null;
            try {
                endPoint = address.ToIPEndPoint(server.Settings.AddressFamily);

                var connectionPool = new MongoConnectionPool(this);
                try {
                    var connection = connectionPool.AcquireConnection(null);
                    try {
                        try {
                            var isMasterCommand = new CommandDocument("ismaster", 1);
                            isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);
                        } catch (MongoCommandException ex) {
                            isMasterResult = ex.CommandResult;
                            throw;
                        }

                        isPrimary   = isMasterResult.Response["ismaster", false].ToBoolean();
                        isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                        isPassive   = isMasterResult.Response["passive", false].ToBoolean();
                        isArbiter   = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                        if (!isPrimary && !slaveOk)
                        {
                            throw new MongoConnectionException("Server is not a primary and SlaveOk is false");
                        }

                        maxDocumentSize  = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                        maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize
                    } finally {
                        connectionPool.ReleaseConnection(connection);
                    }
                } catch {
                    connectionPool.Close();
                    throw;
                }

                State = MongoServerState.Connected;
                this.connectionPool = connectionPool;
            } catch (Exception ex) {
                State            = MongoServerState.Disconnected;
                connectException = ex;
                throw;
            }
        }
Beispiel #2
0
        // public methods
        /// <summary>
        /// Gets the IP end point of this server instance.
        /// </summary>
        /// <returns>The IP end point of this server instance.</returns>
        public IPEndPoint GetIPEndPoint()
        {
            // use a lock free algorithm because DNS lookups are rare and concurrent lookups are tolerable
            // the intermediate variable is important to avoid race conditions
            var ipEndPoint = _ipEndPoint;

            if (ipEndPoint == null)
            {
                ipEndPoint  = _address.ToIPEndPoint(_server.Settings.AddressFamily);
                _ipEndPoint = ipEndPoint;
            }
            return(ipEndPoint);
        }
        internal void Connect(bool slaveOk)
        {
            // Console.WriteLine("MongoServerInstance[{0}]: Connect(slaveOk={1}) called.", sequentialId, slaveOk);
            lock (serverInstanceLock)
            {
                // note: don't check that state is Disconnected here
                // when reconnecting to a replica set state can transition from Connected -> Connecting -> Connected

                SetState(MongoServerState.Connecting);
                connectException = null;
                try
                {
                    endPoint = address.ToIPEndPoint(server.Settings.AddressFamily);

                    try
                    {
                        var connection = connectionPool.AcquireConnection(null);
                        try
                        {
                            VerifyState(connection);
                            if (!isPrimary && !slaveOk)
                            {
                                throw new MongoConnectionException("Server is not a primary and SlaveOk is false.");
                            }
                        }
                        finally
                        {
                            connectionPool.ReleaseConnection(connection);
                        }
                    }
                    catch
                    {
                        connectionPool.Clear();
                        throw;
                    }

                    SetState(MongoServerState.Connected);
                }
                catch (Exception ex)
                {
                    SetState(MongoServerState.Disconnected);
                    connectException = ex;
                    throw;
                }
            }
        }