fdConnection.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00043
00044 void FDConnection::readNB( void* buffer, const uint64_t bytes ) { }
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 )
00054 {
00055 EQINFO << "Got EOF, closing connection" << std::endl;
00056 close();
00057 return -1;
00058 }
00059
00060 if( bytesRead == -1 )
00061 {
00062 if( errno == EINTR )
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
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 )
00084 {
00085 if( errno == EINTR )
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 }