예제 #1
0
        public async Task SendAsync(KernelCommand kernelCommand, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            _sender.WriteMessage(KernelCommandEnvelope.Serialize(KernelCommandEnvelope.Create(kernelCommand)));

            await _sender.FlushAsync(cancellationToken);
        }
예제 #2
0
        public static async Task WriteMessageAsync(this PipeStream stream, string message)
        {
            var responseBytes = Encoding.UTF8.GetBytes(message);
            await stream.WriteAsync(responseBytes, 0, responseBytes.Length).ConfigureAwait(false);

            await stream.FlushAsync();

            stream.WaitForPipeDrain();
        }
예제 #3
0
        public async Task SetStatus(bool status, CancellationToken ct)
        {
            Buffer[0] = CounterCommands.SetStatus;
            Buffer[1] = status ? (byte)1 : (byte)0;

            await PipeStream.WriteAsync(Buffer, offset : 0, count : 2, ct).ConfigureAwait(false);

            await PipeStream.FlushAsync(ct).ConfigureAwait(false);
        }
        public async Task WriteObject(TValue value)
        {
            var data = Serialize(value);

            await WriteLengthAsync(data.Length);
            await WriteObjectAsync(data);

            await baseStream.FlushAsync();
        }
예제 #5
0
        /// <summary>
        /// Writing asynchronicly binary data to stream.
        /// </summary>
        /// <param name="stream">Target stream.</param>
        /// <param name="data">Binary data that would be sent to stream.</param>
        /// <returns>Asynchronous operation of data writing.</returns>
        public static async Task StreamWriterAsync(PipeStream stream, byte[] data)
        {
            // Write header.
            await stream.WriteAsync(BitConverter.GetBytes(data.Length), 0, 4);

            // Write data.
            await stream.WriteAsync(data, 0, data.Length);

            // Send data to device.
            await stream.FlushAsync();
        }
예제 #6
0
        private static void WriteCommandPacket(PipeStream channel, byte[] buffer)
        {
            int l = buffer.Length;

            byte[] lengthBuffer = new byte[4] {
                (byte)l, (byte)(l >> 8), (byte)(l >> 16), (byte)(l >> 24)
            };
            channel.Write(lengthBuffer, 0, lengthBuffer.Length);
            channel.Write(buffer, 0, buffer.Length);
            channel.FlushAsync();
        }
예제 #7
0
        /// <inheritdoc />
        public async Task WriteAsync(byte[] bytes, CancellationToken cancellationToken = default(CancellationToken))
        {
            int offset    = 0;
            int bytesLeft = bytes.Length;

            while (bytesLeft > 0)
            {
                int write = bytesLeft > Constants.BufferSize ? Constants.BufferSize : bytesLeft;
                await _stream.WriteAsync(bytes, offset, write, cancellationToken);

                bytesLeft -= write;
                offset    += write;
            }
            await _stream.FlushAsync(cancellationToken);
        }
예제 #8
0
        public static async Task WriteMessageToPipe(PipeStream pipe, byte[] data)
        {
            var header         = BitConverter.GetBytes(data.Length);
            var wrappedMessage = header.Concat(data).ToArray();

            //pipe.Write(wrappedMessage, 0, wrappedMessage.Length);

            //lock (pipe)
            //{
            //    pipe.Write(wrappedMessage, 0, wrappedMessage.Length);
            //    pipe.Flush();
            //}

            await pipe.WriteAsync(wrappedMessage, 0, wrappedMessage.Length);

            await pipe.FlushAsync();
        }
예제 #9
0
        public async Task <bool> GetStatus(CancellationToken ct)
        {
            Buffer[0] = CounterCommands.GetStatus;
            await PipeStream.WriteAsync(Buffer, offset : 0, count : 1, ct).ConfigureAwait(false);

            await PipeStream.FlushAsync(ct).ConfigureAwait(false);

            await PipeStream.ReadAsync(Buffer, offset : 0, count : 2, ct).ConfigureAwait(false);

            if (Buffer[0] != CounterCommands.GetStatus)
            {
                throw new InvalidOperationException($"Expected command {CounterCommands.GetStatus} but received {Buffer[0]}.");
            }

            bool status = Buffer[1] > 0;

            return(status);
        }
예제 #10
0
        /// <summary>
        /// Writing asynchronicly binary data to stream.
        /// </summary>
        /// <param name="stream">Target stream.</param>
        /// <param name="nonBinaryData">Non binary object that would be shared via stream.</param>
        /// <returns>Asynchronous operation of data writing.</returns>
        public static async Task StreamWriterAsync(PipeStream stream, object nonBinaryData)
        {
            //var bs = BinaryHandler.ToMemoryStream(nonBinaryData);

            //// Write header.
            //await stream.WriteAsync(BitConverter.GetBytes(bs.Length), 0, 4);

            //await bs.CopyToAsync(stream);

            //await stream.FlushAsync();

            // Convert data to binary format.
            byte[] data = BinaryHandler.ToByteArray(nonBinaryData);

            // Write header.
            await stream.WriteAsync(BitConverter.GetBytes(data.Length), 0, 4);

            //// Write data.
            await stream.WriteAsync(data, 0, data.Length);

            // Send data to device.
            await stream.FlushAsync();
        }
예제 #11
0
 public override Task FlushAsync(CancellationToken cancellationToken) => _underlying.FlushAsync(cancellationToken);