Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
pcap.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstdio>
5#include <expected>
6#include <filesystem>
7#include <span>
8#include <string>
9#include <vector>
10
16
17namespace mm::capture {
18
20struct Record {
21 int64_t timestampNs = 0;
22 std::vector<uint8_t> bytes;
23};
24
27 public:
28 PcapWriter() = default;
30 PcapWriter(const PcapWriter&) = delete;
31 PcapWriter& operator=(const PcapWriter&) = delete;
32
34 std::expected<void, std::string> open(const std::filesystem::path& path);
35
37 std::expected<void, std::string> write(int64_t timestampNs, std::span<const uint8_t> bytes);
38
40 void close();
41
42 private:
43 std::FILE* file_ = nullptr;
44};
45
51std::expected<std::vector<Record>, std::string> parsePcap(std::span<const uint8_t> bytes);
52
55std::expected<std::vector<Record>, std::string> readPcap(const std::filesystem::path& path);
56
57} // namespace mm::capture
Writes records to a pcap file as they arrive.
Definition pcap.h:26
~PcapWriter()
Definition pcap.cc:45
std::expected< void, std::string > write(int64_t timestampNs, std::span< const uint8_t > bytes)
Appends one frame.
Definition pcap.cc:68
PcapWriter & operator=(const PcapWriter &)=delete
std::expected< void, std::string > open(const std::filesystem::path &path)
Creates path and writes the file header, with nanosecond timestamps.
Definition pcap.cc:47
PcapWriter(const PcapWriter &)=delete
void close()
Flushes and closes the file. Called by the destructor when not called before.
Definition pcap.cc:85
Definition capture.h:14
std::expected< std::vector< Record >, std::string > parsePcap(std::span< const uint8_t > bytes)
Reads every record of a pcap file held in memory.
Definition pcap.cc:92
std::expected< std::vector< Record >, std::string > readPcap(const std::filesystem::path &path)
Reads every record of a pcap file into memory. See parsePcap for what is accepted.
Definition pcap.cc:133
std::size_t bytes
Definition spoe_connection.cc:24
One captured frame.
Definition pcap.h:20
int64_t timestampNs
Nanoseconds since the Unix epoch, from the kernel.
Definition pcap.h:21
std::vector< uint8_t > bytes
The frame from the Ethernet header on.
Definition pcap.h:22