Mixe for Privacy and Anonymity in the Internet
CASocketGroup.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 
00029 #include "StdAfx.h"
00030 #include "CASocket.hpp"
00031 #include "CASocketGroup.hpp"
00032 //#ifdef _DEBUG
00033   #include "CAMsg.hpp"
00034 //#endif
00035 //#ifdef HAVE_EPOLL
00036 //#else
00037 CASocketGroup::CASocketGroup(bool bWrite)
00038   {
00039     #ifndef HAVE_POLL
00040       FD_ZERO(&m_fdset);
00041       FD_ZERO(&m_signaled_set);
00042       #ifndef _WIN32
00043           m_max=0;
00044       #endif
00045     #else
00046       m_pollfd=new struct pollfd[MAX_POLLFD];
00047       memset((void*)m_pollfd,0,sizeof(struct pollfd)*MAX_POLLFD);
00048       m_max=0;
00049     #endif
00050     setPoolForWrite(bWrite);
00051   }
00052 
00053 inline SINT32 CASocketGroup::setPoolForWrite(bool bWrite)
00054   {
00055     #ifndef HAVE_POLL
00056     if(!bWrite)
00057         {
00058           m_set_read=&m_signaled_set;
00059           m_set_write=NULL;
00060             
00061         }
00062       else
00063         {
00064           m_set_read=NULL;
00065           m_set_write=&m_signaled_set;
00066         } 
00067     #else
00068       for(int i=0;i<MAX_POLLFD;i++)
00069         {
00070           if(bWrite)
00071             m_pollfd[i].events=POLLOUT;
00072           else
00073             m_pollfd[i].events=POLLIN;
00074           m_pollfd[i].fd=-1;
00075         }
00076     #endif
00077     return E_SUCCESS;
00078   }
00079 
00080 SINT32 CASocketGroup::remove(CASocket&s)
00081   {
00082     m_csFD_SET.lock();
00083     #ifndef HAVE_POLL
00084       #pragma warning( push )
00085       #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
00086       FD_CLR(s.getSocket(),&m_fdset);
00087       #pragma warning (pop)
00088     #else
00089       SINT sock=s.getSocket();
00090       m_pollfd[sock].fd=-1;     
00091       //CAMsg::printMsg(LOG_DEBUG,"CASocketGroup::remove() - socket: %d\n",sock);
00092     #endif
00093     m_csFD_SET.unlock();
00094     return E_SUCCESS;
00095   }
00096 
00097 SINT32 CASocketGroup::remove(CAMuxSocket&s)
00098   {
00099     m_csFD_SET.lock();
00100     #ifndef HAVE_POLL
00101       #pragma warning( push )
00102       #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
00103       FD_CLR(s.getSocket(),&m_fdset);
00104       #pragma warning (pop)
00105     #else
00106       SINT sock=s.getSocket();
00107       m_pollfd[sock].fd=-1;     
00108       //CAMsg::printMsg(LOG_DEBUG,"CASocketGroup::remove() - socket: %d\n",sock);
00109     #endif
00110     m_csFD_SET.unlock();
00111     return E_SUCCESS;
00112   }
00113 
00114 SINT32 CASocketGroup::select()
00115   {
00116     #ifndef HAVE_POLL
00117       m_csFD_SET.lock();
00118       memcpy(&m_signaled_set,&m_fdset,sizeof(fd_set));
00119       m_csFD_SET.unlock();
00120       #ifdef _DEBUG
00121         #ifdef _WIN32
00122             int ret=::select(0,m_set_read,m_set_write,NULL,NULL);
00123         #else
00124             int ret=::select(m_max,m_set_read,m_set_write,NULL,NULL);
00125         #endif          
00126         if(ret==SOCKET_ERROR)
00127           {
00128             CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",GET_NET_ERROR);
00129           }
00130         return ret;
00131       #else
00132         #ifdef _WIN32
00133             return ::select(0,m_set_read,m_set_write,NULL,NULL);
00134         #else
00135             return ::select(m_max,m_set_read,m_set_write,NULL,NULL);
00136         #endif          
00137       #endif
00138     #else
00139         m_csFD_SET.lock();
00140         SINT32 ret=::poll((struct pollfd*)m_pollfd,m_max,-1);
00141         m_csFD_SET.unlock();
00142         return ret;
00143     #endif
00144   }
00145 
00153 SINT32 CASocketGroup::select(UINT32 time_ms)
00154   {
00155     SINT32 ret;
00156     #ifndef HAVE_POLL
00157       m_csFD_SET.lock();
00158       memcpy(&m_signaled_set,&m_fdset,sizeof(fd_set));
00159       m_csFD_SET.unlock();
00160       timeval ti;
00161       ti.tv_sec=0;
00162       ti.tv_usec=time_ms*1000;
00163   
00164       #ifdef _WIN32
00165           if(m_signaled_set.fd_count==0)
00166             {
00167               Sleep(time_ms);
00168               ret=E_TIMEDOUT;
00169             }
00170           else
00171             ret=::select(0,m_set_read,m_set_write,NULL,&ti);
00172       #else
00173           ret=::select(m_max,m_set_read,m_set_write,NULL,&ti);
00174       #endif
00175     #else
00176       m_csFD_SET.lock();
00177       ret=::poll((struct pollfd*)m_pollfd,m_max,time_ms);
00178       m_csFD_SET.unlock();
00179     #endif
00180     if(ret==0)
00181       {
00182         return E_TIMEDOUT;
00183       }
00184     if(ret==SOCKET_ERROR)
00185       {
00186         ret=GET_NET_ERROR;
00187         #ifdef _DEBUG
00188           CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",ret);
00189         #endif
00190         if(ret==EINTR)
00191           return E_TIMEDOUT;
00192         return E_UNKNOWN;
00193       }
00194     return ret;
00195   }
00196 //#endif