Equalizer 1.0
|
00001 00002 /* Copyright (c) 2006-2010, Stefan Eilemann <eile@equalizergraphics.com> 00003 All rights reserved. */ 00004 00005 #define EQ_ASSEMBLE_THRESHOLD (4096) 00006 00007 template< typename T > 00008 bool Connection::send( Packet &packet, const std::vector<T>& data ) 00009 { 00010 if( data.size() == 0 ) 00011 return send( packet ); 00012 00013 size_t packetStorage = EQ_MAX( 8, sizeof( T )); 00014 const size_t offset = packetStorage % 8; 00015 if( offset ) 00016 packetStorage += 8 - offset; 00017 const size_t nItems = data.size(); 00018 const size_t dataSize = nItems * sizeof( T ); 00019 00020 if( dataSize <= packetStorage ) // fits in existing packet 00021 { 00022 memcpy( (char*)(&packet) + packet.size-packetStorage, &data[0], 00023 dataSize ); 00024 return send( packet ); 00025 } 00026 00027 const uint64_t headerSize = packet.size - packetStorage; 00028 const uint64_t size = headerSize + dataSize; 00029 if( size > EQ_ASSEMBLE_THRESHOLD ) 00030 { 00031 // OPT: lock the connection and use two send() to avoid big memcpy 00032 packet.size = size; 00033 00034 lockSend(); 00035 const bool ret = ( send( &packet, headerSize, true ) && 00036 send( &data[0], dataSize, true )); 00037 unlockSend(); 00038 return ret; 00039 } 00040 // else 00041 00042 char* buffer = static_cast<char*>( alloca( size )); 00043 00044 memcpy( buffer, &packet, headerSize ); 00045 memcpy( buffer + headerSize, &data[0], dataSize ); 00046 00047 ((Packet*)buffer)->size = size; 00048 return send( buffer, size ); 00049 }