/// <summary>
/// Begins an asynchronous write to the network stream.
/// </summary>
/// <param name="buffer">Data to write.</param>
/// <param name="offset">The position in the data <i>buffer</i> from which to begin writing.</param>
/// <param name="size">The number of bytes to write.</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 write.
/// </returns>
///
/// <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>
///
/// <remarks>
/// The <b>BeginWrite</b> method starts an asynchronous
/// write operation.
/// It returns immediately and does not wait for
/// the asynchronous call to complete.
/// <para>
/// The
/// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite"/>
/// method is used to retrieve the results of
/// the asynchronous call. It can be called
/// any time after <b>BeginWrite</b>; if the asynchronous
/// call has not completed,
/// <b>EndWrite</b>
/// will block until it completes.
/// </para>
/// <para>
/// Write operation will not completed until all data, specified by <i>size</i> parameter, are sent.
/// </para>
///
/// <note>
/// The <b>NetworkStreamEx</b> should have access right to write data to the network stream.
/// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite"/> property to check this.
/// </note>
///
/// </remarks>
public override IAsyncResult BeginWrite(
byte[] buffer,
int offset,
int size,
AsyncCallback callback,
object state
)
{
CheckDisposed();
_asyncCtx.SetProgress(true);
try
{
Write_SO stateObj = new Write_SO(
buffer,
offset,
size,
callback,
state);
return(_socket.BeginSend(
buffer,
offset,
size,
new AsyncCallback(Send_End),
stateObj));
}
catch
{
_asyncCtx.SetProgress(false);
CheckDisposed();
throw;
}
}