Beispiel #1
0
        /// <summary>
        /// Performs the sniffing.<para/>
        /// This function returns immediately as the sniffing process is handled through events. It returns
        /// the handler of the output file on which the sniffed data is stored.
        /// </summary>
        /// <returns>The TextWriter on which the sniffed data have been stored.</returns>
        public static TextWriter DoSniff()
        {
            DateTime start   = DateTime.MinValue;
            bool     isFirst = true;

            Sniffer sniffer = new Sniffer(
                GlobalParameters.VirtualPort,
                GlobalParameters.RealPort,
                GlobalParameters.TransmissionBaudRate,
                GlobalParameters.TransmissionParity,
                GlobalParameters.TransmissionStopBits,
                GlobalParameters.TransmissionDataBits);

            sniffer.Mode = GlobalParameters.Mode;
            sniffer.IsCollapsingSameOrigin = GlobalParameters.IsShowCollapsed;

            TextWriter outputFile = null;

            if (GlobalParameters.OutputFileName.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "stdout")
            {
                outputFile = Console.Out;
            }
            else
            {
                outputFile = new StreamWriter(GlobalParameters.OutputFileName);
            }

            sniffer.PacketAvailable += (s, e) =>
            {
                if (isFirst)
                {
                    start   = e.When;
                    isFirst = false;
                }

                string arrivedPacket = DecodeArrivedPacket(e, start);
                outputFile.WriteLine(arrivedPacket);
                if (outputFile != Console.Out)
                {
                    Console.WriteLine(arrivedPacket);
                }
            };
            sniffer.OpenAndSniff();
            return(outputFile);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Gui" /> class.
        /// </summary>
        public Gui()
        {
            this.InitializeComponent();
            this.StartStop.IsEnabled = !this.CheckInputFieldsError();

            Thread runner = new Thread(() =>
            {
                while (true)
                {
                    if (startRequest)
                    {
                        Sniffer sniffer = new Sniffer(
                            GlobalParameters.VirtualPort,
                            GlobalParameters.RealPort,
                            GlobalParameters.TransmissionBaudRate,
                            GlobalParameters.TransmissionParity,
                            GlobalParameters.TransmissionStopBits,
                            GlobalParameters.TransmissionDataBits);

                        sniffer.Mode   = GlobalParameters.Mode;
                        DateTime start = DateTime.MinValue;
                        bool isFirst   = true;

                        sniffer.IsCollapsingSameOrigin = GlobalParameters.IsShowCollapsed;

                        sniffer.PacketAvailable += (s, ee) =>
                        {
                            if (isFirst)
                            {
                                start   = ee.When;
                                isFirst = false;
                            }

                            string sniffed = Program.DecodeArrivedPacket(ee, start);
                            this.SnifferOuput.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                // this.SnifferOuput.Document.
                                Paragraph par = new Paragraph();
                                par.Margin = new Thickness(0);
                                if (ee.Origin == Origin.FromReal)
                                {
                                    par.Background = Brushes.AliceBlue;
                                    par.Foreground = Brushes.Blue;
                                }
                                else
                                {
                                    par.Background = Brushes.OldLace;
                                    par.Foreground = Brushes.Red;
                                }

                                par.Background = (ee.Origin == Origin.FromReal) ? Brushes.AliceBlue : Brushes.OldLace;
                                par.Inlines.Add(sniffed);
                                this.SnifferOuput.Document.Blocks.Add(par);
                                this.SnifferOuput.ScrollToEnd();
                            }));
                        };
                        try
                        {
                            sniffer.OpenAndSniff();
                        }
                        catch (System.IO.IOException)
                        {
                            MessageBox.Show("Connection error: probably one ports has a wrong name");
                        }

                        startRequest = false;
                    }

                    if (stopRequest)
                    {
                        stopRequest = false;
                        break;
                    }
                }
            });

            runner.Start();
        }