0

I'm writing a console app containing an Ethernet protocol analyzing function in C++/Visual Studio 2019 Community Edition that I wish to call from two different scenarios or use cases (which never come mixed, it is either or).

  1. When (in realtime) receiving a frame from an Ethernet socket
  2. When reading a previously recorded frame from a pcap-file (Ethernet recording)

The analysis part of the function need, among other things, to keep track on the period of time passed between two frames and do things differently based on delays, etc.

Case 1 is easy; tag each frame with a timepoint from std::chrono::steady_clock::now() and calculate duration by simple subtraction; delay = t2 - t1;

Case 2 is likewise easy; convert the struct timeval record part of the captured frame into a timepoint using std::chrono::system_clock::from_time_t().

Instead of maintaining two different (complex) functions (one based on steady_clock and another twin based on system_clock) I wish to have an "overloaded" function that handles both.

I've been looking for conversion between the two clocks but have experienced unexplainable effects, offsets, etc. (I could provide examples but I refrain from exposing my, I assume beginner's mistakes on Chrono)

A senior colleague sent me some Howard Hinnant threads and YT recordings which I am very grateful to have received and watched. I believe I have got what a Duration and a Timepoint is and is properly "scared" of using .Count() and other "exits"... ;-)

However, I have not come across any topic nor good example of how to mix/convert between different clocks. At present I have fallen back on having two different versions of the function; one using steady_clock & realtime data from sockets and another using system_clock & recorded data read from file.

  • 1
    Will you consider "static overload" (e.g. using a template)? – warchantua Jun 14 '21 at 19:54
  • 1
    Why not make the clock a parameter to the function and then you just use the provided clock? You can use a template (if you aren't already) so that you can make the clock type a template parameter. This will also let your function work with different types of clocks if that is needed in the future, as long as they have the same interface. – NathanOliver Jun 14 '21 at 19:55

0 Answers0