Mixe for Privacy and Anonymity in the Internet
CASingleSocketGroup.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 __CASINGLESOCKETGROUP__
29 #define __CASINGLESOCKETGROUP__
30 #ifdef _DEBUG
31  #include "CAMsg.hpp"
32 #endif
33 #include "CASocket.hpp"
34 #include "CAMuxSocket.hpp"
36 #if !defined(HAVE_POLL)&&!defined(HAVE_EPOLL)
37  #include "CASocketGroup.hpp"
39  {
40  public:
41  CASingleSocketGroup(bool bWrite):CASocketGroup(bWrite)
42  {
43  }
44 
45  static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
46  {
47  fd_set fdset;
48  FD_ZERO(&fdset);
49  #pragma warning( push )
50  #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
51  FD_SET(s.getSocket(),&fdset);
52  #pragma warning( pop )
53 
54  SINT32 ret;
55  timeval ti;
56  ti.tv_sec=0;
57  ti.tv_usec=time_ms*1000;
58  if(bWrite)
59  ret=::select(1,NULL,&fdset,NULL,&ti);
60  else
61  ret=::select(1,&fdset,NULL,NULL,&ti);
62  if(ret>0)
63  return ret;
64  else if(ret==0)
65  {
67  return E_TIMEDOUT;
68  }
69  return E_UNKNOWN;
70  }
71  };
72 
73 #else
74 #include "CAMuxSocket.hpp"
75 #if defined(HAVE_POLL) && !defined(HAVE_EPOLL)
77  {
78  public:
79  CASingleSocketGroup(bool bWrite)
80  {
81  m_pollfd=new struct pollfd;
82  setPoolForWrite(bWrite);
83  }
85  {
86  delete m_pollfd;
87  m_pollfd = NULL;
88  }
89 
90  SINT32 add(SOCKET s)
91  {
92  m_pollfd->fd = s;
93  return E_SUCCESS;
94  }
95 
97  {
98  m_pollfd->fd=s.getSocket();
99  return E_SUCCESS;
100  }
101 
103  {
104  m_pollfd->fd=s.getSocket();
105  return E_SUCCESS;
106  }
107 
108  SINT32 setPoolForWrite(bool bWrite)
109  {
110  if(bWrite)
111  m_pollfd->events=POLLOUT;
112  else
113  m_pollfd->events=POLLIN;
114  return E_SUCCESS;
115  }
116 
117  SINT32 select()
118  {
119  return ::poll(m_pollfd,1,-1);
120  }
127  SINT32 select(UINT32 time_ms)
128  {
129  SINT32 ret=::poll(m_pollfd,1,time_ms);
130  if(ret>=1)
131  return 1;
132  else if(ret==0)
133  {
135  return E_TIMEDOUT;
136  }
137  #ifdef _DEBUG
138  ret=GET_NET_ERROR;
139  CAMsg::printMsg(LOG_DEBUG,"SocketGroup poll Select-Fehler: %i\n",ret);
140  #endif
141  return E_UNKNOWN;
142  }
143 
144  static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
145  {
146  struct pollfd pollfd;
147  if(bWrite)
148  pollfd.events=POLLOUT;
149  else
150  pollfd.events=POLLIN;
151  pollfd.fd=s.getSocket();
152  SINT32 ret=::poll(&pollfd,1,time_ms);
153  if(ret>=1)
154  return 1;
155  else if(ret==0)
156  {
158  return E_TIMEDOUT;
159  }
160  return E_UNKNOWN;
161  }
162 
163  private:
164  struct pollfd* m_pollfd;
165  };
166 #elif defined(HAVE_EPOLL)
168  {
169  public:
170  CASingleSocketGroup(bool bWrite)
171  {
172  m_hEPFD=epoll_create(1);
173  m_pEvents = new struct epoll_event;
174  memset(m_pEvents, 0, sizeof(struct epoll_event));
175  setPoolForWrite(bWrite);
176  }
177 
179  {
180  epoll_close(m_hEPFD);
181  }
182 
183 
184 
185  SINT32 add(CASocket&s)
186  {
187  return add(s.getSocket());
188  }
189 
190  SINT32 add(SOCKET s)
191  {
192  if (epoll_ctl(m_hEPFD, EPOLL_CTL_ADD, s, m_pEvents) != 0)
193  return E_UNKNOWN;
194  return E_SUCCESS;
195  }
196 
198  {
199  return add(*(s.getCASocket()));
200  }
201 
202  SINT32 setPoolForWrite(bool bWrite)
203  {
204  if(bWrite)
205  m_pEvents->events=POLLOUT|POLLERR|POLLHUP;
206  else
207  m_pEvents->events=POLLIN|POLLERR|POLLHUP;
208  return E_SUCCESS;
209  }
210 
211  SINT32 select()
212  {
213  return ::epoll_wait(m_hEPFD,m_pEvents,1,-1);
214  }
215 
216  SINT32 select(UINT32 time_ms)
217  {
218  SINT32 ret=::epoll_wait(m_hEPFD,m_pEvents,1,time_ms);
219  if(ret>=1)
220  return 1;
221  else if(ret==0)
222  {
224  return E_TIMEDOUT;
225  }
226  #ifdef _DEBUG
227  ret=GET_NET_ERROR;
228  CAMsg::printMsg(LOG_DEBUG,"SingleSocketGroup epoll_wait Select-Fehler: %i\n",ret);
229  #endif
230  return E_UNKNOWN;
231  }
232 
233  /* static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
234  {
235  struct epool_events events;
236  if(bWrite)
237  events.events=POLLOUT|POLLERR|POLLHUP;
238  else
239  events.events=POLLIN|POLLERR|POLLHUP;
240  epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,s.getSocket(),&events);
241  SINT32 ret=::epoll_wait(m_hEPFD,&events,1,time_ms);
242  if(ret>=1)
243  return 1;
244  else if(ret==0)
245  {
246  SET_NET_ERROR(E_TIMEDOUT);
247  return E_TIMEDOUT;
248  }
249  return E_UNKNOWN;
250  }
251 */
252  private:
253  struct epoll_event* m_pEvents;
254  EPOLL_HANDLE m_hEPFD;
255  };
256 #endif
257 #endif
258 #endif
#define GET_NET_ERROR
Definition: StdAfx.h:469
#define epoll_close
Definition: StdAfx.h:434
#define SET_NET_ERROR(x)
Definition: StdAfx.h:470
#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
SOCKET getSocket()
Definition: CAMuxSocket.hpp:92
CASocket * getCASocket()
Definition: CAMuxSocket.hpp:84
CASingleSocketGroup(bool bWrite)
static SINT32 select_once(CASocket &s, bool bWrite, UINT32 time_ms)
SINT32 setPoolForWrite(bool bWrite)
SINT32 add(SOCKET &s)
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