/// <summary>
/// Begins an asynchronous write to a stream.
/// </summary>
/// <param name="buffer">The location in memory that holds the data to send.</param>
/// <param name="offset">The location in buffer to begin sending the data.</param>
/// <param name="size">The size of buffer.</param>
/// <param name="callback">The delegate to call when the asynchronous call is complete.</param>
/// <param name="state">An object containing additional information supplied by the client.</param>
/// <returns>An <see cref="IAsyncResult"/> representing the asynchronous call.</returns>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception>
/// <exception cref="ArgumentOutOfRangeException">The specified <paramref name="offset"/> or <paramref name="size"/> exceeds the size of <paramref name="buffer"/>.</exception>
/// <exception cref="IOException">There is a failure while writing to the network.</exception>
// Thanks go out to Martin Plante for notifying us about a bug in this method.
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
if (buffer == null)
{
throw new ArgumentNullException();
}
if (offset < 0 || offset > buffer.Length || size < 0 || size > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException();
}
if (Socket == null)
{
throw new IOException();
}
if (WriteResult != null)
{
throw new IOException();
}
TransferItem localResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);
WriteResult = localResult;
Array.Copy(buffer, offset, localResult.Buffer, 0, size);
try {
Socket.BeginSend(localResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
return(localResult.AsyncResult);
} catch {
throw new IOException();
}
}