public IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, Object state)
{
#if DEBUG
using (DebugThreadTracking.SetThreadKind(ThreadKinds.User | ThreadKinds.Async))
{
#endif
bool canWrite = CanWrite; // Prevent race with Dispose.
if (_cleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
if (!canWrite)
{
throw new InvalidOperationException(SR.net_readonlystream);
}
// Validate input parameters.
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (offset < 0 || offset > buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (size < 0 || size > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
Socket chkStreamSocket = _streamSocket;
if (chkStreamSocket == null)
{
throw new IOException(SR.Format(SR.net_io_writefailure, SR.net_io_connectionclosed));
}
try
{
// Call BeginSend on the Socket.
IAsyncResult asyncResult =
chkStreamSocket.BeginSend(
buffer,
offset,
size,
SocketFlags.None,
callback,
state);
return asyncResult;
}
catch (Exception exception)
{
if (exception is OutOfMemoryException)
{
throw;
}
// Some sort of error occurred on the socket call,
// set the SocketException as InnerException and throw.
throw new IOException(SR.Format(SR.net_io_writefailure, exception.Message), exception);
}
#if DEBUG
}
#endif
}