// static methods
 public static Task SendMessageAsync(this IConnection connection, RequestMessage message, TimeSpan timeout, CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(connection, "connection");
     return connection.SendMessagesAsync(new[] { message }, timeout, cancellationToken);
 }
 public static Task SendMessageAsync(this IConnection connection, RequestMessage message, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(connection, nameof(connection));
     return connection.SendMessagesAsync(new[] { message }, messageEncoderSettings, cancellationToken);
 }
        private void ProcessInsertMessage(RequestMessage message, Queue<RequestMessage> messageQueue, ConnectionId connectionId, InsertMessageBinaryEncoder<RawBsonDocument> encoder, Stopwatch stopwatch)
        {
            var commandName = "insert";
            var operationId = EventContext.OperationId;
            var requestId = message.RequestId;
            var expectedResponseType = ExpectedResponseType.None;
            int numberOfDocuments = 0;
            int gleRequestId;
            WriteConcern writeConcern;
            if (TryGetWriteConcernFromGLE(messageQueue, out gleRequestId, out writeConcern))
            {
                requestId = gleRequestId;
                expectedResponseType = ExpectedResponseType.GLE;
            }

            if (_startedEvent != null)
            {
                // InsertMessage is generic, and we don't know the generic type... 
                // Plus, for this we really want BsonDocuments, not whatever the generic type is.
                var decodedMessage = encoder.ReadMessage();

                var documents = decodedMessage.DocumentSource.GetRemainingItems().ToList();
                numberOfDocuments = documents.Count;
                try
                {
                    var command = new BsonDocument
                    {
                        { commandName, decodedMessage.CollectionNamespace.CollectionName },
                        { "documents", new BsonArray(documents) },
                        { "ordered", !decodedMessage.ContinueOnError }
                    };

                    if (writeConcern == null)
                    {
                        command["writeConcern"] = WriteConcern.Unacknowledged.ToBsonDocument();
                    }
                    else if (!writeConcern.IsServerDefault)
                    {
                        command["writeConcern"] = writeConcern.ToBsonDocument();
                    }

                    var @event = new CommandStartedEvent(
                        commandName,
                        command,
                        decodedMessage.CollectionNamespace.DatabaseNamespace,
                        operationId,
                        requestId,
                        connectionId);

                    _startedEvent(@event);
                }
                finally
                {
                    documents.ForEach(d => d.Dispose());
                }
            }

            if (_shouldTrackState)
            {
                _state.TryAdd(requestId, new CommandState
                {
                    CommandName = commandName,
                    OperationId = operationId,
                    Stopwatch = stopwatch,
                    ExpectedResponseType = expectedResponseType,
                    NumberOfInsertedDocuments = numberOfDocuments
                });
            }
        }
 // static methods
 public static void SendMessage(this IConnection connection, RequestMessage message, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(connection, nameof(connection));
     connection.SendMessages(new[] { message }, messageEncoderSettings, cancellationToken);
 }