bool Connection::send( const Connections& connections, Packet& packet, const void* data, const uint64_t dataSize, const bool isLocked ) { if( connections.empty( )) return true; if( dataSize <= 8 ) // fits in existing packet { if( dataSize != 0 ) memcpy( (char*)(&packet) + packet.size-8, data, dataSize ); return send( connections, packet, isLocked ); } const uint64_t headerSize = packet.size - 8; const uint64_t size = headerSize + dataSize; if( size > EQ_ASSEMBLE_THRESHOLD ) { // OPT: lock the connection and use two send() to avoid big memcpy packet.size = size; bool success = true; for( Connections::const_iterator i= connections.begin(); i<connections.end(); ++i ) { ConnectionPtr connection = *i; if( !isLocked ) connection->lockSend(); if( !connection->send( &packet, headerSize, true ) || !connection->send( data, dataSize, true )) { success = false; } if( !isLocked ) connection->unlockSend(); } return success; } char* buffer = (char*)alloca( size ); memcpy( buffer, &packet, packet.size-8 ); memcpy( buffer + packet.size-8, data, dataSize ); ((Packet*)buffer)->size = size; bool success = true; for( Connections::const_iterator i = connections.begin(); i < connections.end(); ++i ) { ConnectionPtr connection = *i; if( !connection->send( buffer, size, isLocked )) success = false; } return success; }
void OCommand::sendHeader( const uint64_t additionalSize ) { LBASSERT( !_impl->dispatcher ); LBASSERT( !_impl->isLocked ); LBASSERT( additionalSize > 0 ); const Connections& connections = getConnections(); for( ConnectionsCIter i = connections.begin(); i != connections.end(); ++i ) { ConnectionPtr connection = *i; connection->lockSend(); } _impl->isLocked = true; _impl->size = additionalSize; flush( true ); }
bool Connection::send( const Connections& connections, Packet& packet, const void* const* items, const uint64_t* sizes, const size_t nItems ) { if( connections.empty( )) return true; packet.size -= 8; const uint64_t headerSize = packet.size; for( size_t i = 0; i < nItems; ++i ) { EQASSERT( sizes[i] > 0 ); packet.size += sizes[ i ] + sizeof( uint64_t ); } bool success = true; for( Connections::const_iterator i = connections.begin(); i < connections.end(); ++i ) { ConnectionPtr connection = *i; connection->lockSend(); if( !connection->send( &packet, headerSize, true )) success = false; for( size_t j = 0; j < nItems; ++j ) if( !connection->send( &sizes[j], sizeof(uint64_t), true ) || !connection->send( items[j], sizes[j], true )) { success = false; } connection->unlockSend(); } return success; }