You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
3.0 KiB

/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using MongoDB.Bson;
using MongoDB.Bson.IO;
namespace MongoDB.Driver.Internal {
internal abstract class MongoRequestMessage : MongoMessage, IDisposable {
#region private static fields
private static int lastRequestId = 0;
#endregion
#region protected fields
protected bool disposed = false;
protected BsonBuffer buffer;
protected bool disposeBuffer;
protected int messageStartPosition = -1; // start position in buffer for backpatching messageLength
#endregion
#region constructors
protected MongoRequestMessage(
MessageOpcode opcode
)
: this(opcode, null) {
}
protected MongoRequestMessage(
MessageOpcode opcode,
BsonBuffer buffer // not null if piggybacking this message onto an existing buffer
)
: base(opcode) {
this.buffer = buffer ?? new BsonBuffer();
this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer
this.requestId = Interlocked.Increment(ref lastRequestId);
}
#endregion
#region public propertieds
public BsonBuffer Buffer {
get { return buffer; }
}
#endregion
#region public methods
public void Dispose() {
if (!disposed) {
if (disposeBuffer) {
buffer.Dispose();
}
buffer = null;
disposed = true;
}
}
#endregion
#region internal methods
internal void WriteToBuffer() {
// this method is sometimes called more than once (see MongoConnection and MongoCollection)
if (messageStartPosition == -1) {
messageStartPosition = buffer.Position;
WriteMessageHeaderTo(buffer);
WriteBody();
BackpatchMessageLength();
}
}
#endregion
#region protected methods
protected void BackpatchMessageLength() {
messageLength = buffer.Position - messageStartPosition;
buffer.Backpatch(messageStartPosition, messageLength);
}
protected abstract void WriteBody();
#endregion
}
}