Exemplo n.º 1
0
        /// <summary>
        /// Asynchronously reads an <see cref="int"/> from from the <c>ADB</c> server which was encoded in Hex.
        /// </summary>
        /// <param name="cancellationToken">
        /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
        /// </param>
        /// <returns>
        /// The <see cref="int"/> received from from the <c>ADB</c> server.
        /// </returns>
        public virtual async Task <ushort> ReadUInt16Async(CancellationToken cancellationToken)
        {
            using var messageBuffer = this.memoryPool.Rent(4);

            if (await this.stream.ReadBlockAsync(messageBuffer.Memory.Slice(0, 4), cancellationToken).ConfigureAwait(false) != 4)
            {
                throw new InvalidOperationException("Failed to read an integer.");
            }

            return(HexPrimitives.ReadUShort(messageBuffer.Memory.Span));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Asynchronously writes a message to the <c>ADB</c> server.
        /// </summary>
        /// <param name="message">
        /// The message to write.
        /// </param>
        /// <param name="cancellationToken">
        /// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> which represents the asynchronous operation.
        /// </returns>
        public virtual async Task WriteAsync(string message, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            ushort length = (ushort)AdbEncoding.GetByteCount(message);

            using var messageBuffer = this.memoryPool.Rent(length + 4);

            HexPrimitives.WriteUInt16(length, messageBuffer.Memory[0..].Span);