示例#1
0
        /// <summary>
        /// Reads the object using asynchronous methods.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A hot running task that returns the read object.
        /// </returns>
        public async Task <byte[]> ReadBytes(CancellationToken cancellationToken = default)
        {
            using (var binaryReader = new AsyncBinaryReader(this.stream, true))
            {
                var msgLength = await binaryReader.ReadIntAsync(cancellationToken).ConfigureAwait(false);

                if (msgLength == 0)
                {
                    return(null);
                }

                var bytes = await binaryReader.ReadBytesAsync(msgLength, cancellationToken).ConfigureAwait(false);

                return(bytes);
            }
        }
示例#2
0
        /// <summary>
        /// Reads the object using asynchronous methods.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A hot running task that returns the read object.
        /// </returns>
        public async Task <string> ReadString(CancellationToken cancellationToken = default)
        {
            using (var binaryReader = new AsyncBinaryReader(this.stream, true))
            {
                var msgLength = await binaryReader.ReadIntAsync(cancellationToken).ConfigureAwait(false);

                if (msgLength == 0)
                {
                    return(null);
                }

                var bytes = await binaryReader.ReadBytesAsync(msgLength, cancellationToken).ConfigureAwait(false);

                return(System.Text.Encoding.UTF8.GetString(bytes));
            }
        }