Mixe for Privacy and Anonymity in the Internet
CASocketGroupEpoll.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 __CASOCKETGOROPEPOLL__
00029 #define __CASOCKETGOROPEPOLL__
00030 #include "doxygen.h"
00031 #ifdef HAVE_EPOLL
00032 #include "CASocket.hpp"
00033 #include "CAMuxSocket.hpp"
00034 #include "CAMutex.hpp"
00035 #ifdef DEBUG
00036   #include "CAMsg.hpp"
00037 #endif
00038 class CASocketGroupEpoll
00039   {
00040     public:
00041       CASocketGroupEpoll(bool bWrite);
00042       ~CASocketGroupEpoll();
00043       SINT32 setPoolForWrite(bool bWrite);
00044       
00047       SINT32 add(CASocket&s,void * datapointer)
00048         {
00049           SINT32 ret=E_SUCCESS;
00050           m_csFD_SET.lock();
00051           SOCKET socket=s.getSocket();
00052           m_pEpollEvent->data.ptr=datapointer;
00053           
00054           //if(epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,socket,m_pEpollEvent)!=0)
00055           //  ret=E_UNKNOWN;
00056           ret=epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,socket,m_pEpollEvent);
00057           m_csFD_SET.unlock();
00058           return ret;
00059         }
00060 
00063       SINT32 add(CAMuxSocket&s,void * datapointer)
00064         {
00065           SINT32 ret=E_SUCCESS;
00066           m_csFD_SET.lock();
00067           SOCKET socket=s.getSocket();
00068           m_pEpollEvent->data.ptr=datapointer;
00069           //if(epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,socket,m_pEpollEvent)!=0)
00070           //  ret=E_UNKNOWN;
00071           ret=epoll_ctl(m_hEPFD,EPOLL_CTL_ADD,socket,m_pEpollEvent);
00072           m_csFD_SET.unlock();
00073           return ret;
00074         }
00075 
00076       SINT32 remove(CASocket&s)
00077         {
00078           SINT32 ret=E_SUCCESS;
00079           m_csFD_SET.lock();
00080           if(epoll_ctl(m_hEPFD,EPOLL_CTL_DEL,s.getSocket(),m_pEpollEvent)!=0)
00081             ret=E_UNKNOWN;
00082           ASSERT(ret==E_SUCCESS,"Error in Epoll socket group remove")
00083           m_csFD_SET.unlock();
00084           return ret;
00085         }
00086 
00087       SINT32 remove(CAMuxSocket&s)
00088         {
00089           SINT32 ret=E_SUCCESS;
00090           m_csFD_SET.lock();
00091           if(epoll_ctl(m_hEPFD,EPOLL_CTL_DEL,s.getSocket(),m_pEpollEvent)!=0)
00092             ret=E_UNKNOWN;
00093           ASSERT(ret==E_SUCCESS,"Error in Epoll socket group remove")
00094           m_csFD_SET.unlock();
00095           return ret;
00096         }
00097 
00098       SINT32 select()
00099         {
00100           m_csFD_SET.lock();
00101           m_iNumOfReadyFD=epoll_wait(m_hEPFD,m_pEvents,MAX_POLLFD,-1);
00102           if(m_iNumOfReadyFD>0)
00103             {
00104               m_csFD_SET.unlock();
00105               return m_iNumOfReadyFD;
00106             }
00107           m_csFD_SET.unlock();
00108           return E_UNKNOWN;
00109         }
00110       
00118       SINT32 select(UINT32 time_ms)
00119         {
00120           m_csFD_SET.lock();
00121           m_iNumOfReadyFD=epoll_wait(m_hEPFD,m_pEvents,MAX_POLLFD,time_ms);
00122           if(m_iNumOfReadyFD>0)
00123             {
00124               m_csFD_SET.unlock();
00125               return m_iNumOfReadyFD;
00126             }
00127           else if(m_iNumOfReadyFD==0)
00128             {
00129               m_csFD_SET.unlock();
00130               return E_TIMEDOUT;
00131             }
00132           m_csFD_SET.unlock();
00133           return E_UNKNOWN;
00134         }
00135 
00139     /*  bool isSignaled(CASocket&s)
00140         {
00141         SINT32 socket=s.getSocket();
00142           for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
00143             {
00144               if(socket==m_pEvents->data.fd)
00145                 return true;
00146             }
00147           return false;
00148         }
00149 
00150       bool isSignaled(CASocket*ps)
00151         {
00152         SINT32 socket=ps->getSocket();
00153           for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
00154             {
00155               if(socket==m_pEvents->data.fd)
00156                 return true;
00157             }
00158           return false;
00159         }
00160 
00161       bool isSignaled(CAMuxSocket&s)
00162         {
00163           SINT32 socket=s.getSocket();
00164           for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
00165             {
00166               if(socket==m_pEvents->data.fd)
00167                 return true;
00168             }
00169           return false;
00170         }
00171 */    
00172 
00173       bool isSignaled(void* datapointer)
00174         {
00175           for(SINT32 i=0;i<m_iNumOfReadyFD;i++)
00176             {
00177               if(datapointer==m_pEvents->data.ptr)
00178                 return true;
00179             }
00180           return false;
00181         }
00182 
00183       void * getFirstSignaledSocketData()
00184         {
00185           m_iAktSignaledSocket=0;
00186           if(m_iNumOfReadyFD>0)
00187             return m_pEvents[0].data.ptr;
00188           return NULL;
00189         }
00190 
00191       void * getNextSignaledSocketData()
00192         {
00193           m_iAktSignaledSocket++;
00194           if(m_iNumOfReadyFD>m_iAktSignaledSocket)
00195             return m_pEvents[m_iAktSignaledSocket].data.ptr;
00196           return NULL;
00197         }
00198 
00199     private:
00200       SINT32 m_hEPFD; //the EPoll file descriptor
00201       struct epoll_event* m_pEvents;
00202       struct epoll_event* m_pEpollEvent;
00203       SINT32 m_iNumOfReadyFD;
00204       SINT32 m_iAktSignaledSocket;
00205       CAMutex m_csFD_SET;
00206   };
00207 #endif
00208 #endif