Skip to content

Commit e12fabe

Browse files
authored
Refactor: Use std::chrono for sleep and SystemUtils (#1858)
1 parent a9d69e7 commit e12fabe

File tree

6 files changed

+35
-80
lines changed

6 files changed

+35
-80
lines changed

Common++/src/SystemUtils.cpp

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,18 @@
3131
#endif
3232

3333
#ifdef _MSC_VER
34+
# include <chrono>
3435
int gettimeofday(struct timeval* tp, struct timezone* tzp)
3536
{
36-
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
37-
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
3837

39-
SYSTEMTIME system_time;
40-
FILETIME file_time;
41-
uint64_t time;
38+
auto now = std::chrono::system_clock::now();
39+
auto duration = now.time_since_epoch();
4240

43-
GetSystemTime(&system_time);
44-
SystemTimeToFileTime(&system_time, &file_time);
45-
time = ((uint64_t)file_time.dwLowDateTime);
46-
time += ((uint64_t)file_time.dwHighDateTime) << 32;
41+
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
42+
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration - seconds);
4743

48-
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
49-
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
44+
tp->tv_sec = static_cast<long>(seconds.count());
45+
tp->tv_usec = static_cast<long>(microseconds.count());
5046
return 0;
5147
}
5248
#endif
@@ -211,63 +207,18 @@ namespace pcpp
211207

212208
int clockGetTime(long& sec, long& nsec)
213209
{
214-
sec = 0;
215-
nsec = 0;
210+
using namespace std::chrono;
216211

217-
#if defined(_WIN32)
218-
219-
# define CLOCK_GETTIME_BILLION (1E9)
220-
221-
static BOOL clock_gettime_first_time = 1;
222-
static LARGE_INTEGER clock_gettime_counts_per_sec;
223-
224-
LARGE_INTEGER count;
225-
226-
if (clock_gettime_first_time)
227-
{
228-
clock_gettime_first_time = 0;
229-
230-
if (0 == QueryPerformanceFrequency(&clock_gettime_counts_per_sec))
231-
{
232-
clock_gettime_counts_per_sec.QuadPart = 0;
233-
}
234-
}
235-
236-
if ((clock_gettime_counts_per_sec.QuadPart <= 0) || (0 == QueryPerformanceCounter(&count)))
237-
{
238-
return -1;
239-
}
240-
241-
sec = count.QuadPart / clock_gettime_counts_per_sec.QuadPart;
242-
nsec = ((count.QuadPart % clock_gettime_counts_per_sec.QuadPart) * CLOCK_GETTIME_BILLION) /
243-
clock_gettime_counts_per_sec.QuadPart;
212+
auto now = system_clock::now();
213+
auto duration = now.time_since_epoch();
244214

245-
return 0;
246-
247-
#elif defined(__APPLE__)
215+
auto secondsDuration = duration_cast<seconds>(duration);
216+
auto nanosecondsDuration = duration_cast<nanoseconds>(duration - secondsDuration);
248217

249-
clock_serv_t cclock;
250-
mach_timespec_t mts;
251-
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
252-
clock_get_time(cclock, &mts);
253-
mach_port_deallocate(mach_task_self(), cclock);
254-
sec = mts.tv_sec;
255-
nsec = mts.tv_nsec;
218+
sec = static_cast<long>(secondsDuration.count());
219+
nsec = static_cast<long>(nanosecondsDuration.count());
256220

257221
return 0;
258-
259-
#else // Linux
260-
261-
timespec tspec{};
262-
const int res = clock_gettime(CLOCK_REALTIME, &tspec);
263-
if (res == 0)
264-
{
265-
sec = tspec.tv_sec;
266-
nsec = tspec.tv_nsec;
267-
}
268-
return res;
269-
270-
#endif
271222
}
272223

273224
time_t mkUtcTime(std::tm& tm)

Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void receiveFile(pcpp::IPv4Address pitcherIP, pcpp::IPv4Address catcherIP, int p
316316

317317
// if rate limit was set by the user, sleep between sending packets
318318
if (packetPerSec > 1)
319-
usleep(sleepBetweenPackets);
319+
std::this_thread::sleep_for(std::chrono::microseconds(sleepBetweenPackets));
320320
else if (packetPerSec == 1)
321321
std::this_thread::sleep_for(std::chrono::seconds(1));
322322

@@ -334,7 +334,7 @@ void receiveFile(pcpp::IPv4Address pitcherIP, pcpp::IPv4Address catcherIP, int p
334334
{
335335
sendIcmpRequest(dev, pitcherMacAddr, catcherMacAddr, pitcherIP, catcherIP, icmpId, ICMP_FT_ABORT,
336336
nullptr, 0);
337-
usleep(SLEEP_BETWEEN_ABORT_MESSAGES);
337+
std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_BETWEEN_ABORT_MESSAGES));
338338
}
339339

340340
file.close();
@@ -502,7 +502,7 @@ void sendFile(const std::string& filePath, pcpp::IPv4Address pitcherIP, pcpp::IP
502502

503503
// use usleep or sleep (see comment a few lines below)
504504
if (packetPerSec > 1)
505-
usleep(sleepBetweenPackets);
505+
std::this_thread::sleep_for(std::chrono::microseconds(sleepBetweenPackets));
506506
else if (packetPerSec == 1)
507507
std::this_thread::sleep_for(std::chrono::seconds(1));
508508

Pcap++/src/DpdkDevice.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "rte_cycles.h"
2121
#include <string>
2222
#include <unistd.h>
23+
#include <chrono>
24+
#include <thread>
2325

2426
#define MAX_BURST_SIZE 64
2527

@@ -1115,7 +1117,7 @@ namespace pcpp
11151117
{
11161118
PCPP_LOG_DEBUG(
11171119
"Since NIC couldn't send all packet in this iteration, waiting for 0.2 second for H/W descriptors to get free");
1118-
usleep(200000);
1120+
std::this_thread::sleep_for(std::chrono::microseconds(200000));
11191121
lastSleep = packetsSent;
11201122
}
11211123
}

Pcap++/src/KniDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "SystemUtils.h"
88

99
#include <unistd.h>
10-
#include <time.h>
1110
#include <thread>
1211
#include <sys/ioctl.h>
1312
#include <net/if.h>
@@ -410,7 +409,8 @@ namespace pcpp
410409
struct rte_kni* kni_dev = device->m_Device;
411410
for (;;)
412411
{
413-
nanosleep(&sleepTime, nullptr);
412+
std::this_thread::sleep_for(std::chrono::seconds(sleepTime.tv_sec) +
413+
std::chrono::nanoseconds(sleepTime.tv_nsec));
414414
rte_kni_handle_request(kni_dev);
415415
if (stopThread)
416416
{

Pcap++/src/NetworkUtils.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ namespace pcpp
3131
std::mutex& mutex;
3232
std::condition_variable& cond;
3333
IPv4Address ipAddr;
34-
clock_t start;
34+
std::chrono::steady_clock::time_point start;
3535
MacAddress result;
3636
double arpResponseTime;
3737
};
3838

3939
static void arpPacketReceived(RawPacket* rawPacket, PcapLiveDevice*, void* userCookie)
4040
{
4141
// extract timestamp of packet
42-
clock_t receiveTime = clock();
42+
auto receiveTime = std::chrono::steady_clock::now();
4343

4444
// get the data from the main thread
4545
ArpingReceivedData* data = (ArpingReceivedData*)userCookie;
@@ -66,8 +66,8 @@ namespace pcpp
6666
return;
6767

6868
// measure response time
69-
double diffticks = receiveTime - data->start;
70-
double diffms = (diffticks * 1000) / CLOCKS_PER_SEC;
69+
auto duration = receiveTime - data->start;
70+
auto diffms = std::chrono::duration<double, std::milli>(duration).count();
7171

7272
data->arpResponseTime = diffms;
7373
data->result = arpReplyLayer->getSenderMacAddress();
@@ -143,7 +143,7 @@ namespace pcpp
143143
// this is the token that passes between the 2 threads. It contains pointers to the conditional mutex, the
144144
// target IP for identifying the ARP response, the iteration index and a timestamp to calculate the response
145145
// time
146-
ArpingReceivedData data = { mutex, cond, ipAddr, clock(), MacAddress::Zero, 0 };
146+
ArpingReceivedData data = { mutex, cond, ipAddr, std::chrono::steady_clock::now(), MacAddress::Zero, 0 };
147147

148148
struct timeval now;
149149
gettimeofday(&now, nullptr);
@@ -186,7 +186,7 @@ namespace pcpp
186186
std::condition_variable& cond;
187187
std::string hostname;
188188
uint16_t transactionID;
189-
clock_t start;
189+
std::chrono::steady_clock::time_point start;
190190
IPv4Address result;
191191
uint32_t ttl;
192192
double dnsResponseTime;
@@ -195,7 +195,7 @@ namespace pcpp
195195
static void dnsResponseReceived(RawPacket* rawPacket, PcapLiveDevice*, void* userCookie)
196196
{
197197
// extract timestamp of packet
198-
clock_t receiveTime = clock();
198+
auto receiveTime = std::chrono::steady_clock::now();
199199

200200
// get data from the main thread
201201
DNSReceivedData* data = (DNSReceivedData*)userCookie;
@@ -268,8 +268,8 @@ namespace pcpp
268268
// if we got here it means an IPv4 resolving was found
269269

270270
// measure response time
271-
clock_t diffticks = receiveTime - data->start;
272-
double diffms = (diffticks * 1000.0) / CLOCKS_PER_SEC;
271+
auto duration = receiveTime - data->start;
272+
auto diffms = std::chrono::duration<double, std::milli>(duration).count();
273273

274274
data->dnsResponseTime = diffms;
275275
data->result = dnsAnswer->getData()->castAs<IPv4DnsResourceData>()->getIpAddress();
@@ -386,7 +386,9 @@ namespace pcpp
386386
std::condition_variable cond;
387387

388388
// this is the token that passes between the 2 threads
389-
DNSReceivedData data = { mutex, cond, hostname, transactionID, clock(), IPv4Address::Zero, 0, 0 };
389+
DNSReceivedData data = {
390+
mutex, cond, hostname, transactionID, std::chrono::steady_clock::now(), IPv4Address::Zero, 0, 0
391+
};
390392

391393
struct timeval now;
392394
gettimeofday(&now, nullptr);

Pcap++/src/PfRingDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ namespace pcpp
831831
"Try #"
832832
<< tries
833833
<< ": Got ENOBUFS (write buffer full) error while sending packet. Sleeping 20 usec and trying again");
834-
usleep(2000);
834+
std::this_thread::sleep_for(std::chrono::microseconds(2000));
835835
}
836836
else
837837
break;

0 commit comments

Comments
 (0)