Exemple #1
0
        private void ReadDataCallback(IAsyncResult asyncResult)
        {
            NetworkReadState state = ((NetworkReadState)asyncResult.AsyncState);

            try
            {
                if (!state.DataStream.CanRead)
                {
                    throw new IOException(String.Format(Messages.Exception_CouldNotReadFromStream, state.BytesLeft, 0));
                }
                int actualBytes = state.DataStream.EndRead(asyncResult);
                if (actualBytes == 0)
                {
                    throw new IOException(String.Format(Messages.Exception_CouldNotReadFromStream, state.BytesLeft, actualBytes));
                }
                state.BytesLeft -= actualBytes;
            }
            catch (Exception ex)
            {
                state.Exception = ex;
            }
            finally
            {
                _resetEvent.Set();
            }
        }
Exemple #2
0
        /// <summary>
        /// Read requested bytes from response stream. The implementation will block until all requested bytes readed from source stream, or <see cref="TimeoutException"/> will be thrown.
        /// </summary>
        /// <param name="buffer">An array of type Byte that is the location in memory to store data read from the NetworkStream.</param>
        /// <param name="length">Number of bytes to be read from the source stream.</param>
        /// <exception cref="TimeoutException">Thrown when the time allotted for data read operation has expired.</exception>
        public override int ReadBytes(byte[] buffer, int length)
        {
            ArgumentAssert.IsNotNull(buffer, "buffer");
            ArgumentAssert.IsGreaterThan(length, 0, "length");

            NetworkReadState state = new NetworkReadState();

            state.DataStream = Stream;
            state.BytesLeft  = length;
            _resetEvent      = new ManualResetEvent(false);

            while (state.BytesLeft > 0)
            {
                _resetEvent.Reset();
                Stream.BeginRead(buffer, length - state.BytesLeft, state.BytesLeft, ReadDataCallback, state);

                WaitForNetworkData();

                if (state.Exception != null)
                {
                    throw state.Exception;
                }
            }
            return(length);
        }
		/// <summary>
		/// Read requested bytes from response stream. The implementation will block until all requested bytes readed from source stream, or <see cref="TimeoutException"/> will be thrown.
		/// </summary>
		/// <param name="buffer">An array of type Byte that is the location in memory to store data read from the NetworkStream.</param>
		/// <param name="length">Number of bytes to be read from the source stream.</param>
		/// <exception cref="TimeoutException">Thrown when the time allotted for data read operation has expired.</exception>
		public override int ReadBytes(byte[] buffer, int length)
		{
			ArgumentAssert.IsNotNull(buffer, "buffer");
			ArgumentAssert.IsGreaterThan(length, 0, "length");

			NetworkReadState state = new NetworkReadState();
			state.DataStream = Stream;
			state.BytesLeft = length;
			_resetEvent = new ManualResetEvent(false);

			while (state.BytesLeft > 0)
			{
				_resetEvent.Reset();
				Stream.BeginRead(buffer, length - state.BytesLeft, state.BytesLeft, ReadDataCallback, state);
				WaitForNetworkData();
			}
			return length;
		}