Mixe for Privacy and Anonymity in the Internet
CASingleSocketGroup.hpp
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 #ifndef __CASINGLESOCKETGROUP__
00029 #define __CASINGLESOCKETGROUP__
00030 #ifdef _DEBUG
00031   #include "CAMsg.hpp"
00032 #endif
00033 
00034 #if !defined(HAVE_POLL)&&!defined(HAVE_EPOLL)
00035   #include "CASocketGroup.hpp"
00036 class CASingleSocketGroup:public CASocketGroup
00037   {
00038     public:
00039       CASingleSocketGroup(bool bWrite):CASocketGroup(bWrite)
00040       {
00041       }
00042 
00043       static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
00044         {
00045           fd_set fdset;
00046           FD_ZERO(&fdset);
00047           #pragma warning( push )
00048           #pragma warning( disable : 4127 ) //Disable: Bedingter Ausdruck ist konstant
00049           FD_SET(s.getSocket(),&fdset);
00050           #pragma warning( pop )
00051 
00052           SINT32 ret;
00053           timeval ti;
00054           ti.tv_sec=0;
00055           ti.tv_usec=time_ms*1000;
00056           if(bWrite)
00057             ret=::select(1,NULL,&fdset,NULL,&ti);
00058           else
00059             ret=::select(1,&fdset,NULL,NULL,&ti);
00060           if(ret>0)
00061             return ret;
00062           else if(ret==0)
00063           {
00064             SET_NET_ERROR(E_TIMEDOUT);
00065             return E_TIMEDOUT;
00066           }
00067           return E_UNKNOWN;
00068         }
00069   };
00070 
00071 #else
00072 #include "CAMuxSocket.hpp"
00073 #ifdef HAVE_POLL
00074 class CASingleSocketGroup
00075   {
00076     public:
00077       CASingleSocketGroup(bool bWrite)
00078         {
00079           m_pollfd=new struct pollfd;
00080           setPoolForWrite(bWrite);
00081         }
00082       ~CASingleSocketGroup()
00083       {
00084         delete m_pollfd;
00085         m_pollfd = NULL;
00086       }
00087       
00088       SINT32 add(CASocket&s)
00089         {
00090           m_pollfd->fd=s.getSocket();
00091           return E_SUCCESS;
00092         }
00093 
00094       SINT32 add(CAMuxSocket&s)
00095         {
00096           m_pollfd->fd=s.getSocket();
00097           return E_SUCCESS;
00098         }
00099 
00100       SINT32 setPoolForWrite(bool bWrite)
00101         {
00102           if(bWrite)
00103             m_pollfd->events=POLLOUT;
00104           else
00105             m_pollfd->events=POLLIN;
00106           return E_SUCCESS;
00107         }
00108 
00109       SINT32 select()
00110         {
00111           return ::poll(m_pollfd,1,-1);
00112         }
00119       SINT32 select(UINT32 time_ms)
00120         {
00121           SINT32 ret=::poll(m_pollfd,1,time_ms);
00122           if(ret>=1)
00123             return 1;
00124           else if(ret==0)
00125           {
00126             SET_NET_ERROR(E_TIMEDOUT);
00127             return E_TIMEDOUT;
00128           }
00129           #ifdef _DEBUG
00130             ret=GET_NET_ERROR;
00131             CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",ret);
00132           #endif
00133           return E_UNKNOWN;
00134         }
00135 
00136         static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
00137           {
00138             struct pollfd pollfd;
00139             if(bWrite)
00140               pollfd.events=POLLOUT;
00141             else
00142               pollfd.events=POLLIN;
00143             pollfd.fd=s.getSocket();
00144             SINT32 ret=::poll(&pollfd,1,time_ms);
00145             if(ret>=1)
00146               return 1;
00147             else if(ret==0)
00148             {
00149               SET_NET_ERROR(E_TIMEDOUT);
00150               return E_TIMEDOUT;
00151             }
00152             return E_UNKNOWN;             
00153           }
00154 
00155     private:
00156       struct pollfd* m_pollfd;
00157   };
00158 #elif defined(HAVE_EPOLL)
00159 class CASingleSocketGroup
00160   {
00161     public:
00162       CASingleSocketGroup(bool bWrite)
00163         {
00164           m_hEPFD=epoll_create(1);
00165           setPoolForWrite(bWrite);
00166         }
00167 
00168       ~CASingleSocketGroup()
00169         {
00170           close(hEPFD);
00171         }
00172       
00173       SINT32 add(CAMuxSocket&s)
00174         {
00175           if(epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,s.getSocket(),&m_pollAdd)!=0)
00176             return E_UNKNOWN;
00177           return E_SUCCESS;
00178         }
00179 
00180       SINT32 add(CAMuxSocket&s)
00181         {
00182           if(epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,s.getSocket(),&m_pollAdd)!=0)
00183             return E_UNKNOWN;
00184           return E_SUCCESS;
00185         }
00186 
00187       SINT32 setPoolForWrite(bool bWrite)
00188         {
00189           if(bWrite)
00190             m_pollAdd->events=POLLOUT|POLLERR|POLLHUP;
00191           else
00192             m_pollfd->events=POLLIN|POLLERR|POLLHUP;
00193           return E_SUCCESS;
00194         }
00195 
00196       SINT32 select()
00197         {
00198           return ::epoll_wait(m_hEPFD,&m_Events,1,-1);
00199         }
00200       
00201       SINT32 select(UINT32 time_ms)
00202         {
00203           SINT32 ret=::epoll_wait(m_hEPFD,&m_Events,1,time_ms);
00204           if(ret>=1)
00205             return 1;
00206           else if(ret==0)
00207           {
00208             SET_NET_ERROR(E_TIMEDOUT);
00209             return E_TIMEDOUT;
00210           }
00211           #ifdef _DEBUG
00212             ret=GET_NET_ERROR;
00213             CAMsg::printMsg(LOG_DEBUG,"SocketGroup Select-Fehler: %i\n",ret);
00214           #endif
00215           return E_UNKNOWN;
00216         }
00217 
00218       static SINT32 select_once(CASocket& s,bool bWrite,UINT32 time_ms)
00219         {
00220           struct epool_events events;
00221           if(bWrite)
00222             events.events=POLLOUT|POLLERR|POLLHUP;
00223           else
00224             events.events=POLLIN|POLLERR|POLLHUP;
00225           epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,s.getSocket(),&events);
00226           SINT32 ret=::epoll_wait(m_hEPFD,&events,1,time_ms);
00227           if(ret>=1)
00228             return 1;
00229           else if(ret==0)
00230           {
00231             SET_NET_ERROR(E_TIMEDOUT);
00232             return E_TIMEDOUT;
00233           }
00234           return E_UNKNOWN;
00235         }
00236 
00237     private:
00238       struct epool_events m_epollAdd;
00239       struct epool_events m_Events;
00240       SINT32 m_hEPFD;
00241   };
00242 #endif
00243 #endif
00244 #endif