Works with bytes that could be modified in different locations at any given time
Пример #1
0
 /// <summary>
 /// Writes a the bytes for a CS string in BSON format
 /// </summary>
 public static byte[] AsCString(string value)
 {
     var stream = new DynamicStream();
     stream.Append(AsString(value));
     stream.InsertAt(0, BitConverter.GetBytes(stream.Length));
     return stream.ToArray();
 }
Пример #2
0
        /// <summary>
        /// Generates the message to send
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            BsonDocument document = new BsonDocument();
            document["db.users.remove()"] = 1.0;

            stream.Append(document.ToBsonByteArray());
        }
Пример #3
0
        /// <summary>
        /// Prepares the body of the request to send to the server
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            //required ZERO int after header
            stream.Append(BsonTranslator.AsInt32(0));

            //apply the collection and database
            stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget()));

            //and generate each of the documents
            foreach (MongoDocument document in this.Documents) {
                stream.Append(document.ToBsonByteArray());
            }
        }
Пример #4
0
        /// <summary>
        /// Generates the body for this request
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            //required ZERO byte
            stream.Append(BsonTranslator.AsInt32(0));

            //the name of the collection
            stream.Append(BsonTranslator.AsString(Cursor.Query.GetDatabaseTarget()));

            //the total records to select
            stream.Append(BsonTranslator.AsInt32(Count));

            //required ZERO byte
            stream.Append(BsonTranslator.AsInt64(Cursor.Cursor));
        }
Пример #5
0
        /// <summary>
        /// Prepares the body of the request to send to the server
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            //required ZERO byte after header
            stream.Append(BsonTranslator.AsInt32(0));

            //apply the collection and database
            stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget()));

            //required ZERO byte before selector
            stream.Append(BsonTranslator.AsInt32(0));

            //then the actual selection
            stream.Append(this.Parameters.ToBsonByteArray());
        }
Пример #6
0
        /// <summary>
        /// Writes a binary object to the Mongo database
        /// </summary>
        public static byte[] AsBinary(byte[] value)
        {
            var stream = new DynamicStream(4);

            //write the kinds of stream this is
            //for now default to User-Defined
            stream.Append((byte)MongoBinaryTypes.UserDefined);

            //write the bytes and update the length
            stream.Append(value);
            stream.WriteAt(0, BitConverter.GetBytes(value.Length));

            //and return the final bytes to use
            return stream.ToArray();
        }
Пример #7
0
        /// <summary>
        /// Generates the command to send to the server
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            //default 'none' for the query options
            stream.Append(BsonTranslator.AsInt32(0));

            //apply the collection and database which should
            //be the database.$target format
            stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget()));

            //skip 0 and take 1 means to do a 'FindOne' command
            //which actually executes our command as code
            stream.Append(BsonTranslator.AsInt32(0));
            stream.Append(BsonTranslator.AsInt32(1));

            //insert the command value at the front of the request
            stream.Append(this.Arguments.ToBsonByteArray());
        }
Пример #8
0
        /// <summary>
        /// Renders the bytes required to create a document
        /// </summary>
        public override byte[] ToBsonByteArray()
        {
            //create the default size
            DynamicStream stream = new DynamicStream(5);

            //generate the bytes
            stream.InsertAt(4, base.ToBsonByteArray());

            //update the length
            stream.WriteAt(0, BsonTranslator.AsInt32(stream.Length));

            //and return the bytes to use
            return stream.ToArray();
        }
Пример #9
0
        //generates the entire request
        private void GenerateStream()
        {
            //if the stream has already been created then don't bother
            if (_output != null) { return; }

            //called just before the generation starts
            OnBeforeGenerateStream();

            //start building the header
            var stream = new DynamicStream(DefaultHeaderLength);
            stream.WriteAt(PositionOpCode, BitConverter.GetBytes((int)OpCode));

            //generate the bytes to use for the body
            GenerateBody(stream);

            //update the request/response IDs incase they change when building
            stream.WriteAt(PositionRequestId, BitConverter.GetBytes(RequestId));
            stream.WriteAt(PositionResponseId, BitConverter.GetBytes(ResponseId));

            //finally, remember to update the length
            stream.WriteAt(PositionRequestLength, BitConverter.GetBytes(stream.Length));

            //cache this value to use it later
            _output = stream;
        }
Пример #10
0
 /// <summary>
 /// Required function to generate the content for sending
 /// </summary>
 protected abstract void GenerateBody(DynamicStream stream);
Пример #11
0
 /// <summary>
 /// Resets the bytes for this request
 /// </summary>
 public void Reset()
 {
     _output = null;
 }
Пример #12
0
        //generates the entire request
        private void _GenerateStream()
        {
            //if the stream has already been created then don't bother
            if (this._Output is DynamicStream) { return; }

            //called just before the generation starts
            this.OnBeforeGenerateStream();

            //start building the header
            DynamicStream stream = new DynamicStream(DEFAULT_HEADER_LENGTH);
            stream.WriteAt(POSITION_OP_CODE, BitConverter.GetBytes((int)this.OpCode));

            //generate the bytes to use for the body
            this.GenerateBody(stream);

            //update the request/response IDs incase they change when building
            stream.WriteAt(POSITION_REQUEST_ID, BitConverter.GetBytes(this.RequestId));
            stream.WriteAt(POSITION_RESPONSE_ID, BitConverter.GetBytes(this.ResponseId));

            //finally, remember to update the length
            stream.WriteAt(POSITION_REQUEST_LENGTH, BitConverter.GetBytes(stream.Length));

            //cache this value to use it later
            this._Output = stream;
        }
Пример #13
0
 /// <summary>
 /// Resets the bytes for this request
 /// </summary>
 public void Reset()
 {
     this._Output = null;
 }
Пример #14
0
        /// <summary>
        /// Creates the body of the request to send
        /// </summary>
        protected override void GenerateBody(DynamicStream stream)
        {
            //determine the correct options to use
            stream.Append(BsonTranslator.AsInt32((int)this.Options));

            //apply the collection and database
            stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget()));

            //update the range information for this request
            stream.Append(BsonTranslator.AsInt32(this.Skip));
            stream.Append(BsonTranslator.AsInt32(this.Take));

            //generate the query
            stream.Append(this.Parameters.ToBsonByteArray());

            //generate the field selectors if there are any
            if (this.Fields.Count > 0) {

                //create the selector document
                BsonDocument select = new BsonDocument();
                for (int i = 0; i < this.Fields.Count; i++) {
                    select.Set(this.Fields[i], i + 1);
                }

                //append the bytes
                stream.Append(select.ToBsonByteArray());

            }
        }
Пример #15
0
 /// <summary>
 /// Creates the bytes for a regular string in BSON format
 /// </summary>
 public static byte[] AsString(string value)
 {
     var stream = new DynamicStream();
     stream.Append(Encoding.UTF8.GetBytes(value));
     stream.Append(0);
     return stream.ToArray();
 }
Пример #16
0
 /// <summary>
 /// Writes a regex in BSON format
 /// </summary>
 /// <param name="value">The regex instance</param>
 /// <returns>regex as bson byte arrray</returns>
 public static byte[] AsRegex(Regex value)
 {
     var optionsStr = "";
     var flags = value.Options;
     if ((flags & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) optionsStr += "i";
     if ((flags & RegexOptions.CultureInvariant) == RegexOptions.CultureInvariant) optionsStr += "l";
     if ((flags & RegexOptions.Multiline) == RegexOptions.Multiline) optionsStr += "m";
     if ((flags & RegexOptions.Singleline) == RegexOptions.Singleline) optionsStr += "s";
     if ((flags & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace) optionsStr += "x";
     var stream = new DynamicStream();
     stream.Append(AsString(value.ToString()));
     stream.Append(AsString(optionsStr));
     return stream.ToArray();
 }