Ejemplo n.º 1
0
		/// <summary>
		/// Creates new XslReader instance with given <see cref="XslCompiledTransform"/>, 
		/// mode (multithreaded/singlethreaded) and initial buffer size. The buffer will be
		/// expanded if necessary to be able to store any element start tag with all its 
		/// attributes.
		/// </summary>
		/// <param name="xslTransform">Loaded <see cref="XslCompiledTransform"/> object</param>
		/// <param name="multiThread">Defines in which mode (multithreaded or singlethreaded)
		/// this instance of XslReader will operate</param>
		/// <param name="initialBufferSize">Initial buffer size (number of nodes, not bytes)</param>
		public XslReader(XslCompiledTransform xslTransform, bool multiThread, int initialBufferSize)
		{
			this.xslCompiledTransform = xslTransform;
			this.multiThread = multiThread;
			this.initialBufferSize = initialBufferSize;

			nameTable = new NameTable();
			pipe = this.multiThread ? new TokenPipeMultiThread(initialBufferSize) : new TokenPipe(initialBufferSize);
			writer = new BufferWriter(pipe, nameTable);
			scope = new ScopeManager(nameTable);
			SetUndefinedState(ReadState.Initial);
		}
Ejemplo n.º 2
0
        /// <summary>
        /// Receiver for the combined VAD signal and audio data.
        /// </summary>
        /// <param name="data">A message containing the combined VAD signal and audio data.</param>
        /// <param name="e">The message envelope.</param>
        /// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
        protected override async Task ReceiveAsync(ValueTuple <AudioBuffer, bool> data, Envelope e)
        {
            byte[] audioData = data.Item1.Data;
            bool   hasSpeech = data.Item2;

            if (this.lastAudioOriginatingTime == default)
            {
                this.lastAudioOriginatingTime = e.OriginatingTime - data.Item1.Duration;
            }

            var previousAudioOriginatingTime = this.lastAudioOriginatingTime;

            this.lastAudioOriginatingTime = e.OriginatingTime;

            // Throw if a fatal error has occurred in the OnConversationError event handler
            if (this.fatalError)
            {
                if (this.conversationError != null)
                {
                    var error = this.conversationError;
                    this.conversationError = null;
                    throw error;
                }

                // Stop processing until the pipeline terminates
                return;
            }

            if (hasSpeech)
            {
                this.lastAudioContainingSpeechTime = e.OriginatingTime;

                bool newSession = false;
                if (!this.lastAudioContainedSpeech)
                {
                    // queue a new recognition task
                    this.currentRecognitionTask = new SpeechRecognitionTask {
                        SpeechStartTime = previousAudioOriginatingTime
                    };
                    this.pendingRecognitionTasks.Enqueue(this.currentRecognitionTask);

                    // create a new session when sending the first audio packet
                    newSession = true;
                }

                // Send the audio data to the cloud
                await this.speechRecognitionClient.SendAudioAsync(audioData, this.cancellationTokenSource.Token, newSession);

                // Add audio to the current utterance queue so we can reconstruct it in the recognition result later
                this.currentQueue.Enqueue(data.DeepClone(this.In.Recycler));
            }

            // If this is the last audio packet containing speech
            if (!hasSpeech && this.lastAudioContainedSpeech)
            {
                // If this is the first audio packet containing no speech, use the time of the previous audio packet
                // as the end of the actual speech, since that is the last packet that contained any speech.
                var lastVADSpeechEndTime = this.lastAudioContainingSpeechTime;

                // update the latest in-progress recognition

                // Allocate a buffer large enough to hold the buffered audio
                BufferWriter bw = new BufferWriter(this.currentQueue.Sum(b => b.Item1.Length));

                // Get the audio associated with the recognized text from the current queue.
                ValueTuple <AudioBuffer, bool> buffer;
                while (this.currentQueue.Count > 0)
                {
                    buffer = this.currentQueue.Dequeue();
                    bw.Write(buffer.Item1.Data);

                    // We are done with this buffer so enqueue it for recycling
                    this.In.Recycle(buffer);
                }

                // Save the buffered audio
                this.currentRecognitionTask.Audio         = new AudioBuffer(bw.Buffer, this.Configuration.InputFormat);
                this.currentRecognitionTask.SpeechEndTime = lastVADSpeechEndTime;

                // Call EndAudio to signal that this is the last packet
                await this.speechRecognitionClient.SendEndAudioAsync(this.cancellationTokenSource.Token);
            }

            // Remember last audio state.
            this.lastAudioContainedSpeech = hasSpeech;
        }
Ejemplo n.º 3
0
        public void Buffer_Write_Numbers()
        {
            var source = new BufferSlice(new byte[1000], 0, 1000);

            // numbers
            using (var w = new BufferWriter(source))
            {
                // max values
                w.Write(int.MaxValue);
                w.Write(uint.MaxValue);
                w.Write(long.MaxValue);
                w.Write(double.MaxValue);
                w.Write(decimal.MaxValue);

                // min values
                w.Write(int.MinValue);
                w.Write(uint.MinValue);
                w.Write(long.MinValue);
                w.Write(double.MinValue);
                w.Write(decimal.MinValue);

                // zero values
                w.Write(0);  // int
                w.Write(0u); // uint
                w.Write(0L); // long
                w.Write(0d); // double
                w.Write(0m); // decimal

                // fixed values
                w.Write(1990);  // int
                w.Write(1990u); // uint
                w.Write(1990L); // long
                w.Write(1990d); // double
                w.Write(1990m); // decimal
            }

            var p = 0;

            source.ReadInt32(p).Should().Be(int.MaxValue);
            p += 4;
            source.ReadUInt32(p).Should().Be(uint.MaxValue);
            p += 4;
            source.ReadInt64(p).Should().Be(long.MaxValue);
            p += 8;
            source.ReadDouble(p).Should().Be(double.MaxValue);
            p += 8;
            source.ReadDecimal(p).Should().Be(decimal.MaxValue);
            p += 16;

            source.ReadInt32(p).Should().Be(int.MinValue);
            p += 4;
            source.ReadUInt32(p).Should().Be(uint.MinValue);
            p += 4;
            source.ReadInt64(p).Should().Be(long.MinValue);
            p += 8;
            source.ReadDouble(p).Should().Be(double.MinValue);
            p += 8;
            source.ReadDecimal(p).Should().Be(decimal.MinValue);
            p += 16;

            source.ReadInt32(p).Should().Be(0);
            p += 4;
            source.ReadUInt32(p).Should().Be(0u);
            p += 4;
            source.ReadInt64(p).Should().Be(0L);
            p += 8;
            source.ReadDouble(p).Should().Be(0d);
            p += 8;
            source.ReadDecimal(p).Should().Be(0m);
            p += 16;

            source.ReadInt32(p).Should().Be(1990);
            p += 4;
            source.ReadUInt32(p).Should().Be(1990u);
            p += 4;
            source.ReadInt64(p).Should().Be(1990L);
            p += 8;
            source.ReadDouble(p).Should().Be(1990d);
            p += 8;
            source.ReadDecimal(p).Should().Be(1990m);
            p += 16;
        }
 private static void Render(ref BufferWriter writer, StructuredReport report)
 {
     writer.Write(report.ToString());
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Write transport-specific parameter (port number).
 /// </summary>
 /// <param name="writer">Buffer writer to which to write.</param>
 public void WriteTransportParams(BufferWriter writer)
 {
     writer.Write(this.port);
 }
Ejemplo n.º 6
0
 protected override void WriteXmsHeaders(ref BufferWriter writer, ref GetFileRequest arguments)
 {
     writer.WriteHeader("x-ms-date", Time, 'R');
     writer.WriteHeader("x-ms-version", "2017-04-17");
 }
Ejemplo n.º 7
0
 protected virtual void WriteOtherHeaders(ref BufferWriter writer, ref T arguments)
 {
     writer.WriteHeader("Content-Length", arguments.ContentLength);
     writer.WriteHeader("Host", arguments.Client.Host);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Wrtie any BsonValue. Use 1 byte for data type, 1 byte for length (optional), 0-255 bytes to value.
        /// For document or array, use BufferWriter
        /// </summary>
        public static void WriteIndexKey(this BufferSlice buffer, BsonValue value, int offset)
        {
            DEBUG(IndexNode.GetKeyLength(value, true) <= MAX_INDEX_KEY_LENGTH, $"index key must have less than {MAX_INDEX_KEY_LENGTH} bytes");

            if (value.IsString)
            {
                var str       = value.AsString;
                var strLength = (ushort)Encoding.UTF8.GetByteCount(str);

                ExtendedLengthHelper.WriteLength(BsonType.String, strLength, out var typeByte, out var lengthByte);

                buffer[offset++] = typeByte;
                buffer[offset++] = lengthByte;
                buffer.Write(str, offset);
            }
            else if (value.IsBinary)
            {
                var arr = value.AsBinary;

                ExtendedLengthHelper.WriteLength(BsonType.Binary, (ushort)arr.Length, out var typeByte, out var lengthByte);

                buffer[offset++] = typeByte;
                buffer[offset++] = lengthByte;
                buffer.Write(arr, offset);
            }
            else
            {
                buffer[offset++] = (byte)value.Type;

                switch (value.Type)
                {
                case BsonType.Null:
                case BsonType.MinValue:
                case BsonType.MaxValue:
                    break;

                case BsonType.Int32: buffer.Write(value.AsInt32, offset); break;

                case BsonType.Int64: buffer.Write(value.AsInt64, offset); break;

                case BsonType.Double: buffer.Write(value.AsDouble, offset); break;

                case BsonType.Decimal: buffer.Write(value.AsDecimal, offset); break;

                case BsonType.Document:
                    using (var w = new BufferWriter(buffer))
                    {
                        w.Skip(offset);     // skip offset from buffer
                        w.WriteDocument(value.AsDocument, true);
                    }
                    break;

                case BsonType.Array:
                    using (var w = new BufferWriter(buffer))
                    {
                        w.Skip(offset);     // skip offset from buffer
                        w.WriteArray(value.AsArray, true);
                    }
                    break;

                case BsonType.ObjectId: buffer.Write(value.AsObjectId, offset); break;

                case BsonType.Guid: buffer.Write(value.AsGuid, offset); break;

                case BsonType.Boolean: buffer[offset] = (value.AsBoolean) ? (byte)1 : (byte)0; break;

                case BsonType.DateTime: buffer.Write(value.AsDateTime, offset); break;

                default: throw new NotImplementedException();
                }
            }
        }
Ejemplo n.º 9
0
        private void MetaClientBackground()
        {
            Guid guid = Guid.Empty;

            try
            {
                var metaClient = new TcpClient();
                metaClient.Connect(this.host, this.port);
                var metaStream = metaClient.GetStream();

                // send protocol version and replay interval
                var buffer = new byte[256];
                var writer = new BufferWriter(buffer);
                writer.Write(RemoteExporter.ProtocolVersion);
                writer.Write(this.replayRemoteLatestStart ? -1 : this.replayStart);
                writer.Write(this.replayEnd);
                metaStream.Write(writer.Buffer, 0, writer.Position);

                // receive ID and transport info
                var reader = new BufferReader(buffer);
                Transport.Read(reader.Buffer, 4, metaStream);
                var len = reader.ReadInt32();
                reader.Reset(len);
                Transport.Read(reader.Buffer, len, metaStream);
                var id = new byte[16];
                reader.Read(id, id.Length);
                var transport = Transport.TransportOfName(reader.ReadString());
                transport.ReadTransportParams(reader);
                guid = new Guid(id);
                Trace.WriteLine($"RemoteImporter meta client connected (ID={guid})");

                // process metadata updates
                while (!this.disposed)
                {
                    reader.Reset(sizeof(int));
                    Transport.Read(reader.Buffer, sizeof(int), metaStream);
                    var metalen = reader.ReadInt32();
                    if (metalen > 0)
                    {
                        reader.Reset(metalen);
                        Transport.Read(reader.Buffer, metalen, metaStream);
                        var meta = Metadata.Deserialize(reader);
                        if (meta.Kind == MetadataKind.StreamMetadata)
                        {
                            try
                            {
                                this.storeWriter.OpenStream((PsiStreamMetadata)meta);
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError($"RemoteImporter meta update duplicate stream - expected after reconnect (Name={meta.Name}, ID={guid}, Error={ex.Message})");
                            }
                        }
                        else
                        {
                            this.storeWriter.WriteToCatalog(meta);
                        }

                        Trace.WriteLine($"RemoteImporter meta update (Name={meta.Name}, ID={guid})");
                    }
                    else
                    {
                        // "intermission" in meta updates
                        this.Importer = this.importerThunk(this.storeWriter.Name); // now that we have a populated catalog
                        this.StartDataClient(guid, transport);
                        this.connected.Set();
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"RemoteImporter meta connection error (Message={ex.Message}, ID={guid})");
                this.StartMetaClient(); // restart
            }
        }
Ejemplo n.º 10
0
        public void PsiStreamMetadataTest()
        {
            var messageCount             = 42L;
            var avgMessageSize           = 1234L;
            var avgLatency               = 70000L;
            var messageSizeCumulativeSum = messageCount * avgMessageSize;
            var latencyCumulativeSum     = messageCount * avgLatency;

            // serialize manually, simulating older/newer versions
            (byte[] Buffer, int Size) Serialize(int version, bool isPolymorphic)
            {
                var writer = new BufferWriter(0);

                writer.Write("SomeName");                                                    // Name
                writer.Write(123);                                                           // ID
                writer.Write("SomeFakeTypeName");                                            // TypeName
                writer.Write(version);                                                       // Version
                writer.Write("SomeFakeSerializerTypeName");                                  // SerializerTypeName
                writer.Write(7);                                                             // SerializerVersion
                writer.Write((ushort)(isPolymorphic ? StreamMetadataFlags.Polymorphic : 0)); // CustomFlags
                writer.Write((ushort)MetadataKind.StreamMetadata);                           // MetadataKind
                writer.Write(new DateTime(1969, 4, 2));                                      // OpenedTime
                writer.Write(new DateTime(2070, 1, 1));                                      // ClosedTime
                if (version > 1)
                {
                    writer.Write(messageCount);             // MessageCount
                    writer.Write(messageSizeCumulativeSum); // MessageSizeCumulativeSum
                    writer.Write(latencyCumulativeSum);     // LatencyCumulativeSum
                }
                else
                {
                    writer.Write((int)messageCount); // MessageCount
                }

                writer.Write(new DateTime(1971, 11, 3));  // FirstMessageCreationTime
                writer.Write(new DateTime(1972, 10, 12)); // LastMessageCreationTime
                writer.Write(new DateTime(1971, 2, 3));   // FirstMessageOriginatingTime
                writer.Write(new DateTime(1972, 1, 12));  // LastMessageOriginatingTime
                if (version <= 1)
                {
                    writer.Write((int)avgMessageSize);  // AverageMessageSize
                    writer.Write((int)avgLatency / 10); // AverageLatency in *microseconds*
                }

                if (isPolymorphic)
                {
                    writer.Write(2);                   // RuntimeTypes.Count
                    writer.Write(1);                   // Key
                    writer.Write("SomeFakePolyType1"); // Value
                    writer.Write(2);                   // Key
                    writer.Write("SomeFakePolyType2"); // Value
                }

                if (version > 0)
                {
                    var payload = new byte[] { 1, 2, 3 };
                    writer.Write(payload.GetType().FullName);                       // SupplementalMetadataTypeName
                    writer.Write(payload.Length + 4 /* prefix */ + 4 /* length */); // supplementalMetadataBytes.Length
                    writer.Write(SerializationHandler.RefPrefixNew);
                    writer.Write(payload.Length);
                    writer.Write(payload); // bytes
                }

                return(writer.Buffer, writer.Size);
            }

            // verify all versions deserialize/serialize correctly with migration to latest version
            void TestAllVersions(Action <PsiStreamMetadata> verify)
            {
                // verify metadata instance
                void VerifyMeta(PsiStreamMetadata meta, int version, bool isPolymorphic)
                {
                    verify(meta);

                    Assert.AreEqual(isPolymorphic, meta.IsPolymorphic);
                    if (isPolymorphic)
                    {
                        Assert.AreEqual(2, meta.RuntimeTypes.Count);
                        Assert.AreEqual("SomeFakePolyType1", meta.RuntimeTypes[1]);
                        Assert.AreEqual("SomeFakePolyType2", meta.RuntimeTypes[2]);
                    }

                    Assert.AreEqual(2, meta.Version); // expect upgraded version
                    if (version > 0)
                    {
                        var expected = new byte[] { 1, 2, 3 };
                        Assert.AreEqual(expected.GetType().FullName, meta.SupplementalMetadataTypeName);
                        Assert.IsTrue(Enumerable.SequenceEqual(expected, meta.GetSupplementalMetadata <byte[]>()));
                    }
                    else
                    {
                        Assert.AreEqual(null, meta.SupplementalMetadataTypeName);
                    }
                }

                // verify with isPolymorphic=true/false and versions=0..2
                for (var pass = 0; pass < 2; pass++)
                {
                    var isPolymorphic = pass > 0;
                    for (var version = 0; version < 3; version++)
                    {
                        // manually serialize (including older formats)
                        var(buffer, size) = Serialize(version, isPolymorphic);
                        var reader = new BufferReader(buffer, size);
                        var meta   = (PsiStreamMetadata)Metadata.Deserialize(reader);
                        VerifyMeta(meta, version, isPolymorphic);

                        // test serialization via round-trip
                        var writer = new BufferWriter(0);
                        meta.Serialize(writer);
                        var roundtrip = (PsiStreamMetadata)Metadata.Deserialize(new BufferReader(writer));
                        VerifyMeta(roundtrip, version, isPolymorphic);
                    }
                }
            }

            TestAllVersions(meta =>
            {
                Assert.AreEqual("SomeName", meta.Name);
                Assert.AreEqual(123, meta.Id);
                Assert.AreEqual(123, meta.Id);
                Assert.AreEqual("SomeFakeTypeName", meta.TypeName);
                Assert.AreEqual("SomeFakeSerializerTypeName", meta.SerializerTypeName);
                Assert.AreEqual(7, meta.SerializerVersion);
                Assert.AreEqual(new DateTime(1969, 4, 2), meta.OpenedTime);
                Assert.AreEqual(new DateTime(2070, 1, 1), meta.ClosedTime);
                Assert.AreEqual(messageCount, meta.MessageCount);
                Assert.AreEqual(messageSizeCumulativeSum, meta.MessageSizeCumulativeSum);
                Assert.AreEqual(latencyCumulativeSum, meta.LatencyCumulativeSum);
                Assert.AreEqual(new DateTime(1971, 11, 3), meta.FirstMessageCreationTime);
                Assert.AreEqual(new DateTime(1972, 10, 12), meta.LastMessageCreationTime);
                Assert.AreEqual(new DateTime(1971, 2, 3), meta.FirstMessageOriginatingTime);
                Assert.AreEqual(new DateTime(1972, 1, 12), meta.LastMessageOriginatingTime);
                Assert.AreEqual(avgMessageSize, meta.AverageMessageSize);
                Assert.AreEqual(avgLatency / TimeSpan.TicksPerMillisecond, meta.AverageMessageLatencyMs);
            });
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Consumes the <paramref name="stream"/> to the end into a <see cref="BufferWriter"/> and disposes the stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public static ValueTask <BufferWriter> Consume(this Stream stream, CancellationToken ct = default) // TODO dispose stream?
        {
            const int minReadCapacity = 1024;

            var knownSize = -1;

            if (stream.CanSeek)
            {
                knownSize = checked ((int)stream.Length);
            }

            var bw = BufferWriter.Create(knownSize);

            // ReSharper disable RedundantAssignment
            ValueTask <int> t          = default;
            bool            finishSync = false;

            // ReSharper restore RedundantAssignment
#if NETCOREAPP
            while (true)
            {
                bw.EnsureCapacity(minReadCapacity);

                t = stream.ReadAsync(bw.FreeMemory, ct);
                if (t.IsCompletedSuccessfully)
                {
                    bw.Advance(t.Result);
                    if (t.Result == 0 || (knownSize >= 0 && t.Result == knownSize))
                    {
                        stream.Dispose();
                        return(new ValueTask <BufferWriter>(bw));
                    }
                    // Continue, we do not know the size or have read partially. Cannot tell that at the end of stream until ReadAsync returns 0.
                }
                else
                {
                    finishSync = true;
                    break;
                }
            }
#endif

            return(ConsumeAsync());

            async ValueTask <BufferWriter> ConsumeAsync()
            {
                byte[] tempBuff = null;
                try
                {
                    do
                    {
                        if (finishSync)
                        {
#if NETCOREAPP
                            await t;
#endif
                            // we have written to free memory but not advanced yet
                            bw.Advance(t.Result);

                            if (t.Result == 0 || (knownSize >= 0 && t.Result == knownSize))
                            {
                                return(bw);
                            }
                        }

                        bw.EnsureCapacity(minReadCapacity);
#if NETCOREAPP
                        t = stream.ReadAsync(bw.FreeMemory, ct);
#else
                        if (tempBuff == null)
                        {
                            tempBuff = BufferPool <byte> .Rent(bw.FreeCapacity);
                        }
                        var bytesRead = await stream.ReadAsync(tempBuff, 0, tempBuff.Length, ct);

                        if (bytesRead > 0)
                        {
                            tempBuff.AsSpan().Slice(0, bytesRead).CopyTo(bw.FreeSpan);
                        }
                        t = new ValueTask <int>(bytesRead);
#endif
                        finishSync = true;
                    } while (true);
                }
                finally
                {
                    // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                    if (tempBuff != null)
                    {
                        BufferPool <byte> .Return(tempBuff);
                    }
                    stream.Dispose();
                }
            }
        }
Ejemplo n.º 12
0
        public void PocoPerfSerialize()
        {
            int  iter = 100000;
            Poco poco = new Poco();

            poco.DateProp   = DateTime.Now;
            poco.IntProp    = 100;
            poco.GuidProp   = Guid.NewGuid();
            poco.StringProp = "test data";

            // byte[] is a baseline to compare with
            BufferWriter bw = new BufferWriter(256);
            var          sc = new SerializationContext();

            Serializer.Serialize(bw, poco, sc);
            int byteSize = bw.Position;

            byte[] bytes = new byte[byteSize];
            Serializer.Serialize(bw, bytes, sc);
            BufferReader br = new BufferReader(bw.Buffer);

            Poco poco2 = new Poco();

            Serializer.Deserialize(br, ref poco2, sc);
            byte[] bytes2 = new byte[byteSize];
            Serializer.Deserialize(br, ref bytes2, sc);
            sc.Reset();

            // baseline
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < iter; i++)
            {
                bw.Reset();
                Serializer.Serialize(bw, bytes, sc);
                sc.Reset();
            }

            for (int i = 0; i < iter; i++)
            {
                br.Reset();
                Serializer.Deserialize(br, ref bytes2, sc);
                sc.Reset();
            }

            sw.Stop();
            Console.WriteLine($"Baseline (byte[{byteSize}]): {sw.ElapsedMilliseconds * 1000000d / iter} nanoseconds per serialization + deserialization");

            // POCO test
            sw = Stopwatch.StartNew();
            for (int i = 0; i < iter; i++)
            {
                bw.Reset();
                Serializer.Serialize(bw, poco, sc);
                sc.Reset();
            }

            for (int i = 0; i < iter; i++)
            {
                br.Reset();
                Serializer.Deserialize(br, ref poco2, sc);
                sc.Reset();
            }

            sw.Stop();
            Console.WriteLine($"Simple object (poco): {sw.ElapsedMilliseconds * 1000000d / iter} nanoseconds per serialization + deserialization");
            Console.WriteLine($"{iter / (sw.ElapsedMilliseconds / 1000d)} roundtrip operations per second");

            Assert.AreEqual(poco.StringProp, poco2.StringProp);
            Assert.AreEqual(poco.DateProp, poco2.DateProp);
            Assert.AreEqual(poco.IntProp, poco2.IntProp);
            Assert.AreEqual(poco.GuidProp, poco2.GuidProp);
        }
Ejemplo n.º 13
0
 public int Write(BufferWriter buffer, Envelope envelope)
 {
     return(this.Write(envelope, buffer.Buffer, 0, buffer.Position));
 }
Ejemplo n.º 14
0
        public void BufferTest()
        {
            BufferWriter bufW = new BufferWriter(2); // force reallocations
            byte         b    = 5;

            byte[] br = new byte[] { 1, 2, 3 };
            char   c  = 'A';

            char[] cr  = new char[] { 'A', 'B', 'C' };
            string str = "a string";
            double d   = 1.1;

            double[] dr = new double[] { 1.1, 2.2, 3.3 };
            float    f  = 0.1f;

            float[] fr = new float[] { 0.1f, 0.01f, 0.001f };
            int     i  = 0x0FF00FFF;

            int[] ir = new[] { 0x0FF00FF0, 0x0FF00FF1, 0x0FF00FF2 };
            short s  = 0x0FFF;

            short[] sr = new short[] { 0x0FF0, 0x0FF1, 0x0FF2 };
            long    l  = 0x00FFFF0000EEEE0F;

            long[]   lr     = new long[] { 0x00FFFF0000EEEE00, 0x00FFFF0000EEEE01, 0x00FFFF0000EEEE02 };
            ushort   us     = 0xFFFF;
            uint     ui     = 0xFFFFFFFF;
            ulong    ul     = 0xFFFFFFFFFFFFFFFF;
            DateTime dt     = DateTime.UtcNow;
            var      stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });

            bufW.Write(b);
            bufW.Write(br);
            bufW.Write(c);
            bufW.Write(cr);
            bufW.Write(str);
            bufW.Write(d);
            bufW.Write(dr);
            bufW.Write(f);
            bufW.Write(fr);
            bufW.Write(i);
            bufW.Write(ir);
            bufW.Write(s);
            bufW.Write(sr);
            bufW.Write(l);
            bufW.Write(lr);
            bufW.Write(us);
            bufW.Write(ui);
            bufW.Write(ul);
            bufW.Write(dt);
            bufW.CopyFromStream(stream, 2);
            bufW.CopyFromStream(stream, 3);
            int position = bufW.Position;

            bufW.CopyFromStream(stream, 2); // attempt to read past end of stream
            Assert.AreEqual(position, bufW.Position);

            BufferReader bufR = new BufferReader(bufW.Buffer);

            Assert.AreEqual(b, bufR.ReadByte());
            var br_r = new byte[br.Length];

            bufR.Read(br_r, br.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(br, br_r));
            Assert.AreEqual(c, bufR.ReadChar());
            var cr_r = new char[cr.Length];

            bufR.Read(cr_r, cr.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(cr, cr_r));
            Assert.AreEqual(str, bufR.ReadString());
            Assert.AreEqual(d, bufR.ReadDouble());
            var dr_r = new double[dr.Length];

            bufR.Read(dr_r, dr.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(dr, dr_r));
            Assert.AreEqual(f, bufR.ReadSingle());
            var fr_r = new float[fr.Length];

            bufR.Read(fr_r, fr.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(fr, fr_r));
            Assert.AreEqual(i, bufR.ReadInt32());
            var ir_r = new int[ir.Length];

            bufR.Read(ir_r, ir.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(ir, ir_r));
            Assert.AreEqual(s, bufR.ReadInt16());
            var sr_r = new short[sr.Length];

            bufR.Read(sr_r, sr.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(sr, sr_r));
            Assert.AreEqual(l, bufR.ReadInt64());
            var lr_r = new long[lr.Length];

            bufR.Read(lr_r, lr.Length);
            Assert.IsTrue(Enumerable.SequenceEqual(lr, lr_r));
            Assert.AreEqual(us, bufR.ReadUInt16());
            Assert.AreEqual(ui, bufR.ReadUInt32());
            Assert.AreEqual(ul, bufR.ReadUInt64());
            Assert.AreEqual(dt, bufR.ReadDateTime());
            var stream_r = new MemoryStream();

            bufR.CopyToStream(stream_r, (int)stream.Length);
            CollectionAssert.AreEqual(stream.ToArray(), stream_r.ToArray());
        }
Ejemplo n.º 15
0
 public void WriteTo(BufferWriter writer, int index, int length) {
     int limit = index + length;
     if (limit > _text.Length)
         limit = _text.Length;
     WriteToInternal(writer, index, limit);
 }
Ejemplo n.º 16
0
 public void WriteTo(ref BufferWriter <byte> writer)
 {
     writer.WriteVarArray(Headers);
 }
Ejemplo n.º 17
0
 // The untyped interface methods call the inner serializer directly (bypassing the handler methods)
 // to avoid writing and reading a second prefix
 internal override void UntypedSerialize(BufferWriter writer, object instance, SerializationContext context)
 => this.innerSerializer.Serialize(writer, (T)instance, context);
Ejemplo n.º 18
0
 public void Write(BufferWriter bufferWriter)
 {
     this.ReserveBlock(bufferWriter.Position);
     this.WriteToBlock(bufferWriter.Buffer, 0, bufferWriter.Position);
     this.CommitBlock();
 }
Ejemplo n.º 19
0
 protected abstract void WriteXmsHeaders(ref BufferWriter writer, ref T arguments);
Ejemplo n.º 20
0
 public void WriteTo(BufferWriter writer, int index)
 {
     WriteToInternal(writer, index, _text.Length);
 }
Ejemplo n.º 21
0
 public void Serialize(BufferWriter writer, T instance, SerializationContext context)
 {
     this.serializeImpl(writer, instance, context);
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Receiver for the combined VAD signal and audio data.
        /// </summary>
        /// <param name="data">A message containing the combined VAD signal and audio data.</param>
        /// <param name="e">The message envelope</param>
        /// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
        protected override async Task ReceiveAsync(ValueTuple <AudioBuffer, bool> data, Envelope e)
        {
            byte[] audioData = data.Item1.Data;
            bool   hasSpeech = data.Item2;

            this.lastAudioOriginatingTime = e.OriginatingTime;

            // Throw if a fatal error has occurred in the OnConversationError event handler
            if (this.fatalError)
            {
                if (this.conversationError != null)
                {
                    var error = this.conversationError;
                    this.conversationError = null;
                    throw error;
                }

                // Stop processing until the pipeline terminates
                return;
            }

            if (hasSpeech || this.lastAudioContainedSpeech)
            {
                // Send the audio data to the cloud
                await this.speechRecognitionClient.SendAudioAsync(audioData, this.cancellationTokenSource.Token);

                // Add audio to the current utterance queue so we can reconstruct it in the recognition result later
                this.currentQueue.Enqueue(data.DeepClone(this.In.Recycler));
            }

            // If this is the last audio packet containing speech
            if (!hasSpeech && this.lastAudioContainedSpeech)
            {
                this.lastVADSpeechEndTime      = e.OriginatingTime;
                this.lastVADSpeechTimeInterval = new TimeInterval(this.lastVADSpeechStartTime, this.lastVADSpeechEndTime);

                // Allocate a buffer large enough to hold the buffered audio
                BufferWriter bw = new BufferWriter(this.currentQueue.Sum(b => b.Item1.Length));

                // Get the audio associated with the recognized text from the current queue.
                ValueTuple <AudioBuffer, bool> buffer;
                while (this.currentQueue.TryDequeue(out buffer))
                {
                    bw.Write(buffer.Item1.Data);

                    // We are done with this buffer so enqueue it for recycling
                    this.In.Recycle(buffer);
                }

                // Save the buffered audio
                this.lastAudioBuffer = bw.Buffer;

                // Call EndAudio to signal that this is the last packet
                await this.speechRecognitionClient.SendEndAudioAsync(this.cancellationTokenSource.Token);
            }
            else if (hasSpeech && !this.lastAudioContainedSpeech)
            {
                // If this is the first audio packet containing speech
                this.lastVADSpeechStartTime = e.OriginatingTime;

                // Also post a null partial recognition result
                this.lastPartialResult = string.Empty;
                this.PartialRecognitionResults.Post(this.BuildPartialSpeechRecognitionResult(this.lastPartialResult), e.OriginatingTime);
            }

            // Remember last audio state.
            this.lastAudioContainedSpeech = hasSpeech;
        }
Ejemplo n.º 23
0
 public void Serialize(BufferWriter writer, MemoryStream instance, SerializationContext context)
 {
     this.innerSerializer.Serialize(writer, instance, context);
 }
Ejemplo n.º 24
0
 private static void PlainText(ref BufferWriter <WriterAdapter> writer, ReadOnlySpan <byte> body)
 {
     writer.Write(body);
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Wrtie any BsonValue. Use 1 byte for data type, 1 byte for length (optional), 0-255 bytes to value.
        /// For document or array, use BufferWriter
        /// </summary>
        public static void WriteIndexKey(this BufferSlice buffer, BsonValue value, int offset)
        {
            ENSURE(value.GetBytesCount(false) < 255, "index key must have less than 255 bytes");

            buffer[offset++] = (byte)value.Type;

            switch (value.Type)
            {
            case BsonType.Null:
            case BsonType.MinValue:
            case BsonType.MaxValue:
                break;

            case BsonType.Int32: buffer.Write(value.AsInt32, offset); break;

            case BsonType.Int64: buffer.Write(value.AsInt64, offset); break;

            case BsonType.Double: buffer.Write(value.AsDouble, offset); break;

            case BsonType.Decimal: buffer.Write(value.AsDecimal, offset); break;

            case BsonType.String:
                var str       = value.AsString;
                var strLength = (byte)Encoding.UTF8.GetByteCount(str);
                buffer[offset++] = strLength;
                buffer.Write(str, offset);
                break;

            case BsonType.Document:
                using (var w = new BufferWriter(buffer))
                {
                    w.Skip(offset);     // skip offset from buffer
                    w.WriteDocument(value.AsDocument, true);
                }
                break;

            case BsonType.Array:
                using (var w = new BufferWriter(buffer))
                {
                    w.Skip(offset);     // skip offset from buffer
                    w.WriteArray(value.AsArray, true);
                }
                break;

            case BsonType.Binary:
                var arr = value.AsBinary;
                buffer[offset++] = (byte)arr.Length;
                buffer.Write(arr, offset);
                break;

            case BsonType.ObjectId: buffer.Write(value.AsObjectId, offset); break;

            case BsonType.Guid: buffer.Write(value.AsGuid, offset); break;

            case BsonType.Boolean: buffer[offset] = (value.AsBoolean) ? (byte)1 : (byte)0; break;

            case BsonType.DateTime: buffer.Write(value.AsDateTime, offset); break;

            default: throw new NotImplementedException();
            }
        }
Ejemplo n.º 26
0
 /// <inheritdoc />
 public void Serialize(BufferWriter writer, Dictionary <TKey, TValue> instance, SerializationContext context)
 {
     this.comparerHandler.Serialize(writer, instance.Comparer, context);
     this.entriesHandler.Serialize(writer, instance.ToArray(), context);
 }
Ejemplo n.º 27
0
        private void TestMemoryStreamSerialization(MemoryStream source)
        {
            var expectedValues = source.ToArray();

            // serialize the source
            var bw = new BufferWriter(0);

            Serializer.Serialize(bw, source, new SerializationContext());

            // verify serialized length matches the source data length (+ refHeader + dataLength)
            Assert.AreEqual(source.Length + 8, bw.Position);

            // deserialize into default target
            var br     = new BufferReader(bw.Buffer);
            var target = default(MemoryStream);

            Serializer.Deserialize(br, ref target, new SerializationContext());
            CollectionAssert.AreEqual(expectedValues, target.ToArray());
            Assert.AreEqual(source.Length, target.Length);
            Assert.AreEqual(source.Length, target.Capacity);

            // deserialize into target with expandable capacity
            br     = new BufferReader(bw.Buffer);
            target = new MemoryStream();
            Serializer.Deserialize(br, ref target, new SerializationContext());
            CollectionAssert.AreEqual(expectedValues, target.ToArray());
            Assert.AreEqual(source.Length, target.Length);

            // deserialize into target with existing data
            br     = new BufferReader(bw.Buffer);
            target = new MemoryStream();
            target.Write(new byte[] { 1, 2, 3, 4, 5 });
            Serializer.Deserialize(br, ref target, new SerializationContext());
            CollectionAssert.AreEqual(expectedValues, target.ToArray());
            Assert.AreEqual(source.Length, target.Length);

            // deserialize into target with fixed capacity >> length
            br     = new BufferReader(bw.Buffer);
            target = new MemoryStream(new byte[1000]);
            Serializer.Deserialize(br, ref target, new SerializationContext());
            CollectionAssert.AreEqual(expectedValues, target.ToArray());
            Assert.AreEqual(source.Length, target.Length);
            Assert.AreEqual(1000, target.Capacity); // original capacity preserved

            // deserialize into target with fixed and insufficient capacity
            br     = new BufferReader(bw.Buffer);
            target = new MemoryStream(new byte[1]);
            try
            {
                Serializer.Deserialize(br, ref target, new SerializationContext());
                Assert.Fail("Expected exception was not thrown");
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(NotSupportedException));
            }

            // deserialize into read-only target
            br     = new BufferReader(bw.Buffer);
            target = new MemoryStream(new byte[source.Length], writable: false);
            try
            {
                Serializer.Deserialize(br, ref target, new SerializationContext());
                Assert.Fail("Expected exception was not thrown");
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(NotSupportedException));
            }
        }
Ejemplo n.º 28
0
 /// <inheritdoc />
 public void Serialize(BufferWriter writer, Dictionary <TKey, TValue> instance, SerializationContext context)
 {
     this.innerSerializer.Serialize(writer, instance, context);
 }
Ejemplo n.º 29
0
 internal HubReader(BufferWriter bufferWriter)
 {
     _bufferWriter = bufferWriter;
 }
Ejemplo n.º 30
0
 public void Serialize(BufferWriter writer, Shared <T> instance, SerializationContext context)
 {
     this.handler.Serialize(writer, instance.inner, context);
 }
Ejemplo n.º 31
0
 public void WriteTo(BufferWriter writer, int index) {
     WriteToInternal(writer, index, _text.Length);
 }
Ejemplo n.º 32
0
 public override void WriteTransactionData(ref BufferWriter <byte> writer)
 {
     writer.WriteLittleEndian(Nonce);
 }
Ejemplo n.º 33
0
 private void WriteToInternal(BufferWriter writer, int index, int limit) {
     char[] temp = GetInternalTemporaryBufferForCopy();
     // Note: must be temp.Length >= limit here
     int tempIndex = 0;
     for (int i = index; i < limit; i++) {
         char ch = _text[i];
         if (ch == '\0')
             break;
         if (ch != WIDECHAR_PAD)
             temp[tempIndex++] = ch;
     }
     if (writer != null)
         writer(temp, tempIndex);
 }
Ejemplo n.º 34
0
 public static int WriteItem <T>(BufferWriter bufferWriter, in T value)