C++ #ND2 challenge wrong MessageID and wrong index begin

I’m stuck on Stage #ND2 C++

I have tested everything but I keep getting wrong hashes, and my first PIECE ID is indeed 7 but the rest is -32 and the returned index, begin are gibberish from the first response
Anyone can help?

Here are my logs:

Here’s my code

void TorrentClient::downloadPiece(const std::string &path, size_t pieceIndex) const
{
	std::ofstream out(path, std::ios_base::out);

	if (!out) {
		std::stringstream error;
		error << "Error creating file: " << path << std::endl;
		exit_with_message(error);
	}

	auto message = m_client->recv();
	if (!message.isType(PeerMessagesType::BITFIELD))
		exit_with_message(make_message(message));

	m_client->send(PeerMessage(PeerMessagesType::INTERESTED));
	
	message = m_client->recv();
	if (!message.isType(PeerMessagesType::UNCHOKE))
		exit_with_message(make_message(message));

	size_t size = m_torrent.piece_length;
	size_t blockIndex = 0;

	while (size > 0) {
		size_t blockSize = size > BLOCK_SIZE ? BLOCK_SIZE : size;
		std::stringstream ss;
		ss << bitos(pieceIndex)
		   << bitos(BLOCK_SIZE * (blockIndex++))
		   << bitos(blockSize);
		m_client->send(PeerMessage(PeerMessagesType::REQUEST, ss.str()));
		
		message = m_client->recv();
		// if (!message.isType(PeerMessagesType::PIECE))
		// 	exit_with_message(make_message(message));

		auto payload = std::string_view(message.getPayload());

		[[maybe_unused]]
		auto index = bstoi(std::string(payload.substr(0, sizeof(int))));
		payload.remove_prefix(sizeof(int));

		[[maybe_unused]]
		auto begin = bstoi(std::string(payload.substr(0, sizeof(int))));
		payload.remove_prefix(sizeof(int));

		auto block = payload;

		out << block;
		size -= block.size();
	}
}

NOTE: the receive and send method is tested out effectively where they ensure sending and receiving the whole message regardless of the size. also the maximum buffer size is 32 KB which is twice as big as block size

Could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

I may need to get back to you in a few days.

The code is already published on github, I just have included my last version, I will be more than happy to hear some comments about the code from you, Thanks

1 Like

@ycsvenom I added several logs to your code and here’s what I found:

  1. Your code was sending and receiving messages accurately.
    a. Messages sizes were correct, with 16392 = 1 + 4 + 4 + 16k bytes.
    b. Messages received in socket.cpp were consistent, with 0x4000 increments (16k in decimal).

  2. However, some messages became garbled when transferred from socket.cpp to peer_socket_wrapper. This may be worth investigating to see why the data isn’t intact at that stage.

@andy1li it’s weird indeed, I have tested socket wrapper in the tests directory, it sends and receives the exact replica of the content, I will investigate further on this,
If you read the code, do you have any suggestions or notes?

Thanks for help

@ycsvenom Feel free to tag me again if you’re still stuck after a few days. Honestly, my C++ skills are a bit rusty, but I’ll do my best to help!