private async void Receive(TcpClient client)
        {
            this._clients.Add(client);
            Log4jXmlParser    parser      = new Log4jXmlParser(this, string.Format("{0} - {1}", this.Address, client));
            UnprocessedString unprocessed = new UnprocessedString();

            while (!this.Cts.IsCancellationRequested)
            {
                byte[] buffer = new byte[client.ReceiveBufferSize];
                int    num;
                try
                {
                    num = await client.GetStream().ReadAsync(buffer, 0, buffer.Length, this.Cts.Token);
                }
                catch (InvalidOperationException ex)
                {
                    _logger.Warn($"Client on TcpReceiver {this.Name}, {this.Address} disconnected foul", ex, true);
                    break;
                }
                catch (IOException ioEx)
                {
                    _logger.Warn($"Client on TcpReceiver {this.Name}, {this.Address} disconnected foul", ioEx, true);
                    break;
                }
                if (num > 0)
                {
                    MessageBlock withUnprocessed = parser.ParseWithUnprocessed(new InputBuffer(buffer, buffer.Length, this.Encoding, new int?(), new CancellationToken(), int.MaxValue), unprocessed, new DateTimeOffset?(), new DateTimeOffset?(), new int?(), new CancellationToken());
                    this.AddNewMessages(withUnprocessed);
                    buffer = (byte[])null;
                }
                else
                {
                    break;
                }
            }
            try
            {
                client?.Dispose();
            }
            catch (Exception ex)
            {
                _logger.Warn($"Client on TcpReceiver {this.Name}, {this.Address} could not be closed/disposed", ex, true);
            }
        }
예제 #2
0
        public MessageBlock ParseWithUnprocessed(
            InputBuffer buffer,
            UnprocessedString unprocessedData,
            DateTimeOffset?readFrom  = null,
            DateTimeOffset?readUntil = null,
            int?maxMessageCount      = null,
            CancellationToken ct     = default(CancellationToken))
        {
            MessageBlock messageBlock = new MessageBlock();

            if (unprocessedData == null)
            {
                unprocessedData = new UnprocessedString();
            }
            this._lineNumber = 0;
            bool flag = false;
            int  num1 = Math.Max(500, buffer.LineCount / 10);

            foreach (string line in buffer.Lines)
            {
                string str1 = line.Trim(new char[1]);
                if (!string.IsNullOrEmpty(str1))
                {
                    int num2    = 0;
                    int length1 = unprocessedData.Length;
                    unprocessedData.Append(str1);
                    int startIndex = num2;
                    int num3       = 0;
                    do
                    {
                        if (num3 > -1)
                        {
                            num3 = str1.IndexOf("event>", startIndex, StringComparison.OrdinalIgnoreCase);
                        }
                        string str2 = flag ? "]]>" : "<![CDATA[";
                        int    num4 = str1.IndexOf(str2, startIndex, StringComparison.OrdinalIgnoreCase);
                        if (num4 >= 0 && (num3 <= 0 || num4 < num3))
                        {
                            flag       = !flag;
                            startIndex = num4 + str2.Length;
                        }
                        else
                        {
                            startIndex = num3 < 0 ? -1 : num3 + "event>".Length;
                        }
                    }while ((num3 < 0 || num3 > startIndex) && (startIndex >= 0 && startIndex < str1.Length));
                    int length2 = length1 + num3 + "event>".Length - num2;
                    while (!flag && num3 >= 0 && length2 > 0)
                    {
                        LogMessage xmlElement = this.ParseXmlElement(unprocessedData.Substring(num2, length2), buffer.Id);
                        if (xmlElement != null)
                        {
                            if (readUntil.HasValue)
                            {
                                DateTimeOffset?nullable     = readUntil;
                                DateTimeOffset adjustedTime = xmlElement.AdjustedTime;
                                if ((nullable.HasValue ? (nullable.GetValueOrDefault() < adjustedTime ? 1 : 0) : 0) != 0)
                                {
                                    this.NotifyProgress(1.0);
                                    return(messageBlock);
                                }
                            }
                            if (!readFrom.HasValue || xmlElement.AdjustedTime >= readFrom.Value)
                            {
                                messageBlock.Add(xmlElement);
                                if (maxMessageCount.HasValue)
                                {
                                    return(messageBlock);
                                }
                            }
                        }
                        num2 += length2;
                        if (num2 == unprocessedData.Length)
                        {
                            num3    = -1;
                            length2 = 0;
                        }
                        else
                        {
                            num3    = unprocessedData.IndexOfValueInLines("event>", num2, StringComparison.OrdinalIgnoreCase);
                            length2 = num3 - num2;
                        }
                    }
                    unprocessedData.Shorten(num2);
                }
                if (ct.IsCancellationRequested)
                {
                    return(messageBlock);
                }
                if (++this._lineNumber % num1 == 0)
                {
                    this.NotifyProgress(this._lineNumber * 1.0 / buffer.LineCount);
                }
            }
            this.NotifyProgress(1.0);
            return(messageBlock);
        }