Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetSocketSet.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cNetSocketSet_H
7 #define _INC_cNetSocketSet_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cNetSocket.h"
14 
15 namespace GrayLib
16 {
18  {
24 
25  public:
26  static const int k_nSocketSetSize;
27  fd_set m_fds;
28 
29  public:
31  {
32  ClearSockets();
33  }
34  cNetSocketSet(SOCKET hSocket)
35  {
37  ClearSockets();
38  AddSocket(hSocket);
39  }
40  cNetSocketSet(const cNetSocket& socket)
41  {
43  ClearSockets();
44  AddSocket(socket.get_HSocket());
45  }
46  cNetSocketSet(const fd_set* pfds)
47  {
49  ClearSockets();
50  CopySockets(pfds);
51  }
53  {
55  ClearSockets();
56  CopySockets(&nss.m_fds);
57  }
59  {
60  }
61 
62  void operator = (const fd_set* pfds)
63  {
64  CopySockets(pfds);
65  }
66  void operator = (const cNetSocketSet& nss)
67  {
68  CopySockets(&nss.m_fds);
69  }
70 
71  void CopySockets(const fd_set* pfds)
72  {
74  memcpy(&m_fds, pfds, sizeof(m_fds));
75  }
76 
77  bool AddSocket(SOCKET hSocket);
78  void RemoveSocket(SOCKET hSocket);
79 
80  bool IsSocketSet(SOCKET hSocket) const
81  {
83  if (hSocket == INVALID_SOCKET)
84  return false;
85  return FD_ISSET(hSocket, &m_fds) ? true : false;
86  }
87  void ClearSockets() noexcept
88  {
90  FD_ZERO(&m_fds);
91  }
92  int get_NFDS() const noexcept
93  {
96 #ifdef _WIN32
97  return m_fds.fd_count;
98 #else
99  return FD_SETSIZE + 1; // __linux__ equiv?? k_nSocketSetSize
100 #endif
101  }
102  void put_NFDS(int iCount) noexcept
103  {
105 #ifdef _WIN32
106  m_fds.fd_count = iCount;
107 #endif
108  }
109 
110  operator fd_set*()
111  {
112  return &m_fds;
113  }
114  operator const fd_set*() const
115  {
116  return &m_fds;
117  }
118 
119  static int GRAYCALL Select(fd_set* pReadSet, fd_set* pWriteSet = nullptr, fd_set* pExceptSet = nullptr, const cTimeVal* pTimeout = nullptr);
120  };
121 };
122 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cNetSocketSet.h:18
cNetSocketSet(const cNetSocketSet &nss)
Definition: cNetSocketSet.h:52
void put_NFDS(int iCount) noexcept
Definition: cNetSocketSet.h:102
cNetSocketSet(const fd_set *pfds)
Definition: cNetSocketSet.h:46
int get_NFDS() const noexcept
Definition: cNetSocketSet.h:92
static const int k_nSocketSetSize
= FD_SETSIZE. Max number of sockets i can add to this array.
Definition: cNetSocketSet.h:26
void CopySockets(const fd_set *pfds)
Definition: cNetSocketSet.h:71
cNetSocketSet()
Definition: cNetSocketSet.h:30
cNetSocketSet(SOCKET hSocket)
Definition: cNetSocketSet.h:34
~cNetSocketSet()
Definition: cNetSocketSet.h:58
void ClearSockets() noexcept
Definition: cNetSocketSet.h:87
fd_set m_fds
array of FD_SETSIZE possible SOCKET(s). NOTE: sizeof(m_fds) varies with FD_SETSIZE
Definition: cNetSocketSet.h:27
bool IsSocketSet(SOCKET hSocket) const
Definition: cNetSocketSet.h:80
cNetSocketSet(const cNetSocket &socket)
Definition: cNetSocketSet.h:40
Definition: cNetSocket.h:185
SOCKET get_HSocket() const noexcept
Definition: cNetSocket.h:340
Definition: cTimeVal.h:20
Definition: cMesh.h:22