void ScheduleForResend(UdpMessageData msg)
        {
            Task t = new Task(async() =>
            {
                await Task.Delay(5000); //wait 5 seconds
                if (!ackedMessages.ContainsKey(msg.id))
                {
                    DateTime endOfRetries = this.endOfRetries[msg.id];
                    if (DateTime.UtcNow <= endOfRetries)
                    {
                        messages.Add(msg); //retry
                    }
                    else
                    {
                        FailedToSend?.Invoke(msg.id);//signal failed transmission
                    }
                }
                else
                {
                    //remove if it was acked
                    ackedMessages.TryRemove(msg.id, out msg);
                    endOfRetries.TryRemove(msg.id, out DateTime dt);
                }
            });

            t.Start();
        }
        private void FailedQueuedLetters()
        {
            while (_queue.TryDequeue(out ILetter letter))
            {
                FailedToSend?.Invoke(this, letter);
            }

            _batchBuilder.Clear();
        }
 private void FailQueuedLetters()
 {
     while (_queue.TryDequeue(out ILetter letter))
     {
         if (letter.Type == LetterType.User || letter.Type == LetterType.Batch)
         {
             FailedToSend?.Invoke(this, letter);
         }
     }
 }
        public EnqueueResult Enqueue(ILetter letter)
        {
            if (!CanSend)
            {
                FailedToSend?.Invoke(this, letter);
                return(EnqueueResult.CantEnqueueMore);
            }

            InternalEnqueue(letter);

            return(EnqueueResult.CantEnqueueMore);
        }
        private void ChannelOnFailedToSend(IChannel channel, ILetter letter)
        {
            _sentBatch = false;

            if (letter.Type == LetterType.Batch)
            {
                FailedQueuedLetters();
            }
            else
            {
                FailedToSend?.Invoke(this, letter);
            }
        }