dataIStream.cpp

00001 
00002 /* Copyright (c) 2007, 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 #include "dataIStream.h"
00019 
00020 #include "log.h"
00021 #include <eq/base/debug.h>
00022 
00023 #include <string.h>
00024 
00025 using namespace std;
00026 
00027 namespace eq
00028 {
00029 namespace net
00030 {
00031 DataIStream::DataIStream()
00032         : _input( 0 )
00033         , _inputSize( 0 )
00034         , _position( 0 )
00035 {}
00036 
00037 DataIStream::~DataIStream()
00038 {
00039     reset();
00040 }
00041 
00042 void DataIStream::reset()
00043 {
00044     _input     = 0;
00045     _inputSize = 0;
00046     _position  = 0;
00047 }
00048 
00049 void DataIStream::read( void* data, uint64_t size )
00050 {
00051     if( !_checkBuffer( ))
00052     {
00053         EQUNREACHABLE;
00054         EQERROR << "No more input data" << endl;
00055         return;
00056     }
00057 
00058     EQASSERT( _input );
00059     
00060     if( _position + size > _inputSize )
00061     {
00062         EQUNREACHABLE;
00063         EQERROR << "Not enough data in input buffer" << endl;
00064         // TODO: Allow reads which are asymmetric to writes by reading from
00065         // multiple blocks here?
00066         return;
00067     }
00068 
00069     memcpy( data, _input + _position, size );
00070     _position += size;
00071 }
00072 
00073 const void* DataIStream::getRemainingBuffer()
00074 {
00075     if( !_checkBuffer( ))
00076         return 0;
00077 
00078     return _input + _position;
00079 }
00080 
00081 uint64_t DataIStream::getRemainingBufferSize()
00082 {
00083     if( !_checkBuffer( ))
00084         return 0;
00085 
00086     return _inputSize - _position;
00087 }
00088 
00089 void DataIStream::advanceBuffer( const uint64_t offset )
00090 {
00091     EQASSERT( _position + offset <= _inputSize );
00092     _position += offset;
00093 }
00094 
00095 bool DataIStream::_checkBuffer()
00096 {
00097     if( _position < _inputSize )
00098         return true;
00099 
00100     if( !getNextBuffer( &_input, &_inputSize ))
00101     {
00102         return false;
00103     }
00104     _position = 0;
00105     return true;
00106 }
00107 
00108 }
00109 }
00110 
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8