public void PostMessage(NntpMessage message)
        {
            Boolean wait = true;

            while (wait)
            {
                lock (MessagesToPost)
                {
                    if (MessagesToPost.Count <= configuration.MaxConnectionCount * 2)
                    {
                        if (MessagesToPost.Count >= configuration.MaxConnectionCount)
                        {
                            StartPostingThreadsIfNotStarted();
                        }

                        MessagesToPost.Enqueue(message);
                        wait = false;
                    }
                }
                if (wait)
                {
                    lock (monitor)
                    {
                        if (!wait)
                        {
                            break;
                        }
                        Monitor.Wait(monitor, 1000);
                    }
                }
            }
        }
示例#2
0
        private void PostPart(InntpMessagePoster poster, PostedFileInfo postedFileInfo, YEncFilePart part,
                              String subjectNameBase)
        {
            var message = new NntpMessage();

            message.FromAddress = postedFileInfo.FromAddress;
            message.Subject     = String.Format(subjectNameBase, part.Number);

            message.YEncFilePart = part;
            message.PostInfo     = postedFileInfo;

            if (TotalParts > 1)
            {
                message.Prefix.Add(String.Format("=ybegin part={0} total={1} line={2} size={3} name={4}",
                                                 part.Number, TotalParts, configuration.YEncLineSize, File.Length, File.Name));

                message.Prefix.Add(String.Format("=ypart begin={0} end={1}",
                                                 part.Begin, part.End));

                message.Suffix.Add(String.Format("=yend size={0} part={1} pcrc32={2}",
                                                 part.Size, part.Number, part.CRC32));
            }
            else
            {
                message.Prefix.Add(String.Format("=ybegin line={0} size={1} name={2}",
                                                 configuration.YEncLineSize, File.Length, File.Name));

                message.Suffix.Add(String.Format("=yend size={0} crc32={1}",
                                                 File.Length, part.CRC32));
            }

            poster.PostMessage(message);
        }
示例#3
0
        private NntpMessage GetNextMessageToPost()
        {
            NntpMessage message = null;

            log.Debug("GetNextMessageToPost started.");
            lock (_messageQueue)
            {
                //log.Debug("Locked messageQueue");
                var count = _messageQueue.Count;
                log.DebugFormat("The messageQueue has {0} items.", count);
                if (count > 0)
                {
                    message = _messageQueue.Dequeue();
                }
            }
            //log.Debug("Unlocked messageQueue");
            if (message == null && !StopRequested) //If stop is requested it is logical the queue gets empty.
            {
                log.Warn("Posting thread is starved, reduce threads to make more optimal use of resources.");
            }
            return(message);
        }
示例#4
0
 protected virtual void OnMessagePosted(NntpMessage e)
 {
     MessagePosted?.Invoke(this, e);
 }
示例#5
0
        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.");
                    }
                }
            }
        }
 void PostingThread_MessagePosted(object sender, NntpMessage e)  //TODO propagate this further instead of yEnc part? (combine the two ?)
 {
     OnFilePartPosted(e.YEncFilePart);
 }