private void InnerStartOperationSendPackets()
{
// Prevent mutithreaded manipulation of the list.
if (_sendPacketsElements != null)
{
_sendPacketsElementsInternal = (SendPacketsElement[])_sendPacketsElements.Clone();
}
// TransmitPackets uses an array of TRANSMIT_PACKET_ELEMENT structs as
// descriptors for buffers and files to be sent. It also takes a send size
// and some flags. The TRANSMIT_PACKET_ELEMENT for a file contains a native file handle.
// This function basically opens the files to get the file handles, pins down any buffers
// specified and builds the native TRANSMIT_PACKET_ELEMENT array that will be passed
// to TransmitPackets.
// Scan the elements to count files and buffers.
_sendPacketsElementsFileCount = 0;
_sendPacketsElementsBufferCount = 0;
Debug.Assert(_sendPacketsElementsInternal != null);
foreach (SendPacketsElement spe in _sendPacketsElementsInternal)
{
if (spe != null)
{
if (spe._filePath != null)
{
_sendPacketsElementsFileCount++;
}
if (spe._buffer != null && spe._count > 0)
{
_sendPacketsElementsBufferCount++;
}
}
}
// Attempt to open the files if any were given.
if (_sendPacketsElementsFileCount > 0)
{
// Create arrays for streams and handles.
_sendPacketsFileStreams = new FileStream[_sendPacketsElementsFileCount];
_sendPacketsFileHandles = new SafeHandle[_sendPacketsElementsFileCount];
// Loop through the elements attempting to open each files and get its handle.
int index = 0;
foreach (SendPacketsElement spe in _sendPacketsElementsInternal)
{
if (spe != null && spe._filePath != null)
{
Exception fileStreamException = null;
try
{
// Create a FileStream to open the file.
_sendPacketsFileStreams[index] =
new FileStream(spe._filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception ex)
{
// Save the exception to throw after closing any previous successful file opens.
fileStreamException = ex;
}
if (fileStreamException != null)
{
// Got an exception opening a file - do some cleanup then throw.
for (int i = 0; i < _sendPacketsElementsFileCount; i++)
{
// Drop handles.
_sendPacketsFileHandles[i] = null;
// Close any open streams.
if (_sendPacketsFileStreams[i] != null)
{
_sendPacketsFileStreams[i].Dispose();
_sendPacketsFileStreams[i] = null;
}
}
throw fileStreamException;
}
// Get the file handle from the stream.
_sendPacketsFileHandles[index] = _sendPacketsFileStreams[index].SafeFileHandle;
index++;
}
}
}
CheckPinSendPackets();
}