Skip to content

Cleanup of PcapLiveDevice capture machinery. #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
56 changes: 2 additions & 54 deletions Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,44 +82,6 @@ namespace pcpp
bool isLoopback;
};

/// @brief A worker thread that periodically calls the provided callback with updated statistics.
class StatisticsUpdateWorker
{
public:
/// @brief Constructs and starts a worker thread that periodically calls the provided callback with updated
/// statistics.
/// @param pcapDevice A pointer to the PcapLiveDevice instance to be monitored.
/// @param onStatsUpdateCallback A callback function to be called with updated statistics.
/// @param onStatsUpdateUserCookie A user-defined pointer that is passed to the callback function.
/// @param updateIntervalMs The interval in milliseconds between each callback invocation.
StatisticsUpdateWorker(PcapLiveDevice const& pcapDevice, OnStatsUpdateCallback onStatsUpdateCallback,
void* onStatsUpdateUserCookie = nullptr, unsigned int updateIntervalMs = 1000);

/// @brief Stops the worker thread.
void stopWorker();

private:
struct ThreadData
{
PcapLiveDevice const* pcapDevice = nullptr;
OnStatsUpdateCallback cbOnStatsUpdate;
void* cbOnStatsUpdateUserCookie = nullptr;
unsigned int updateIntervalMs = 1000; // Default update interval is 1 second
};

struct SharedThreadData
{
std::atomic_bool stopRequested{ false };
};

/// @brief Main function for the worker thread.
/// @remarks This function is static to allow the worker class to be movable.
static void workerMain(std::shared_ptr<SharedThreadData> sharedThreadData, ThreadData threadData);

std::shared_ptr<SharedThreadData> m_SharedThreadData;
std::thread m_WorkerThread;
};

// This is a second descriptor for the same device. It is needed because of a bug
// that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
// It's impossible to capture packets sent by the same descriptor
Expand All @@ -131,22 +93,15 @@ namespace pcpp
uint32_t m_DeviceMtu;
MacAddress m_MacAddress;
IPv4Address m_DefaultGateway;
std::thread m_CaptureThread;

// TODO: Cpp17 Using std::optional might be better here
std::unique_ptr<StatisticsUpdateWorker> m_StatisticsUpdateWorker;
std::thread m_CaptureThread;
std::thread m_StatsThread;

// Should be set to true by the Caller for the Callee
std::atomic<bool> m_StopThread;
// Should be set to true by the Callee for the Caller
std::atomic<bool> m_CaptureThreadStarted;

OnPacketArrivesCallback m_cbOnPacketArrives;
void* m_cbOnPacketArrivesUserCookie;
OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
void* m_cbOnPacketArrivesBlockingModeUserCookie;
RawPacketVector* m_CapturedPackets;
bool m_CaptureCallbackMode;
LinkLayerType m_LinkType;
bool m_UsePoll;

Expand All @@ -162,13 +117,6 @@ namespace pcpp
void setDeviceMacAddress();
void setDefaultGateway();

// threads
void captureThreadMain();

static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);

public:
/// The type of the live device
enum LiveDeviceType
Expand Down
Loading
Loading