private void StartSend(AsyncSendRecordsResult asyncSendResult)
{
lock (_sendLock) {
if (_sending)
{
// Send in progress, add to queue
if (asyncSendResult != null)
{
_sendQueue.Add(asyncSendResult);
}
return;
}
else if (asyncSendResult != null)
{
// No send in progress, insert to queue
_sendQueue.Insert(0, asyncSendResult);
}
byte[] outputBuffer = new byte[0];
while (outputBuffer.Length == 0 && _sendQueue.Count > 0)
{
// Get first asyncSendResult in queue
asyncSendResult = _sendQueue[0];
_sendQueue.Remove(asyncSendResult);
// Get output buffer from record bytes
outputBuffer = asyncSendResult.GetRecords(0);
if (outputBuffer.Length == 0)
{
asyncSendResult.SetComplete();
asyncSendResult = null;
}
}
if (asyncSendResult != null && outputBuffer.Length > 0)
{
// Start sending the record
_innerStream.BeginWrite(outputBuffer, 0, outputBuffer.Length,
new AsyncCallback(WriteCallback),
asyncSendResult);
_sending = true;
}
}
}