static PacketFormatter ChoosePacketFormatter(TJPacketExportFormat fmt) { PacketFormatter formatter = null; switch (fmt) { case TJPacketExportFormat.Binary: formatter = PacketToBinary; break; case TJPacketExportFormat.CSV: formatter = PacketToCSV; break; case TJPacketExportFormat.Custom: break; default: throw new ArgumentException("Invalid formatter type"); } if (formatter == null) throw new InvalidOperationException("No packet formatter assigned"); return formatter; }
/// <summary> /// Takes an IEnumerable of raw packets and exports them to a file in the given format. /// </summary> /// <param name="packets">The raw packets</param> /// <param name="outputStream">Output stream</param> /// <param name="fmt">The format in which each packet is to be written</param> public static void ExportPackets(IEnumerable<TJPacket> packets, Stream outputStream, TJPacketExportFormat fmt) { byte[] buffer = new byte[kBufferSize]; byte[] formattedPacket; PacketFormatter formatter = ChoosePacketFormatter(fmt); foreach (TJPacket packet in packets) { formattedPacket = formatter(packet); outputStream.Write(formattedPacket, 0, formattedPacket.Length); } }
/// <summary> /// Listens for the Dragonfly's packets and forwards them to the a file with the given packet format /// </summary> /// <param name="fmt">The format in which each packet is to be written</param> /// <param name="outputFilePath"></param> /// <param name="maxNumberOfPackets">The maximum number of packets to record</param> public void StartCapturing(string outputFilePath, TJPacketExportFormat fmt, int maxNumberOfPackets = 1000000) { isExternalStream = false; outputStream = new FileStream(outputFilePath, FileMode.Append, FileAccess.Write, FileShare.Read); StartCapturing(outputStream, fmt, maxNumberOfPackets); }
/// <summary> /// Takes an IEnumerable of raw packets and exports them to a file in the given format. /// </summary> /// <param name="packets">The raw packets</param> /// <param name="outputFilename">Output filename</param> /// <param name="fmt">The format in which each packet is to be written</param> public static void ExportPackets(IEnumerable<TJPacket> packets, string outputFilePath, TJPacketExportFormat fmt) { FileStream outputStream = new FileStream(outputFilePath, FileMode.Append, FileAccess.Write, FileShare.Read); using (outputStream) { ExportPackets(packets, outputStream, fmt); } }
/// <summary> /// Listens for the Dragonfly's packets and forwards them to the a stream with the given packet format /// </summary> /// <param name="fmt">The format in which each packet is to be written</param> /// <param name="outputStream">The stream to write the packets to. This stream will not be disposed/closed automatically.</param> /// <param name="maxNumberOfPackets">The maximum number of packets to record</param> public void StartCapturing(Stream outputStream, TJPacketExportFormat fmt, int maxNumberOfPackets = 1000000) { if (maxNumberOfPackets <= 0) StopRecording(); this.packetFormat = fmt; this.maxNumberOfPackets = maxNumberOfPackets; TJDragonfly.PacketReceived += TJDragonfly_PacketReceived; }