Mixe for Privacy and Anonymity in the Internet
CASocketGroupEpoll.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #ifndef __CASOCKETGOROPEPOLL__
29 #define __CASOCKETGOROPEPOLL__
30 #include "doxygen.h"
31 #ifdef HAVE_EPOLL
32 #include "CASocket.hpp"
33 #include "CAMuxSocket.hpp"
34 #include "CAMutex.hpp"
35 #ifdef DEBUG
36  #include "CAMsg.hpp"
37 #endif
38 
39 /*** A socket group implemented using epoll() call. You can use a socket group to query a et of sockets
40 for new events.
41 ***/
43  {
44  public:
45  CASocketGroupEpoll(bool bWrite);
47  SINT32 setPoolForWrite(bool bWrite);
48 
51  {
52  return add(s,NULL);
53  }
54 
57  {
58  return add(s, NULL);
59  }
60 
61  /*** Adds the socket s to the socket group. Additional one can set a parameter datapointer, which is
62  * assoziated with the socket s.
63  Note: if datapointer!=NULL than you cannot use the isSignaled(socket) functions.!
64  ***/
65  SINT32 add(CASocket&s,void * datapointer)
66  {
67  SINT32 ret=E_SUCCESS;
68  m_pcsFD_SET->lock();
69  SOCKET socket=s.getSocket();
70  if (datapointer != NULL)
71  m_pEpollEvent->data.ptr = datapointer;
72  else
73  m_pEpollEvent->data.fd = socket;
74 
75  if (epoll_ctl(m_hEPFD, EPOLL_CTL_ADD, socket, m_pEpollEvent) != 0)
76  {
77  SINT32 err = GETERROR;
78  if (err == EEXIST)
79  {
80  ret=E_ALREADY_CONTAINED;
81  }
82  else
83  {
84  CAMsg::printMsg(LOG_DEBUG, "Failed to add socket %i to epoll group --- errno: %i\n", socket, err);
85  ret = E_UNKNOWN;
86  }
87  }
89  return ret;
90  }
91 
92 
95  SINT32 add(CAMuxSocket&s,void* datapointer)
96  {
97  return add(*(s.getCASocket()),datapointer);
98  }
99 
101  {
102  SINT32 ret=E_SUCCESS;
103  m_pcsFD_SET->lock();
104  if(epoll_ctl(m_hEPFD,EPOLL_CTL_DEL,s.getSocket(),m_pEpollEvent)!=0)
105  ret=E_UNKNOWN;
106  ASSERT(ret==E_SUCCESS,"Error in Epoll socket group remove")
107  m_pcsFD_SET->unlock();
108  return ret;
109  }
110 
112  {
113  return remove(*(s.getCASocket()));
114  }
115 
117  {
118  m_pcsFD_SET->lock();
120  if(m_iNumOfReadyFD>0)
121  {
122  m_pcsFD_SET->unlock();
123  return m_iNumOfReadyFD;
124  }
125  m_pcsFD_SET->unlock();
126  return E_UNKNOWN;
127  }
128 
137  {
138  m_pcsFD_SET->lock();
139  m_iNumOfReadyFD=epoll_wait(m_hEPFD,m_pEvents,MAX_POLLFD,time_ms);
140  if(m_iNumOfReadyFD>0)
141  {
142  m_pcsFD_SET->unlock();
143  return m_iNumOfReadyFD;
144  }
145  else if(m_iNumOfReadyFD==0)
146  {
147  m_pcsFD_SET->unlock();
148  return E_TIMEDOUT;
149  }
150  m_pcsFD_SET->unlock();
151  return E_UNKNOWN;
152  }
153 
154  /*** Note: only works, if datapointer was not set!***/
156  {
157  SINT32 socket=s.getSocket();
158  for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
159  {
160  if(socket==m_pEvents[i].data.fd)
161  return true;
162  }
163  return false;
164  }
165 
166  /*** Note: only works, if datapointer was not set!***/
168  {
169  SINT32 socket=ps->getSocket();
170  for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
171  {
172  if(socket==m_pEvents[i].data.fd)
173  return true;
174  }
175  return false;
176  }
177 
178  /*** Note: only works, if datapointer was not set!***/
180  {
181  SINT32 socket=s.getSocket();
182  for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
183  {
184  if(socket==m_pEvents[i].data.fd)
185  return true;
186  }
187  return false;
188  }
189 
190  /*** Note: only works, if datapointer was set!***/
191  bool isSignaled(void* datapointer)
192  {
193  for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
194  {
195  if(datapointer==m_pEvents[i].data.ptr)
196  return true;
197  }
198  return false;
199  }
200 
201  /*** Note: only works, if datapointer was set!***/
203  {
205  if(m_iNumOfReadyFD>0)
206  return m_pEvents[0].data.ptr;
207  return NULL;
208  }
209 
210  /*** Note: only works, if datapointer was set!***/
212  {
215  return m_pEvents[m_iAktSignaledSocket].data.ptr;
216  return NULL;
217  }
218 
219  private:
220  EPOLL_HANDLE m_hEPFD; //the EPoll file descriptor
221  struct epoll_event* m_pEvents;
222  struct epoll_event* m_pEpollEvent;
226  };
227 #endif
228 #endif
#define GETERROR
Definition: StdAfx.h:473
#define ASSERT(cond, msg)
Definition: StdAfx.h:546
#define MAX_POLLFD
Definition: StdAfx.h:192
#define EPOLL_HANDLE
Definition: StdAfx.h:433
#define SOCKET
Definition: StdAfx.h:460
signed int SINT32
Definition: basetypedefs.h:132
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
SOCKET getSocket()
Definition: CAMuxSocket.hpp:92
CASocket * getCASocket()
Definition: CAMuxSocket.hpp:84
SINT32 add(CAMuxSocket &s)
Adds the socket s to the socket group.
bool isSignaled(CAMuxSocket &s)
SINT32 remove(CAMuxSocket &s)
SINT32 remove(CASocket &s)
CASocketGroupEpoll(bool bWrite)
bool isSignaled(CASocket &s)
bool isSignaled(void *datapointer)
SINT32 add(CASocket &s, void *datapointer)
SINT32 add(CAMuxSocket &s, void *datapointer)
Adds the socket s to the socket group.
struct epoll_event * m_pEvents
struct epoll_event * m_pEpollEvent
SINT32 select(UINT32 time_ms)
Waits for events on the sockets.
bool isSignaled(CASocket *ps)
SINT32 setPoolForWrite(bool bWrite)
SINT32 add(CASocket &s)
Adds the socket s to the socket group.
SOCKET getSocket()
Returns the number of the Socket used.
Definition: CASocket.hpp:87
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
#define E_TIMEDOUT
Definition: errorcodes.hpp:10
UINT8 data[PAYLOAD_SIZE]
Definition: typedefs.hpp:2