00001
00002
00003
00004
00005 #ifndef EQNET_DATAOSTREAM_H
00006 #define EQNET_DATAOSTREAM_H
00007
00008 #include <eq/base/buffer.h>
00009 #include <eq/net/types.h>
00010
00011 #include <iostream>
00012 #include <vector>
00013
00014 namespace eq
00015 {
00016 namespace net
00017 {
00018 class Connection;
00019
00025 class EQ_EXPORT DataOStream
00026 {
00027 public:
00028 DataOStream();
00029 virtual ~DataOStream();
00030
00032 void enable( const NodeVector& receivers );
00033 void enable( const ConnectionVector& receivers );
00034 void enable( const NodePtr node );
00035 void enable();
00036
00038 void resend( const NodePtr node );
00039
00041 void disable();
00042
00044 void flush();
00045
00047 void enableBuffering();
00049 void disableBuffering();
00050
00052 void enableSave();
00054 void disableSave();
00055
00057
00059 bool hasSentData() const { return _dataSent; }
00060
00062 const base::Bufferb& getSaveBuffer() const
00063 { EQASSERT( _save ); return _buffer; }
00064
00065
00067
00068 template< typename T >
00069 DataOStream& operator << ( const T& value )
00070 { write( &value, sizeof( value )); return *this; }
00071
00072 template< typename T >
00073 DataOStream& operator << ( const std::vector< T >& value )
00074 {
00075 const uint64_t nElems = value.size();
00076 write( &nElems, sizeof( nElems ));
00077 if( nElems > 0 )
00078 write( &value[0], nElems * sizeof( T ) );
00079 return *this;
00080 }
00081
00082 void write( const void* data, uint64_t size );
00083 void writeOnce( const void* data, uint64_t size );
00084
00085
00086
00087 protected:
00089 virtual void sendHeader( const void* buffer, const uint64_t size ) = 0;
00091 virtual void sendBuffer( const void* buffer, const uint64_t size ) = 0;
00093 virtual void sendFooter( const void* buffer, const uint64_t size ) = 0;
00095 virtual void sendSingle( const void* buffer, const uint64_t size )
00096 { sendHeader( buffer, size ); sendFooter( 0, 0 ); }
00097
00099 ConnectionVector _connections;
00100
00101 private:
00103 base::Bufferb _buffer;
00105 uint64_t _bufferStart;
00107 static uint64_t _highWaterMark;
00108
00110 bool _enabled;
00112 bool _dataSent;
00114 bool _buffered;
00116 bool _save;
00117
00119 void _sendBuffer( const void* data, const uint64_t size );
00120
00122 void _resetStart();
00123
00125 void _unlockConnections();
00126 };
00127
00128 }
00129 }
00130
00131 #include <eq/net/nodeID.h>
00132 namespace eq
00133 {
00134 namespace net
00135 {
00136
00137 template<>
00138 inline DataOStream& DataOStream::operator << ( const std::string& str )
00139 {
00140 const uint64_t nElems = str.length();
00141 write( &nElems, sizeof( nElems ));
00142 if ( nElems > 0 )
00143 write( str.c_str(), nElems );
00144
00145 return *this;
00146 }
00147 template<>
00148 inline DataOStream& DataOStream::operator << ( const NodeID& nodeID )
00149 {
00150 NodeID out( nodeID );
00151 out.convertToNetwork();
00152 write( &out, sizeof( out ));
00153 return *this;
00154 }
00155 }
00156 }
00157 #endif //EQNET_DATAOSTREAM_H