Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetSystem.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cNetSystem_H
7 #define _INC_cNetSystem_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
15 
16 #ifdef _WIN32
17 #include <winsock2.h> // need to declare _WINSOCKAPI_ before windows.h and link with ws2_32.lib
18 #include <ws2tcpip.h> // getaddrinfo addrinfo ADDRINFOA in6_addr
19 
20 #elif defined(__linux__)
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <netdb.h> // hostent
26 #include <netinet/in.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <arpa/inet.h>
30 #include <signal.h>
31 //#include <ctype.h>
32 
33 struct WSADATA
34 {
35  // __linux__ Compatibility definition.
36  // socket stack version?
37 };
38 #else // __linux__
39 #error NOOS
40 #endif
41 
42 namespace GrayLib
43 {
44  class GRAYLIB_LINK cNetSystem : public cSingletonSmart<cNetSystem>
45  {
50 
51  friend class cSingleton<cNetSystem>;
52 
53  private:
54  WSADATA m_NetData;
55  HRESULT m_hResInitNet;
56 
57  protected:
58  cNetSystem();
59  ~cNetSystem();
60 
61  public:
62  bool isInitNet() const noexcept
63  {
65  if (!cMem::IsValidApp(this))
66  return false;
67 #ifdef _WIN32
68  return SUCCEEDED(m_hResInitNet);
69 #else
70  return true;
71 #endif
72  }
73  const WSADATA* get_NetData() const noexcept
74  {
75  if (FAILED(m_hResInitNet))
76  return nullptr;
77  return &m_NetData;
78  }
79 
80  static inline bool IsInitNetCalls()
81  {
84 #ifdef _WIN32
85  return isSingleCreated() && I().isInitNet();
86 #else
87  return true;
88 #endif
89  }
90  static inline HRESULT GetLastError()
91  {
95 #if defined(_WIN32) && ! defined(UNDER_CE)
96  // WSABASEERR
97  LSTATUS iErrorCode = ::WSAGetLastError();
98  return HResult::FromWin32(iErrorCode);
99 #else
100  // Just use the normal GetLastError
101  // @return errno = EAGAIN, EWOULDBLOCK, etc
102  return HResult::GetLast();
103 #endif
104  }
105  static inline HRESULT GetLastErrorDef(HRESULT hResDef = E_FAIL) // static
106  {
108  return HResult::GetDef(GetLastError(), hResDef);
109  }
110  };
111 
112  typedef cSingletonPtr<cNetSystem> cNetSystemPtr; // Reference the network system for the life of the object.
113 };
114 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define SUCCEEDED(x)
Definition: HResult.h:29
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cNetSystem.h:45
const WSADATA * get_NetData() const noexcept
Definition: cNetSystem.h:73
static HRESULT GetLastErrorDef(HRESULT hResDef=E_FAIL)
Definition: cNetSystem.h:105
bool isInitNet() const noexcept
Definition: cNetSystem.h:62
static bool IsInitNetCalls()
Definition: cNetSystem.h:80
static HRESULT GetLastError()
Definition: cNetSystem.h:90
static HRESULT __stdcall GetLast() noexcept
Definition: HResult.cpp:130
static HRESULT GetDef(HRESULT hRes, HRESULT hResDef=E_FAIL) noexcept
Definition: HResult.h:232
static HRESULT FromWin32(DWORD dwWin32Code) noexcept
Definition: HResult.h:198
Definition: cSingletonPtr.h:20
Definition: cSingleton.h:127
Definition: cMesh.h:22
cSingletonPtr< cNetSystem > cNetSystemPtr
Definition: cNetSystem.h:112
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
static bool IsValidApp(const void *pData) noexcept
Definition: cMem.h:42