Ejemplo n.º 1
0
        private void EventCopyLogDoWork(object sender, DoWorkEventArgs e)
        {
            File.Delete(iTempLogFile);
            using (BinaryReader binaryReader = (BinaryReader)e.Argument)
                using (StreamWriter streamWriter = new StreamWriter(iTempLogFile, false))
                {
                    int          count = 0;
                    long         bytesRead = 0;
                    long         fileSize = new FileInfo(iMainLogFileCopy).Length;
                    int          percentComplete = 0, newPercentage;
                    ViewerRecord viewerRecord;
                    ViewerRecord prevViewerRecord = null;

                    while (bytesRead < fileSize)
                    {
                        if (((BackgroundWorker)sender).CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        viewerRecord = new ViewerRecord(binaryReader);
                        count++;
                        StringBuilder payload = new StringBuilder(viewerRecord.ToString());
                        if (viewerRecord.PayloadLength != 0 && payload.ToString().Contains("\n"))
                        {
                            //We must convert from DS supplied Unix-style '\n' newlines to
                            //windows '\r\n' newlines which are the only ones notepad can handle.
                            payload.Insert(payload.Length - 1, '\r');
                        }

                        //Some lines are made up of multiple records, in this case we only want to write one timestamp
                        //at the start of the line.
                        if (prevViewerRecord == null || prevViewerRecord.Payload.Contains((byte)'\n'))
                        {
                            //Here we trim excess zeroes from the timespan's output.
                            TimeSpan t         = TimeSpan.FromMilliseconds(viewerRecord.Timestamp);
                            string   timeStamp = t.ToString();
                            //some timestamps don't have milliseconds, so just skip them.
                            if (!(timeStamp.Length < 11))
                            {
                                timeStamp = timeStamp.Substring(0, timeStamp.LastIndexOf('.') + 3);
                            }
                            payload.Insert(0, timeStamp + " ");
                        }
                        streamWriter.Write(payload);

                        bytesRead       += viewerRecord.Size;
                        prevViewerRecord = viewerRecord;

                        newPercentage = (int)((bytesRead * 100) / fileSize);
                        if (newPercentage > percentComplete)
                        {
                            iCopyLog.ReportProgress(newPercentage);
                            percentComplete = newPercentage;
                        }
                    }
                }
        }
Ejemplo n.º 2
0
        private void RunTcpServer()
        {
            while (true)
            {
                try
                {
                    TcpClient iClient = iTcpListener.AcceptTcpClient();
                    lock (iTcpListenerThread)
                    {
                        iTcpClient = iClient;
                        iState     = TcpListenenerState.Handling;
                    }
                }
                //The following 3 exceptions are thrown due to iTcpListener being disposed as ViewerManager is shutting down.
                //We interpret these exceptions as a signal to shutdown the iTcpListenerThread.
                catch (SocketException)
                {
                    return;
                }
                catch (NullReferenceException)
                {
                    return;
                }
                catch (InvalidOperationException)
                {
                    return;
                }

                if (ConnectionAccepted != null)
                {
                    ConnectionAccepted(this, EventArgs.Empty);
                }

                iViewerRecords   = new List <ViewerRecord>();
                iViewerOutputSrb = new Srb(iSrbMaxBufferSize, new TcpReaderSource(iTcpClient.GetStream()));
                while (true)
                {
                    try
                    {
                        ViewerRecord record = new ViewerRecord(iViewerOutputSrb);
                        lock (iViewerRecords)
                        {
                            iViewerRecords.Add(record);
                        }
                        if (ViewerOutputAvailable != null)
                        {
                            ViewerOutputAvailable(this, EventArgs.Empty);
                        }
                    }
                    catch (ReaderError)
                    {
                        //remote host has disconnected.
                        if (ConnectionClosed != null)
                        {
                            ConnectionClosed(this, EventArgs.Empty);
                        }
                        break;
                    }
                }
                CloseConnection();
            }
        }