Exemple #1
0
        private async Task <PacketSource> CreatePacketSource(IMessage message, CancellationToken token)
        {
            var messageId   = message.MessageId;
            var messageType = message.GetType().AssemblyQualifiedName;
            var messageData = new MemoryStream();

            Stream streamData = null;

            if (message is IStreamMessage streamMessage)
            {
                streamData = streamMessage.StreamFunc();
            }
            else if (message is IFileMessage fileMessage)
            {
                streamData = File.Open(fileMessage.Filename, FileMode.Open, FileAccess.Read);
            }

            // TODO: BsonDataWriter should be disposed!
            //   but it will close the stream.
            var bsonWriter = new BsonDataWriter(messageData);

            jsonSerializer.Serialize(bsonWriter, message);
            await bsonWriter.FlushAsync(token);

            messageData.Seek(0, SeekOrigin.Begin);

            return(new PacketSource(messageId, messageType)
            {
                MessageData = messageData,
                StreamData = streamData,
            });
        }
Exemple #2
0
        public async Task CloseAsync(CancellationToken cancellationToken)
        {
            using (await _lock.LockAsync(cancellationToken).ConfigureAwait(false))
            {
                if (null == _jsonWriter)
                {
                    return;
                }

                await _jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Exemple #3
0
        public async Task WriteValueOutsideOfObjectOrArrayAsync()
        {
            await ExceptionAssert.ThrowsAsync <JsonWriterException>(async() =>
            {
                MemoryStream stream = new MemoryStream();

                using (BsonDataWriter writer = new BsonDataWriter(stream))
                {
                    await writer.WriteValueAsync("test");
                    await writer.FlushAsync();
                }
            }, "Error writing String value. BSON must start with an Object or Array. Path ''.");
        }
        /// <summary>
        /// Saves the bson file.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="fileName">Name of the file.</param>
        private async Task saveBsonFile(Object data, string fileName)
        {
            Directory.CreateDirectory(_path);

            var name     = fileName;
            var fullName = Path.Combine(_path, name);

            var file = new FileStream(fullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

            using (var bson = new BsonDataWriter(file))
            {
                _serializer.Serialize(bson, data);
                await bson.FlushAsync();
            }
        }
Exemple #5
0
        public async Task WriteNestedArrayAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);
            await writer.WriteStartObjectAsync();

            await writer.WritePropertyNameAsync("_id");

            await writer.WriteValueAsync(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"));

            await writer.WritePropertyNameAsync("a");

            await writer.WriteStartArrayAsync();

            for (int i = 1; i <= 8; i++)
            {
                double value = (i != 5)
                    ? Convert.ToDouble(i)
                    : 5.78960446186581E+77d;

                await writer.WriteValueAsync(value);
            }
            await writer.WriteEndArrayAsync();

            await writer.WritePropertyNameAsync("b");

            await writer.WriteValueAsync("test");

            await writer.WriteEndObjectAsync();

            await writer.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
            string bson     = BytesToHex(ms.ToArray());

            Assert.AreEqual(expected, bson);
        }
Exemple #6
0
        public async Task WriteBytesAsync()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world!");

            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);
            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync("a");

            await writer.WriteValueAsync("b");

            await writer.WriteValueAsync(data);

            await writer.WriteEndArrayAsync();

            await writer.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
            string bson     = BytesToHex(ms.ToArray());

            Assert.AreEqual(expected, bson);

            BsonDataReader reader = new BsonDataReader(new MemoryStream(ms.ToArray()));

            reader.ReadRootValueAsArray = true;
            await reader.ReadAsync();

            await reader.ReadAsync();

            await reader.ReadAsync();

            await reader.ReadAsync();

            Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
            CollectionAssert.AreEquivalent(data, (byte[])reader.Value);
        }
Exemple #7
0
        public async Task WriteArrayBsonFromSiteAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);
            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync("a");

            await writer.WriteValueAsync("b");

            await writer.WriteValueAsync("c");

            await writer.WriteEndArrayAsync();

            await writer.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00";
            string bson     = BytesToHex(ms.ToArray());

            Assert.AreEqual(expected, bson);
        }