Ejemplo n.º 1
0
        public TSharkBlock(TSharkProcess <TDecodedFrame> tsharkProcess, DataLinkType datalinkType = DataLinkType.Ethernet)
        {
            var m_pipename = $"ndx.tshark_{new Random().Next(Int32.MaxValue)}";

            m_wsender = new TSharkSender(m_pipename, datalinkType);

            m_inputBlock = new ActionBlock <RawCapture>(SendFrame);
            m_inputBlock.Completion.ContinueWith((t) => m_wsender.Close());

            m_outputBlock = new BufferBlock <TDecodedFrame>();

            // create and initialize TSHARK:
            m_tshark                = tsharkProcess;
            m_tshark.PipeName       = m_pipename;
            m_tshark.PacketDecoded += PacketDecoded;
            m_tshark.Start();
            m_tshark.Completion.ContinueWith((t) => m_outputBlock.Complete());

            m_wsender.Connected.Wait();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decodes each <see cref="RawCapture"/> of a sequence into a <typeparamref name="TRecord"/> object.
        /// </summary>
        /// <typeparam name="TRecord"></typeparam>
        /// <param name="frames">A sequence of values to invoke a transform function on.</param>
        /// <param name="tsharkProcess">A decoder process to apply to each element.</param>
        /// <param name="datalinkType">The link layer type used in decoding operation. Default is <see cref="DataLinkType.Ethernet"/>.</param>
        /// <returns>
        /// An IEnumerable<PacketFields> whose elements are the result of invoking the decode function on each element of source.
        /// </returns>
        /// <remarks>
        /// This method is implemented by using deferred execution. The immediate return value is an object that stores all the information
        /// that is required to perform the action. The query represented by this method is not executed until the object is enumerated
        /// either by calling its GetEnumerator method directly or by using foreach.
        /// </remarks>
        public static IEnumerable <TRecord> Decode <TRecord>(this IEnumerable <RawCapture> frames, TSharkProcess <TRecord> tsharkProcess, DataLinkType datalinkType = DataLinkType.Ethernet)
        {
            var pipename = $"ndx.tshark_{new Random().Next(Int32.MaxValue)}";
            var wsender  = new TSharkSender(pipename, datalinkType);

            tsharkProcess.PipeName = pipename;

            var decodedPackets = new BlockingCollection <TRecord>();

            void PacketDecoded(object sender, TRecord packet)
            {
                decodedPackets.Add(packet);
            }

            tsharkProcess.PacketDecoded += PacketDecoded;
            tsharkProcess.Start();
            if (!wsender.Connected.Wait(5000))
            {
                throw new InvalidOperationException("Cannot connect to TShark process.");
            }

            var pumpTask = Task.Run(async() =>
            {
                foreach (var frame in frames)
                {
                    await wsender.SendAsync(frame);
                }
                wsender.Close();
            });

            while (tsharkProcess.IsRunning || decodedPackets.Count > 0)
            {
                yield return(decodedPackets.Take());
            }
        }