상속: MongoMessage, IDisposable
예제 #1
0
 internal void SendMessage(MongoRequestMessage message)
 {
     using (var stream = new MemoryStream())
     {
         message.WriteTo(stream);
         SendMessage(stream, message.RequestId);
     }
 }
 internal void SendMessage(MongoRequestMessage message)
 {
     using (var buffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true))
     {
         message.WriteTo(buffer);
         SendMessage(buffer, message.RequestId);
     }
 }
예제 #3
0
        private MongoReplyMessage <TDocument> GetReply(
            MongoConnection connection,
            MongoRequestMessage message
            )
        {
            connection.SendMessage(message, SafeMode.False); // safemode doesn't apply to queries
            var reply = connection.ReceiveMessage <TDocument>();

            openCursorId = reply.CursorId;
            return(reply);
        }
예제 #4
0
        private MongoReplyMessage <TDocument> GetReply(
            MongoRequestMessage message
            )
        {
            connection.SendMessage(message, SafeMode.False); // safemode doesn't apply to queries
            var reply = connection.ReceiveMessage <TDocument>(cursor.Server);

            openCursorId = reply.CursorId;
            if (openCursorId == 0)
            {
                ReleaseConnection();
            }
            return(reply);
        }
예제 #5
0
        internal WriteConcernResult SendMessage(MongoRequestMessage message, WriteConcern writeConcern, string databaseName)
        {
            if (_state == MongoConnectionState.Closed)
            {
                throw new InvalidOperationException("Connection is closed.");
            }
            lock (_connectionLock)
            {
                _requestId = message.RequestId;

                message.WriteToBuffer();
                CommandDocument getLastErrorCommand = null;
                if (writeConcern != null && writeConcern.Enabled)
                {
                    var fsync    = (writeConcern.FSync == null) ? null : (BsonValue)writeConcern.FSync;
                    var journal  = (writeConcern.Journal == null) ? null : (BsonValue)writeConcern.Journal;
                    var w        = (writeConcern.W == null) ? null : writeConcern.W.ToGetLastErrorWValue();
                    var wTimeout = (writeConcern.WTimeout == null) ? null : (BsonValue)(int)writeConcern.WTimeout.Value.TotalMilliseconds;

                    getLastErrorCommand = new CommandDocument
                    {
                        { "getlasterror", 1 }, // use all lowercase for backward compatibility
                        { "fsync", fsync, fsync != null },
                        { "j", journal, journal != null },
                        { "w", w, w != null },
                        { "wtimeout", wTimeout, wTimeout != null }
                    };
                    // piggy back on network transmission for message
                    using (var getLastErrorMessage = new MongoQueryMessage(message.Buffer, message.WriterSettings, databaseName + ".$cmd", QueryFlags.None, 0, 1, getLastErrorCommand, null))
                    {
                        getLastErrorMessage.WriteToBuffer();
                    }
                }

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

                WriteConcernResult writeConcernResult = null;
                if (writeConcern != null && writeConcern.Enabled)
                {
                    var readerSettings = new BsonBinaryReaderSettings
                    {
                        GuidRepresentation = message.WriterSettings.GuidRepresentation,
                        MaxDocumentSize    = _serverInstance.MaxDocumentSize
                    };
                    var replyMessage         = ReceiveMessage <BsonDocument>(readerSettings, null);
                    var getLastErrorResponse = replyMessage.Documents[0];
                    writeConcernResult = new WriteConcernResult();
                    writeConcernResult.Initialize(getLastErrorCommand, getLastErrorResponse);

                    if (!writeConcernResult.Ok)
                    {
                        var errorMessage = string.Format(
                            "WriteConcern detected an error '{0}'. (response was {1}).",
                            writeConcernResult.ErrorMessage, getLastErrorResponse.ToJson());
                        throw new WriteConcernException(errorMessage, writeConcernResult);
                    }
                    if (writeConcernResult.HasLastErrorMessage)
                    {
                        var errorMessage = string.Format(
                            "WriteConcern detected an error '{0}'. (Response was {1}).",
                            writeConcernResult.LastErrorMessage, getLastErrorResponse.ToJson());
                        throw new WriteConcernException(errorMessage, writeConcernResult);
                    }
                }

                return(writeConcernResult);
            }
        }
예제 #6
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);
            }
        }
예제 #7
0
        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);
            }
        }