public static bool TryCompleteSendTo(SafeCloseSocket socket, byte[] buffer, IList<ArraySegment<byte>> buffers, ref int bufferIndex, ref int offset, ref int count, SocketFlags flags, byte[] socketAddress, int socketAddressLen, ref int bytesSent, out SocketError errorCode)
{
for (;;)
{
int sent;
Interop.Error errno;
try
{
if (buffer != null)
{
sent = Send(socket, flags, buffer, ref offset, ref count, socketAddress, socketAddressLen, out errno);
}
else
{
sent = Send(socket, flags, buffers, ref bufferIndex, ref offset, socketAddress, socketAddressLen, out errno);
}
}
catch (ObjectDisposedException)
{
// The socket was closed, or is closing.
errorCode = SocketError.OperationAborted;
return true;
}
if (sent == -1)
{
if (errno != Interop.Error.EAGAIN && errno != Interop.Error.EWOULDBLOCK)
{
errorCode = GetSocketErrorForErrorCode(errno);
return true;
}
errorCode = SocketError.Success;
return false;
}
bytesSent += sent;
bool isComplete = sent == 0 ||
(buffer != null && count == 0) ||
(buffers != null && bufferIndex == buffers.Count);
if (isComplete)
{
errorCode = SocketError.Success;
return true;
}
}
}