/// <summary>
/// Begins an asynchronous reading from the network stream.
/// </summary>
/// <param name="buffer">The buffer to store data to.</param>
/// <param name="offset">The location in the <i>buffer</i> where to start storing the received data.</param>
/// <param name="size">The number of bytes to read.</param>
/// <param name="callback">
/// The <see cref="System.AsyncCallback">AsyncCallback</see> delegate.
/// </param>
/// <param name="state">
/// An object containing state information for this request.
/// </param>
/// <returns>
/// An <see cref="System.IAsyncResult">IAsyncResult</see>
/// that references the asynchronous read.
/// </returns>
/// <remarks>
/// The <b>BeginRead</b> method starts an asynchronous
/// read operation from the network stream.
/// It returns immediately and does not wait for
/// the asynchronous call to complete.
/// <para>
/// The
/// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndRead"/>
/// method is used to retrieve the results of
/// the asynchronous call. It can be called
/// any time after <b>BeginRead</b>; if the asynchronous
/// call has not completed,
/// <b>EndRead</b>
/// will block until it completes.
/// </para>
/// <para>
/// The read operation will not completed until the number of bytes specified by <i>size</i>
/// parameter is read from the stream. If the remote host shuts down the
/// <see cref="BytesRoad.Net.Sockets.SocketEx"/>
/// connection with the
/// <see cref="BytesRoad.Net.Sockets.SocketEx.Shutdown"/>
/// method, and all available data has been received, the <b>Read</b> method
/// will complete and return number of bytes was read.
/// </para>
/// <note>
/// The <b>NetworkStreamEx</b> should have access right to read data from the network stream.
/// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanRead"/> property to check this.
/// </note>
/// </remarks>
///
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="BytesRoad.Net.Sockets.NetworkStreamEx"/> object was disposed.
/// </exception>
///
/// <exception cref="System.ArgumentNullException">
/// <i>buffer</i> is a null reference (<b>Nothing</b> in Visual Basic).
/// </exception>
///
/// <exception cref="System.ArgumentOutOfRangeException">
/// <i>offset</i> is less than 0.
/// <para>-or-</para>
/// <i>offset</i> is greater than the length of <i>buffer</i>.
/// <para>-or-</para>
/// <i>size</i> is less than 0.
/// <para>-or-</para>
/// <i>size</i> is greater than the length of <i>buffer</i> minus
/// the value of the <i>offset</i> parameter.
/// </exception>
public override IAsyncResult BeginRead(
byte[] buffer,
int offset,
int size,
AsyncCallback callback,
object state
)
{
CheckDisposed();
_asyncCtx.SetProgress(true);
try
{
Read_SO stateObj = new Read_SO(
buffer,
offset,
size,
callback,
state);
return(_socket.BeginReceive(
buffer,
offset,
size,
new AsyncCallback(Read_End),
stateObj));
}
catch
{
_asyncCtx.SetProgress(false);
CheckDisposed();
throw;
}
}