예제 #1
0
        /// <summary>
        /// Receive bytes up to a specified buffer size.
        ///
        /// Receives and adds bytes to a single `CommandResponse` object
        /// until no more bytes are received or a Band status has been
        /// received (indicating end of data).
        /// </summary>
        /// <param name="buffer">buffer</param>
        /// <returns>Task<CommandResponse></returns>
        /// <exception cref="BandSocketConnectedNot">Not connected.<exception>
        public async Task <CommandResponse> Receive(uint buffer)
        {
            return(await Task.Run(async() => {
                CommandResponse response = new CommandResponse();
                if (!this.Connected)
                {
                    throw new BandSocketConnectedNot();
                }

                // Keep receiving until we've got status or no data
                while (true)
                {
                    uint bytes = 0;
                    response = await Task.Run(async() => {
                        bytes = await this.DataReader.LoadAsync(buffer);
                        response.AddResponse(this.ReadBytes(bytes));
                        return response;
                    });
                    if (response.StatusReceived() || bytes == 0)
                    {
                        break;
                    }
                }

                return response;
            }));
        }