Mixe for Privacy and Anonymity in the Internet
CASocketGroup.cpp
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 
29 #include "StdAfx.h"
30 #include "CASocket.hpp"
31 #include "CASocketGroup.hpp"
32 //#ifdef _DEBUG
33  #include "CAMsg.hpp"
34 //#endif
35 //#ifdef HAVE_EPOLL
36 //#else
38  {
39  #ifndef HAVE_POLL
40  FD_ZERO(&m_fdset);
41  FD_ZERO(&m_signaled_set);
42  #ifndef _WIN32
43  m_max=0;
44  #endif
45  #else
46  m_pollfd=new struct pollfd[MAX_POLLFD];
47  memset((void*)m_pollfd,0,sizeof(struct pollfd)*MAX_POLLFD);
48  m_max=0;
49  #endif
50  setPoolForWrite(bWrite);
51  }
52 
54  {
55  #ifndef HAVE_POLL
56  if(!bWrite)
57  {
59  m_set_write=NULL;
60 
61  }
62  else
63  {
64  m_set_read=NULL;
66  }
67  #else
68  for(int i=0;i<MAX_POLLFD;i++)
69  {
70  if(bWrite)
71  m_pollfd[i].events=POLLOUT;
72  else
73  m_pollfd[i].events=POLLIN;
74  m_pollfd[i].fd=-1;
75  }
76  #endif
77  return E_SUCCESS;
78  }
79 
81  {
82  m_csFD_SET.lock();
83  #ifndef HAVE_POLL
84  #pragma warning( push )
85  #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
86  FD_CLR(s.getSocket(),&m_fdset);
87  #pragma warning (pop)
88  #else
89  SINT sock=s.getSocket();
90  m_pollfd[sock].fd=-1;
91  //CAMsg::printMsg(LOG_DEBUG,"CASocketGroup::remove() - socket: %d\n",sock);
92  #endif
94  return E_SUCCESS;
95  }
96 
98  {
99  m_csFD_SET.lock();
100  #ifndef HAVE_POLL
101  #pragma warning( push )
102  #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
103  FD_CLR(s.getSocket(),&m_fdset);
104  #pragma warning (pop)
105  #else
106  SINT sock=s.getSocket();
107  m_pollfd[sock].fd=-1;
108  //CAMsg::printMsg(LOG_DEBUG,"CASocketGroup::remove() - socket: %d\n",sock);
109  #endif
110  m_csFD_SET.unlock();
111  return E_SUCCESS;
112  }
113 
115  {
116  #ifndef HAVE_POLL
117  m_csFD_SET.lock();
118  memcpy(&m_signaled_set,&m_fdset,sizeof(fd_set));
119  m_csFD_SET.unlock();
120  #ifdef _DEBUG
121  #ifdef _WIN32
122  int ret=::select(0,m_set_read,m_set_write,NULL,NULL);
123  #else
124  int ret=::select(m_max,m_set_read,m_set_write,NULL,NULL);
125  #endif
126  if(ret==SOCKET_ERROR)
127  {
128  CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",GET_NET_ERROR);
129  }
130  return ret;
131  #else
132  #ifdef _WIN32
133  return ::select(0,m_set_read,m_set_write,NULL,NULL);
134  #else
135  return ::select(m_max,m_set_read,m_set_write,NULL,NULL);
136  #endif
137  #endif
138  #else
139  m_csFD_SET.lock();
140  SINT32 ret=::poll((struct pollfd*)m_pollfd,m_max,-1);
141  m_csFD_SET.unlock();
142  return ret;
143  #endif
144  }
145 
154  {
155  SINT32 ret;
156  #ifndef HAVE_POLL
157  m_csFD_SET.lock();
158  memcpy(&m_signaled_set,&m_fdset,sizeof(fd_set));
159  m_csFD_SET.unlock();
160  timeval ti;
161  ti.tv_sec=0;
162  ti.tv_usec=time_ms*1000;
163 
164  #ifdef _WIN32
165  if(m_signaled_set.fd_count==0)
166  {
167  Sleep(time_ms);
168  ret=E_TIMEDOUT;
169  }
170  else
171  ret=::select(0,m_set_read,m_set_write,NULL,&ti);
172  #else
173  ret=::select(m_max,m_set_read,m_set_write,NULL,&ti);
174  #endif
175  #else
176  m_csFD_SET.lock();
177  ret=::poll((struct pollfd*)m_pollfd,m_max,time_ms);
178  m_csFD_SET.unlock();
179  #endif
180  if(ret==0)
181  {
182  return E_TIMEDOUT;
183  }
184  if(ret==SOCKET_ERROR)
185  {
186  ret=GET_NET_ERROR;
187  #ifdef _DEBUG
188  CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",ret);
189  #endif
190  if(ret==EINTR)
191  return E_TIMEDOUT;
192  return E_UNKNOWN;
193  }
194  return ret;
195  }
196 //#endif
#define GET_NET_ERROR
Definition: StdAfx.h:469
#define SOCKET_ERROR
Definition: StdAfx.h:464
#define MAX_POLLFD
Definition: StdAfx.h:192
signed int SINT32
Definition: basetypedefs.h:132
signed int SINT
Definition: basetypedefs.h:156
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
fd_set * m_set_write
SINT32 setPoolForWrite(bool bWrite)
SINT32 remove(CASocket &s)
fd_set * m_set_read
CASocketGroup(bool bWrite)
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