Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c8e567d
Moved callbacks to anonymous functions local to the translation unit.
Dimi1010 May 31, 2025
9ed6ee9
Moved user callback validity checks to the start of the callbacks.
Dimi1010 May 31, 2025
1954ebc
Changed captureThreadMain to an internal function.
Dimi1010 May 31, 2025
e02c6b4
Fixed callback for statistics only capture.
Dimi1010 May 31, 2025
496938e
Changed isRunning flag to hasStarted.
Dimi1010 May 31, 2025
312247b
Replaced statistics worker with a procedure.
Dimi1010 May 31, 2025
35542a0
Added nullptr defaults to contexts.
Dimi1010 May 31, 2025
4483885
Added try catch blocks to catch exceptions bubbling out of dispatch c…
Dimi1010 Jun 1, 2025
e35b188
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Jun 27, 2025
32fb349
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Jul 2, 2025
2f39f93
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Jul 21, 2025
573be14
Updated statistics to use chrono duration directly.
Dimi1010 Jul 24, 2025
01daa47
Replaced types with `auto`.
Dimi1010 Jul 24, 2025
dc1984c
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Jul 24, 2025
1d2905e
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Jul 27, 2025
9864e4c
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Aug 6, 2025
0505cd8
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Aug 14, 2025
425c8c3
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Sep 2, 2025
c107d18
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Sep 6, 2025
9c4f480
Merge remote-tracking branch 'upstream/dev' into refactor/standalone-…
Dimi1010 Sep 15, 2025
823f745
Merge branch 'dev' into refactor/standalone-capture-callbacks
Dimi1010 Sep 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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