SysUtils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __SYSUTILS_H
  6. #define __SYSUTILS_H
  7. #include "BaseTypes.h"
  8. #include "BaseHeaders.h"
  9. #include "Network.h"
  10. #include "SockAddress.h"
  11. #include "Locks.h"
  12. namespace tzc {
  13. // process schedule policy
  14. enum __POLICY {
  15. POLICY_OTHER = SCHED_OTHER,
  16. POLICY_FIFO = SCHED_FIFO,
  17. POLICY_RR = SCHED_RR,
  18. DEFAULT_POLICY = POLICY_OTHER
  19. };
  20. // process schedule priority
  21. enum __PRIORITY {
  22. PRIO_LOWEST,
  23. PRIO_LOW,
  24. PRIO_NORMAL,
  25. PRIO_HIGHEST,
  26. PRIO_HIGH,
  27. DEFAULT_PRIO = PRIO_NORMAL
  28. };
  29. // thread privacy stack size
  30. enum __STACKSIZE {
  31. STACK_MIN = (16 << 10),
  32. STACK_NORMAL = (2 << 20),
  33. STACK_MAX = (16 << 20),
  34. DEFAULT_STACKSIZE = STACK_NORMAL
  35. };
  36. class DECLDLL SysUtils {
  37. private:
  38. SysUtils();
  39. ~SysUtils();
  40. public:
  41. static TZ_Int32 GetOSMinPriority(TZ_Int32 policy = POLICY_OTHER);
  42. static TZ_Int32 GetOSMaxPriority(TZ_Int32 policy = POLICY_OTHER);
  43. static TZ_Int32 MapPriority(TZ_Int32 prio, TZ_Int32 policy);
  44. static TZ_Int32 ReverseMapPriority(TZ_Int32 prio, TZ_Int32 policy);
  45. static TZ_Int32 GetCPUNumber();
  46. static TZ_Int32 InvalidCPU(const TZ_Int32 iIndex);
  47. static TZ_Int32 DelaySeconds(const TZ_Int32 iCount);
  48. static TZ_Int32 DelayMseconds(const TZ_Int32 iCount);
  49. static TZ_Int32 DelayUseconds(const TZ_Int32 iCount);
  50. static TZ_Int32 GetSysRouteTable(RouteTable & routeTable);
  51. static TZ_Int32 GetSysNICsTable(NICsTable & NICsTable);
  52. static TZ_BOOL IsLinkFile(const std::string & sPath);
  53. static TZ_BOOL IsDeviceFile(const std::string & sPath);
  54. static TZ_BOOL IsHiddenFile(const std::string & sPath);
  55. static TZ_BOOL IsDirectoryFile(const std::string & sPath);
  56. // return 0 if failed
  57. static TZ_Uint16 GetAvailPortTCP(const TZ_CHAR * szIPAddr,
  58. const TZ_Uint16 iBegin = 0, const TZ_Uint16 iEnd = 0);
  59. static TZ_Uint16 GetAvailPortUDP(const TZ_CHAR * szIPAddr,
  60. const TZ_Uint16 iBegin = 0, const TZ_Uint16 iEnd = 0);
  61. static TZ_DLLHD LoadLibrary(const std::string & libpath);
  62. static TZ_PVOID GetSymbol(TZ_DLLHD dllhd, const std::string & symbol);
  63. static void FreeLibrary(TZ_DLLHD dllhd);
  64. static TZ_INT ForkDaemon();
  65. private:
  66. // return 0 if failed
  67. static TZ_Uint16 getAvailPort(TZ_SOCKET iSock,
  68. const TZ_CHAR * szIPAddr,
  69. const TZ_Uint16 iBegin, const TZ_Uint16 iEnd);
  70. static TZ_Int32 getNICAddrInfo(const TZ_ULONG & iIndex,
  71. IPAddress & ipaddr, IPAddress & netmask, IPAddress & broadcast);
  72. static TZ_Int32 getNICAddrInfo(const std::string & sNICName,
  73. IPAddress & ipaddr, IPAddress & netmask, IPAddress & broadcast);
  74. // ...
  75. private:
  76. static Mutex m_portMutex;
  77. };
  78. } // namespace tzc
  79. #endif /* ----- #ifndef __SYSUTILS_H ----- */