Skip to content

Conversation

Dimi1010
Copy link
Collaborator

@Dimi1010 Dimi1010 commented May 31, 2025

This PR moves the implementation of the capture procedures to internally linked functions in PcapLiveDevice.cpp and aims to reduce the the need for extensive bookkeeping of capture thread only variables inside the device object's fields.

The static member function callback routines for pcap_dispatch have been replaced with internal linkage free functions to improve readability and encapsulation. The user token is now expected to be a pointer to a specific capture context object instead of the device instance.

Replacement list:

  • onPacketArrivesCallback replaces onPacketArrives. Expects user token of type CaptureContext
  • onPacketArrivesAccumulator replaces onPacketArrivesNoCallback. Expects user token of type AccumulatorCaptureContext
  • onPacketArrivesCallbackWithCancellation replaces onPacketArrivesBlockingMode. Expects user token of type CaptureContextST
  • Added new callback handler onPacketArrivesNoop to be used when no handling is required.

Replaced captureThreadMain static private function with internal linkage free functions. The free functions take their inputs as parameters instead of relying on data from the implicit this pointer, simplifying data ownership and reducing risk of data races.

  • captureThreadMain - used for callback or noop capture.
  • captureThreadMainAccumulator - used for accumulating the packets into a RawPacketVector.

Changed blocking mode capture to utilize onPacketArrivesCallbackWithCancellation. The new infrastructure provides a layer of separation between the device threading machinery and the cancellation requests by relying on requestStop flag to record a cancellation request that is then forwarded to m_StopThread after dispatch finishes instead of directly writing to m_StopThread.

Removed PcapLiveDevice member fields previously only used to store data for the capture thread as that data is now stored in context objects created on demand during capture startup.

Copy link

codecov bot commented May 31, 2025

Codecov Report

❌ Patch coverage is 66.86747% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.38%. Comparing base (b1da390) to head (c107d18).

Files with missing lines Patch % Lines
Pcap++/src/PcapLiveDevice.cpp 66.86% 36 Missing and 19 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1838      +/-   ##
==========================================
- Coverage   83.43%   83.38%   -0.05%     
==========================================
  Files         306      306              
  Lines       54393    54438      +45     
  Branches    11786    12104     +318     
==========================================
+ Hits        45381    45393      +12     
- Misses       7774     8175     +401     
+ Partials     1238      870     -368     
Flag Coverage Δ
alpine320 75.74% <50.92%> (-0.06%) ⬇️
fedora42 75.89% <51.37%> (-0.06%) ⬇️
macos-13 81.61% <63.50%> (-0.04%) ⬇️
macos-14 81.61% <61.31%> (-0.05%) ⬇️
macos-15 81.61% <61.31%> (-0.04%) ⬇️
mingw32 70.12% <46.72%> (-0.10%) ⬇️
mingw64 70.10% <43.92%> (-0.12%) ⬇️
rhel94 75.57% <50.92%> (-0.07%) ⬇️
ubuntu2004 60.05% <54.08%> (-0.01%) ⬇️
ubuntu2004-zstd 60.13% <54.08%> (-0.05%) ⬇️
ubuntu2204 75.51% <50.92%> (-0.07%) ⬇️
ubuntu2204-icpx 60.80% <48.90%> (-0.11%) ⬇️
ubuntu2404 75.76% <50.92%> (-0.07%) ⬇️
ubuntu2404-arm64 75.39% <50.92%> (-0.04%) ⬇️
unittest 83.38% <66.86%> (-0.05%) ⬇️
windows-2022 85.34% <62.28%> (-0.11%) ⬇️
windows-2025 85.36% <64.75%> (-0.11%) ⬇️
winpcap 85.36% <64.75%> (-0.11%) ⬇️
xdp 53.05% <0.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Dimi1010 Dimi1010 marked this pull request as ready for review May 31, 2025 19:58
@Dimi1010 Dimi1010 requested a review from seladb as a code owner May 31, 2025 19:58
@Dimi1010
Copy link
Collaborator Author

Can we review this? It has been sitting here for a while.

@tigercosmos
Copy link
Collaborator

@Dimi1010 It's a big one... That's why... I will look into this recently.

@tigercosmos tigercosmos requested a review from Copilot July 21, 2025 11:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the PcapLiveDevice capture machinery to improve encapsulation and thread safety by replacing static member function callbacks with internal linkage free functions. The changes move capture-specific state from device member variables into local context objects, reducing the risk of data races and simplifying the threading model.

  • Replaces static member callback functions with internal linkage free functions that use specific context objects
  • Eliminates thread-specific member variables from the device class in favor of local context objects
  • Consolidates the StatisticsUpdateWorker class into a simpler thread function approach

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
PcapLiveDevice.cpp Implements new internal capture context structures and free functions, removes StatisticsUpdateWorker class
PcapLiveDevice.h Removes StatisticsUpdateWorker class and capture-related member variables, adds stats thread member

@Dimi1010
Copy link
Collaborator Author

@seladb can you look through this one when you have time? It has been sitting for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants