private void SendImpl(IEnumerator<IGrouping<string, Message>> enumerator, TaskCompletionSource<object> taskCompletionSource)
{
send:
if (!enumerator.MoveNext())
{
taskCompletionSource.TrySetResult(null);
}
else
{
IGrouping<string, Message> group = enumerator.Current;
// Get the channel index we're going to use for this message
int index = (int)((uint)_sipHashBasedComparer.GetHashCode(group.Key) % StreamCount);
Debug.Assert(index >= 0, "Hash function resulted in an index < 0.");
Task sendTask = StreamManager.Send(index, group.ToArray()).Catch(_trace);
if (sendTask.IsCompleted)
{
try
{
sendTask.Wait();
goto send;
}
catch (Exception ex)
{
taskCompletionSource.SetUnwrappedException(ex);
}
}
else
{
sendTask.Then((enumer, tcs) => SendImpl(enumer, tcs), enumerator, taskCompletionSource)
.ContinueWithNotComplete(taskCompletionSource);
}
}
}