private void PostingTask()
 {
     try
     {
         DateTime lastMessage = DateTime.Now;
         while (!Finished)
         {
             var message = GetNextMessageToPost();
             if (message != null)
             {
                 PostMessage(message);
                 lastMessage = DateTime.Now;
             }
             else
             {
                 if (_client != null)                                           //If the queue runs dry we close the connection
                 {
                     if ((DateTime.Now - lastMessage).TotalMilliseconds > 5000) //TODO: parametrize.
                     {
                         log.Debug("Disposing client because of empty queue.");
                         _client.Dispose();
                         _client = null;
                     }
                     else
                     {
                         Thread.Sleep(100);
                     }
                 }
                 if (StopRequested)
                 {
                     Finished = true;
                 }
                 else
                 {
                     lock (monitor)
                     {
                         if (Finished)
                         {
                             break;
                         }
                         if (StopRequested)
                         {
                             Finished = true;
                             break;
                         }
                         Monitor.Wait(monitor, 100);
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         log.Error("Exception in the posting thread.", ex);
     }
 }
Exemple #2
0
 private void PostingTask()
 {
     try
     {
         while (!Finished)
         {
             var message = GetNextMessageToPost();
             if (message != null)
             {
                 PostMessage(message);
             }
             else
             {
                 if (_client != null)         //If the queue runs dry we close the connection
                 {
                     log.Debug("Disposing client because of empty queue.");
                     _client.Dispose();
                     _client = null;
                 }
                 if (StopRequested)
                 {
                     Finished = true;
                 }
                 else
                 {
                     lock (monitor)
                     {
                         if (Finished)
                         {
                             break;
                         }
                         if (StopRequested)
                         {
                             Finished = true;
                             break;
                         }
                         Monitor.Wait(monitor, 100);
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         log.Error("Exception in the posting thread.", ex);
     }
 }
 private void PostingTask()
 {
     try
     {
         DateTime lastMessage = DateTime.Now;
         while (!Finished)
         {
             var message = GetNextMessageToPost();
             if (message != null)
             {
                 log.DebugFormat("Posting message [{0}]", message.Subject);
                 PostMessage(message);
                 lastMessage = DateTime.Now;
             }
             else
             {
                 log.DebugFormat("No message to post, checking cleanup.");
                 if (_client != null)         //If the queue runs dry we close the connection
                 {
                     //log.DebugFormat("The posting client is not null.");
                     if ((DateTime.Now - lastMessage).TotalMilliseconds > 5000) //TODO: parametrize.
                     {
                         log.Debug("Disposing client because of empty queue.");
                         _client.Dispose();
                         _client = null;
                     }
                     else
                     {
                         //log.DebugFormat("Sleeping 100ms before next loop.");
                         Thread.Sleep(100);
                     }
                 }
                 if (StopRequested)
                 {
                     //log.Debug("Empty queue and stop requested, Finished = true.");
                     Finished = true;
                 }
                 else
                 {
                     //log.Debug("Locking monitor.");
                     lock (monitor)
                     {
                         //log.Debug("Locked monitor.");
                         if (Finished)
                         {
                             //.Debug("Finished = true breaking out of loop.");
                             break;
                         }
                         if (StopRequested)
                         {
                             //log.Debug("Empty queue and stop requested, Finished = true.");
                             Finished = true;
                             break;
                         }
                         //log.Debug("waiting 100 ms on monitor.");
                         Monitor.Wait(monitor, 100);     //TODO: BLB Possible cause of locking issue by UNI.
                     }
                     //log.Debug("Unlocked monitor.");
                 }
             }
             log.DebugFormat("Finished cleanup.");
         }
     }
     catch (Exception ex)
     {
         log.Error("Exception in the posting thread.", ex);
     }
     log.Debug("Finished posting task, exiting thread.");
 }
        private void PostMessage(NntpMessage message)
        {
            var retryCount = 0;
            var retry      = true;

            while (retry && retryCount < _configuration.MaxRetryCount)
            {
                try
                {
                    if (_client == null)
                    {
                        log.Debug("Constructing new client.");
                        _client = new SimpleNntpPostingClient(_connectionInfo);
                        _client.Connect();
                    }

                    String proposedMessageID = null;
                    if (_folderConfiguration.GenerateRandomMessageId)
                    {
                        proposedMessageID = String.Format("<{0}@{1}.{2}>",
                                                          RandomStringGenerator.GetRandomString(20, 30), RandomStringGenerator.GetRandomString(5, 10), RandomStringGenerator.GetRandomString(3));
                    }

                    var partMessageId = _client.PostYEncMessage(
                        proposedMessageID,
                        message.FromAddress,
                        message.Subject,
                        message.PostInfo.PostedGroups,
                        message.PostInfo.PostedDateTime,
                        message.Prefix,
                        message.YEncFilePart.EncodedLines,
                        message.Suffix);
                    log.DebugFormat("Message [{0}] posted. Adding to segments.", message.Subject);
                    lock (message.PostInfo.Segments)
                    {
                        //log.Debug("Locked segments list.");
                        message.PostInfo.Segments.Add(new PostedFileSegment
                        {
                            MessageId     = partMessageId,
                            Bytes         = message.YEncFilePart.Size,
                            SegmentNumber = message.YEncFilePart.Number
                        });
                    }
                    //log.Debug("Unlocked segments list.");
                    retry = false;
                    OnMessagePosted(message);
                }
                catch (Exception ex)
                {
                    if (_client != null)         //If we get an Exception we close the connection
                    {
                        log.Debug("Disposing client because of exception.");
                        _client.Dispose();
                        _client = null;
                    }
                    log.Warn("Posting yEnc message failed", ex);

                    if (retryCount++ < _configuration.MaxRetryCount)
                    {
                        log.DebugFormat("Waiting {0} second(s) before retry.", _configuration.RetryDelaySeconds);
                        Thread.Sleep(new TimeSpan(0, 0, _configuration.RetryDelaySeconds));
                        log.InfoFormat("Retrying to post message, attempt {0}", retryCount);
                    }
                    else
                    {
                        log.Error("Maximum retry attempts reached. Posting is probably corrupt.");
                    }
                }
            }
        }