PcapPlusPlus  Next
SystemUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DeprecationUtils.h"
4 
5 #include <cstdint>
6 #include <string>
7 #include <vector>
8 
10 
11 // @todo Change to constexpr when C++17 is minimum supported version
12 enum : uint8_t
13 {
14  MAX_NUM_OF_CORES = 32
15 };
16 
17 #ifdef _MSC_VER
18 int gettimeofday(struct timeval* tp, struct timezone* tzp);
19 #endif
20 
23 namespace pcpp
24 {
25 
28  struct SystemCore
29  {
30 
35  uint32_t Mask;
36 
38  uint8_t Id;
39 
42  bool operator==(const SystemCore& other) const
43  {
44  return Id == other.Id;
45  }
46  };
47 
51  struct SystemCores
52  {
54  static const SystemCore Core0;
56  static const SystemCore Core1;
58  static const SystemCore Core2;
60  static const SystemCore Core3;
62  static const SystemCore Core4;
64  static const SystemCore Core5;
66  static const SystemCore Core6;
68  static const SystemCore Core7;
70  static const SystemCore Core8;
72  static const SystemCore Core9;
74  static const SystemCore Core10;
76  static const SystemCore Core11;
78  static const SystemCore Core12;
80  static const SystemCore Core13;
82  static const SystemCore Core14;
84  static const SystemCore Core15;
86  static const SystemCore Core16;
88  static const SystemCore Core17;
90  static const SystemCore Core18;
92  static const SystemCore Core19;
94  static const SystemCore Core20;
96  static const SystemCore Core21;
98  static const SystemCore Core22;
100  static const SystemCore Core23;
102  static const SystemCore Core24;
104  static const SystemCore Core25;
106  static const SystemCore Core26;
108  static const SystemCore Core27;
110  static const SystemCore Core28;
112  static const SystemCore Core29;
114  static const SystemCore Core30;
116  static const SystemCore Core31;
118  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
119  };
120 
121  using CoreMask = uint32_t;
122 
126 
130 
134  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
135 
139  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
140 
144  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
145 
150  std::string executeShellCommand(const std::string& command);
151 
155  bool directoryExists(const std::string& dirPath);
156 
162  int clockGetTime(long& sec, long& nsec);
163 
169  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::seconds(seconds)) instead")
170  void multiPlatformSleep(uint32_t seconds);
171 
177  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)) instead")
178  void multiPlatformMSleep(uint32_t milliseconds);
179 
183  uint16_t hostToNet16(uint16_t host);
184 
188  uint16_t netToHost16(uint16_t net);
189 
193  uint32_t hostToNet32(uint32_t host);
194 
198  uint32_t netToHost32(uint32_t net);
199 
204  class AppName
205  {
206  private:
207  static std::string m_AppName;
208 
209  public:
213  // cppcheck-suppress constParameter
214  static void init(int argc, char* argv[])
215  {
216  if (argc == 0)
217  {
218  m_AppName.clear();
219  return;
220  }
221 
222  m_AppName = argv[0];
223 
224  // remove Linux/Unix path
225  size_t lastPos = m_AppName.rfind('/');
226  if (lastPos != std::string::npos)
227  {
228  m_AppName = m_AppName.substr(lastPos + 1);
229  }
230 
231  // remove Windows path
232  lastPos = m_AppName.rfind('\\');
233  if (lastPos != std::string::npos)
234  {
235  m_AppName = m_AppName.substr(lastPos + 1);
236  }
237 
238  // remove file extension
239  lastPos = m_AppName.rfind('.');
240  if (lastPos != std::string::npos)
241  {
242  m_AppName.resize(lastPos);
243  }
244  }
245 
247  static const std::string& get()
248  {
249  return m_AppName;
250  }
251  };
252 
257  {
258  public:
262  using EventHandlerCallback = void (*)(void*);
263 
267  {
268  static ApplicationEventHandler instance;
269  return instance;
270  }
271 
277  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
278 
279  private:
280  EventHandlerCallback m_ApplicationInterruptedHandler;
281  void* m_ApplicationInterruptedCookie;
282 
283  // private c'tor
285 
286 #if defined(_WIN32)
287  static int handlerRoutine(unsigned long fdwCtrlType);
288 #else
289  static void handlerRoutine(int signum);
290 #endif
291  };
292 
293 } // namespace pcpp
Definition: SystemUtils.h:205
static void init(int argc, char *argv[])
Definition: SystemUtils.h:214
static const std::string & get()
Definition: SystemUtils.h:247
Definition: SystemUtils.h:257
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:266
void(*)(void *) EventHandlerCallback
Definition: SystemUtils.h:262
void onApplicationInterrupted(EventHandlerCallback handler, void *cookie)
The main namespace for the PcapPlusPlus lib.
CoreMask createCoreMaskFromCoreIds(const std::vector< int > &coreIds)
void multiPlatformMSleep(uint32_t milliseconds)
std::string executeShellCommand(const std::string &command)
CoreMask createCoreMaskFromCoreVector(const std::vector< SystemCore > &cores)
CoreMask getCoreMaskForAllMachineCores()
void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector< SystemCore > &resultVec)
uint32_t netToHost32(uint32_t net)
int getNumOfCores()
uint16_t hostToNet16(uint16_t host)
uint32_t hostToNet32(uint32_t host)
uint16_t netToHost16(uint16_t net)
int clockGetTime(long &sec, long &nsec)
void multiPlatformSleep(uint32_t seconds)
bool directoryExists(const std::string &dirPath)
Definition: SystemUtils.h:29
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:42
uint32_t Mask
Definition: SystemUtils.h:35
uint8_t Id
Core ID - a value between 0 and 31.
Definition: SystemUtils.h:38
Definition: SystemUtils.h:52
static const SystemCore Core9
Static representation of core #9.
Definition: SystemUtils.h:72
static const SystemCore Core16
Static representation of core #16.
Definition: SystemUtils.h:86
static const SystemCore Core15
Static representation of core #15.
Definition: SystemUtils.h:84
static const SystemCore Core29
Static representation of core #29.
Definition: SystemUtils.h:112
static const SystemCore Core26
Static representation of core #26.
Definition: SystemUtils.h:106
static const SystemCore Core13
Static representation of core #13.
Definition: SystemUtils.h:80
static const SystemCore Core22
Static representation of core #22.
Definition: SystemUtils.h:98
static const SystemCore Core10
Static representation of core #10.
Definition: SystemUtils.h:74
static const SystemCore Core8
Static representation of core #8.
Definition: SystemUtils.h:70
static const SystemCore Core1
Static representation of core #1.
Definition: SystemUtils.h:56
static const SystemCore Core23
Static representation of core #23.
Definition: SystemUtils.h:100
static const SystemCore Core17
Static representation of core #17.
Definition: SystemUtils.h:88
static const SystemCore Core14
Static representation of core #14.
Definition: SystemUtils.h:82
static const SystemCore Core20
Static representation of core #20.
Definition: SystemUtils.h:94
static const SystemCore Core3
Static representation of core #3.
Definition: SystemUtils.h:60
static const SystemCore Core27
Static representation of core #27.
Definition: SystemUtils.h:108
static const SystemCore Core30
Static representation of core #30.
Definition: SystemUtils.h:114
static const SystemCore Core18
Static representation of core #18.
Definition: SystemUtils.h:90
static const SystemCore Core0
Static representation of core #0.
Definition: SystemUtils.h:54
static const SystemCore Core12
Static representation of core #12.
Definition: SystemUtils.h:78
static const SystemCore Core24
Static representation of core #24.
Definition: SystemUtils.h:102
static const SystemCore Core25
Static representation of core #25.
Definition: SystemUtils.h:104
static const SystemCore Core11
Static representation of core #11.
Definition: SystemUtils.h:76
static const SystemCore Core28
Static representation of core #28.
Definition: SystemUtils.h:110
static const SystemCore Core7
Static representation of core #7.
Definition: SystemUtils.h:68
static const SystemCore Core19
Static representation of core #19.
Definition: SystemUtils.h:92
static const SystemCore Core4
Static representation of core #4.
Definition: SystemUtils.h:62
static const SystemCore Core31
Static representation of core #31.
Definition: SystemUtils.h:116
static const SystemCore Core2
Static representation of core #2.
Definition: SystemUtils.h:58
static const SystemCore Core5
Static representation of core #5.
Definition: SystemUtils.h:64
static const SystemCore Core6
Static representation of core #6.
Definition: SystemUtils.h:66
static const SystemCore Core21
Static representation of core #21.
Definition: SystemUtils.h:96
static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES]
A static array for mapping core ID (integer) to the corresponding static SystemCore representation.
Definition: SystemUtils.h:118