fdConnection.cpp

00001 
00002 /* Copyright (c) 2005-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifdef WIN32
00019 # error not used on Win32 builds
00020 #endif
00021 
00022 #include "fdConnection.h"
00023 #include "log.h"
00024 
00025 #include <eq/base/base.h>
00026 
00027 #include <errno.h>
00028 #include <poll.h>
00029 
00030 using namespace eq::base;
00031 
00032 namespace eq
00033 {
00034 namespace net
00035 {
00036 FDConnection::FDConnection()
00037         : _readFD( 0 ),
00038           _writeFD( 0 )
00039 {}
00040 
00041 //----------------------------------------------------------------------
00042 // read
00043 //----------------------------------------------------------------------
00044 void FDConnection::readNB( void* buffer, const uint64_t bytes ) { /* NOP */ }
00045 
00046 int64_t FDConnection::readSync( void* buffer, const uint64_t bytes )
00047 {
00048     if( _readFD < 1 )
00049         return -1;
00050 
00051     const ssize_t bytesRead = ::read( _readFD, buffer, bytes );
00052 
00053     if( bytesRead == 0 ) // EOF
00054     {
00055         EQINFO << "Got EOF, closing connection" << std::endl;
00056         close();
00057         return -1;
00058     }
00059 
00060     if( bytesRead == -1 ) // error
00061     {
00062         if( errno == EINTR ) // if interrupted, try again
00063             return 0;
00064 
00065         EQWARN << "Error during read: " << strerror( errno ) << ", " 
00066                << bytes << "b on fd " << _readFD << std::endl;
00067         return -1;
00068     }
00069 
00070     return bytesRead;
00071 }
00072 
00073 //----------------------------------------------------------------------
00074 // write
00075 //----------------------------------------------------------------------
00076 int64_t FDConnection::write( const void* buffer, const uint64_t bytes )
00077 {
00078     if( _writeFD < 1 )
00079         return -1;
00080 
00081     const ssize_t bytesWritten = ::write( _writeFD, buffer, bytes );
00082 
00083     if( bytesWritten == -1 ) // error
00084     {
00085         if( errno == EINTR ) // if interrupted, try again
00086             return 0;
00087 
00088         EQWARN << "Error during write: " << strerror( errno ) << std::endl;
00089         return -1;
00090     }
00091 
00092     return bytesWritten;
00093 }
00094 
00095 bool FDConnection::hasData() const
00096 {
00097     pollfd fd;
00098     fd.events  = POLLIN;
00099     fd.fd      = getNotifier();
00100     EQASSERT( fd.fd > 0 );
00101 
00102     const int nReady = poll( &fd, 1, 0 );
00103     return nReady > 0;
00104 }
00105 
00106 }
00107 }
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8