Beispiel #1
0
ByteArrayPtr Datagram::encode () const {
	// simple encoding headerLength, contentLength, header, content
	// both with 4 bytes
	size_t headerLength  = mHeader ? mHeader->size() : 0;
	if (headerLength > 2147483647) {
		Log (LogError) << LOGID << "Header to long!" << std::endl;
		assert (false);
		return ByteArrayPtr();
	}
	size_t contentLength = mContent ? mContent->size() : 0;
	if (contentLength > 2147483647) {
		Log (LogError) << LOGID << "Content to long" << std::endl;
		assert (false);
		return ByteArrayPtr();
	}
	uint32_t hln = htonl ((uint32_t)headerLength);
	uint32_t cln = htonl ((uint32_t)contentLength);

	ByteArrayPtr dest = createByteArrayPtr ();
	dest->reserve(8 + headerLength + contentLength);
	dest->append((const char*) &hln, 4);
	dest->append((const char*) &cln, 4);
	if (mHeader)
		dest->append(*mHeader);
	if (mContent)
		dest->append(*mContent);
	assert (dest->size() == 8 + headerLength + contentLength);
	return dest;
}
Beispiel #2
0
sf::ByteArrayPtr BoshTransport::read (long maxSize) {
	ByteArrayPtr result = sf::createByteArrayPtr();
	if (maxSize < 0 || maxSize >= (long) mInputBuffer.size()) {
		result->swap (mInputBuffer);
	} else {
		result->append (mInputBuffer.const_c_array(), maxSize);
		mInputBuffer.l_truncate(maxSize);
	}
	return result;
}