Exemple #1
0
        internal SafeModeResult SendMessage(
            MongoRequestMessage message,
            SafeMode safeMode
            )
        {
            if (state == MongoConnectionState.Closed)
            {
                throw new InvalidOperationException("Connection is closed");
            }
            lock (connectionLock) {
                message.WriteToBuffer();
                CommandDocument safeModeCommand = null;
                if (safeMode.Enabled)
                {
                    safeModeCommand = new CommandDocument {
                        { "getlasterror", 1 }, // use all lowercase for backward compatibility
                        { "fsync", true, safeMode.FSync },
                        { "w", safeMode.W, safeMode.W > 1 },
                        { "wtimeout", (int)safeMode.WTimeout.TotalMilliseconds, safeMode.W > 1 && safeMode.WTimeout != TimeSpan.Zero }
                    };
                    using (
                        var getLastErrorMessage = new MongoQueryMessage(
                            message.Server,
                            "admin.$cmd",  // collectionFullName
                            QueryFlags.None,
                            0,             // numberToSkip
                            1,             // numberToReturn
                            safeModeCommand,
                            null,          // fields
                            message.Buffer // piggy back on network transmission for message
                            )
                        ) {
                        getLastErrorMessage.WriteToBuffer();
                    }
                }

                try {
                    var networkStream = GetNetworkStream();
                    networkStream.WriteTimeout = (int)message.Server.Settings.SocketTimeout.TotalMilliseconds;
                    message.Buffer.WriteTo(networkStream);
                    messageCounter++;
                } catch (Exception ex) {
                    HandleException(ex);
                    throw;
                }

                SafeModeResult safeModeResult = null;
                if (safeMode.Enabled)
                {
                    var replyMessage     = ReceiveMessage <BsonDocument>(message.Server);
                    var safeModeResponse = replyMessage.Documents[0];
                    safeModeResult = new SafeModeResult();
                    safeModeResult.Initialize(safeModeCommand, safeModeResponse);

                    if (!safeModeResult.Ok)
                    {
                        var errorMessage = string.Format("Safemode detected an error: {0} (response: {1})", safeModeResult.ErrorMessage, safeModeResponse.ToJson());
                        throw new MongoSafeModeException(errorMessage, safeModeResult);
                    }
                    if (safeModeResult.HasLastErrorMessage)
                    {
                        var errorMessage = string.Format("Safemode detected an error: {0} (response: {1})", safeModeResult.LastErrorMessage, safeModeResponse.ToJson());
                        throw new MongoSafeModeException(errorMessage, safeModeResult);
                    }
                }

                return(safeModeResult);
            }
        }
        internal SafeModeResult SendMessage(MongoRequestMessage message, SafeMode safeMode)
        {
            if (_state == MongoConnectionState.Closed)
            {
                throw new InvalidOperationException("Connection is closed.");
            }
            lock (_connectionLock)
            {
                _requestId = message.RequestId;

                message.WriteToBuffer();
                CommandDocument safeModeCommand = null;
                if (safeMode.Enabled)
                {
                    safeModeCommand = new CommandDocument
                    {
                        { "getlasterror", 1 }, // use all lowercase for backward compatibility
                        { "fsync", true, safeMode.FSync },
                        { "j", true, safeMode.Journal },
                        { "w", safeMode.W, safeMode.W > 1 },
                        { "w", safeMode.WMode, safeMode.WMode != null },
                        { "wtimeout", (int)safeMode.WTimeout.TotalMilliseconds, safeMode.W > 1 && safeMode.WTimeout != TimeSpan.Zero }
                    };
                    // piggy back on network transmission for message
                    using (var getLastErrorMessage = new MongoQueryMessage(message.Buffer, message.WriterSettings, "admin.$cmd", QueryFlags.None, 0, 1, safeModeCommand, null))
                    {
                        getLastErrorMessage.WriteToBuffer();
                    }
                }

                try
                {
                    var networkStream = GetNetworkStream();
                    var writeTimeout  = (int)_serverInstance.Server.Settings.SocketTimeout.TotalMilliseconds;
                    if (writeTimeout != 0)
                    {
                        networkStream.WriteTimeout = writeTimeout;
                    }
                    message.Buffer.WriteTo(networkStream);
                    _messageCounter++;
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    throw;
                }

                SafeModeResult safeModeResult = null;
                if (safeMode.Enabled)
                {
                    var readerSettings = new BsonBinaryReaderSettings
                    {
                        GuidRepresentation = message.WriterSettings.GuidRepresentation,
                        MaxDocumentSize    = _serverInstance.MaxDocumentSize
                    };
                    var replyMessage     = ReceiveMessage <BsonDocument>(readerSettings, null);
                    var safeModeResponse = replyMessage.Documents[0];
                    safeModeResult = new SafeModeResult();
                    safeModeResult.Initialize(safeModeCommand, safeModeResponse);

                    if (!safeModeResult.Ok)
                    {
                        var errorMessage = string.Format(
                            "Safemode detected an error '{0}'. (response was {1}).",
                            safeModeResult.ErrorMessage, safeModeResponse.ToJson());
                        throw new MongoSafeModeException(errorMessage, safeModeResult);
                    }
                    if (safeModeResult.HasLastErrorMessage)
                    {
                        var errorMessage = string.Format(
                            "Safemode detected an error '{0}'. (Response was {1}).",
                            safeModeResult.LastErrorMessage, safeModeResponse.ToJson());
                        throw new MongoSafeModeException(errorMessage, safeModeResult);
                    }
                }

                return(safeModeResult);
            }
        }