Esempio n. 1
0
std::shared_ptr<MyBuffer> ProtobufCodec::Encode(const CMessage& message)
{
	std::shared_ptr<MyBuffer> ptrBuffer(new MyBuffer());
	assert(0 == ptrBuffer->readableBytes());
	const std::string& typeName = message.GetTypeName();

	int32_t nameLen = static_cast<int32_t>(typeName.size() + 1);//'\0'

	ptrBuffer->appendUint32(nameLen);
	ptrBuffer->append(typeName.c_str(), nameLen);
	//消息内容
	size_t byte_size = message.ByteSize();
	//ensure memory enough
	ptrBuffer->ensureWritableBytes(byte_size);
	//write data
	message.SerializeWithCachedSizesToArray(reinterpret_cast<uint8_t*>(ptrBuffer->beginWrite()));
	ptrBuffer->commit(byte_size);//move write index

	//检验和
	uint32_t checkSum = adler32(1, reinterpret_cast<const Bytef*>(ptrBuffer->peek()), ptrBuffer->readableBytes());

	ptrBuffer->appendUint32(checkSum);
	assert(ptrBuffer->readableBytes() == sizeof(nameLen)+nameLen + byte_size + sizeof(checkSum));
	//消息长度
	int32_t len = htonl(static_cast<int32_t>(ptrBuffer->readableBytes()));
	ptrBuffer->prepend(&len, sizeof(len));
	//delimiter
	ptrBuffer->append("\r\n", 2);
	return ptrBuffer;
}
Esempio n. 2
0
void DB2Mon::getSnapshot(JNIEnv* env, sqlma *ma_ptr, Snapshot& snap)
{
	int		rc = 0;  // return code
	sqlca	sqlca;
	sqluint32 outputFormat;

	// Determine the size of the snapshot
	db2GetSnapshotSizeData getSnapshotSizeParam;
	sqluint32 buffer_sz;  // estimated buffer size

	getSnapshotSizeParam.piSqlmaData = ma_ptr;
	getSnapshotSizeParam.poBufferSize = &buffer_sz;
	getSnapshotSizeParam.iVersion = SQLM_CURRENT_VERSION;
	getSnapshotSizeParam.iNodeNumber = m_aggAllNodes ? SQLM_ALL_NODES : SQLM_CURRENT_NODE;
	getSnapshotSizeParam.iSnapshotClass = SQLM_CLASS_DEFAULT;

	rc = db2GetSnapshotSize(db2Version810, &getSnapshotSizeParam, &sqlca);
	APICHECK(env, sqlca);

	// Allocate memory to a buffer to hold snapshot monitor data.
	// Add about 10k to make sure we avoid the reallocation below
	buffer_sz = buffer_sz + SNAPSHOT_BUFFER_UNIT_SZ;
	MemObj	ptrBuffer(buffer_sz);
	char * buffer_ptr = ptrBuffer.get();
	sqlm_collected collected;  // returned sqlm_collected structure
	memset(&collected, '\0', sizeof(struct sqlm_collected));

	db2GetSnapshotData getSnapshotParam;
	getSnapshotParam.piSqlmaData = ma_ptr;
	getSnapshotParam.poCollectedData = &collected;
	getSnapshotParam.iBufferSize = buffer_sz;
	getSnapshotParam.poBuffer = buffer_ptr;
	getSnapshotParam.iVersion = SQLM_CURRENT_VERSION;
	getSnapshotParam.iStoreResult = 0;
	getSnapshotParam.iNodeNumber = m_aggAllNodes ? SQLM_ALL_NODES : SQLM_CURRENT_NODE;
	getSnapshotParam.poOutputFormat = &outputFormat;
	getSnapshotParam.iSnapshotClass = SQLM_CLASS_DEFAULT;

	rc = db2GetSnapshot(db2Version810, &getSnapshotParam, &sqlca);
	APICHECK(env, sqlca);

	// If buffer was too small enlarge it and repeat until succeeded
	while (sqlca.sqlcode == 1606)
	{
		buffer_sz = buffer_sz + SNAPSHOT_BUFFER_UNIT_SZ;
		ptrBuffer.resize(buffer_sz);
		buffer_ptr = ptrBuffer.get();

		getSnapshotParam.iBufferSize = buffer_sz;
		getSnapshotParam.poBuffer = buffer_ptr;

		rc = db2GetSnapshot(db2Version810, &getSnapshotParam, &sqlca);
		APICHECK(env, sqlca);
	}

	// Fill the snapshot object from the memory buffer
	snap.initialize(env, buffer_ptr);
}