From df2a542377c1f34cc288d35ffc709ba848937b0e Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 9 Jan 2016 15:56:40 +0100 Subject: [PATCH] Fully fix playlist downloading. --- src/Components/Modules/Playlist.cpp | 64 ++++++----------------------- 1 file changed, 12 insertions(+), 52 deletions(-) diff --git a/src/Components/Modules/Playlist.cpp b/src/Components/Modules/Playlist.cpp index 4585ebdb..4be49590 100644 --- a/src/Components/Modules/Playlist.cpp +++ b/src/Components/Modules/Playlist.cpp @@ -45,24 +45,6 @@ namespace Components { Logger::Print("Received playlist request, sending currently stored buffer.\n"); -// // Split playlist data -// unsigned int maxPacketSize = 1000; -// unsigned int maxBytes = Playlist::CurrentPlaylistBuffer.size(); -// -// for (unsigned int i = 0; i < maxBytes; i += maxPacketSize) -// { -// unsigned int sendBytes = min(maxPacketSize, maxBytes - i); -// unsigned int sentBytes = i + sendBytes; -// -// std::string data; -// data.append(reinterpret_cast(&sentBytes), 4); // Sent bytes -// data.append(reinterpret_cast(&maxBytes), 4); // Max bytes -// -// data.append(Playlist::CurrentPlaylistBuffer.data() + i, sendBytes); -// -// Network::SendRaw(address, std::string("playlistresponse\n") + data); -// } - std::string compressedList = Utils::Compression::ZLib::Compress(Playlist::CurrentPlaylistBuffer); unsigned int size = compressedList.size(); unsigned int hash = Utils::OneAtATime(compressedList.data(), compressedList.size()); @@ -89,38 +71,11 @@ namespace Components } else { -// unsigned int sentBytes = *(unsigned int*)(data.data() + 0); -// unsigned int maxBytes = *(unsigned int*)(data.data() + 4); -// -// // Clear current buffer, if we receive a new packet -// if (data.size() - 8 == sentBytes) Playlist::ReceivedPlaylistBuffer.clear(); -// -// // Append received data -// Playlist::ReceivedPlaylistBuffer.append(data.data() + 8, data.size() - 8); -// -// if (Playlist::ReceivedPlaylistBuffer.size() != sentBytes) -// { -// Party::PlaylistError(Utils::VA("Received playlist data, but it seems invalid: %d != %d", sentBytes, Playlist::ReceivedPlaylistBuffer.size())); -// Playlist::ReceivedPlaylistBuffer.clear(); -// return; -// } -// else -// { -// Logger::Print("Received playlist data: %d/%d (%d%%)\n", sentBytes, maxBytes, ((100 * sentBytes) / maxBytes)); -// } -// -// if (Playlist::ReceivedPlaylistBuffer.size() == maxBytes) -// { -// Logger::Print("Received playlist, loading and continuing connection...\n"); -// Game::Live_ParsePlaylists(Playlist::ReceivedPlaylistBuffer.data()); -// Party::PlaylistContinue(); -// -// Playlist::ReceivedPlaylistBuffer.clear(); -// } - + // Read hash and length unsigned int hash = *(unsigned int*)data.data(); unsigned int length = *(unsigned int*)(data.data() + 4); + // Verify length if (length > (data.size() - 8)) { Party::PlaylistError(Utils::VA("Received playlist response, but it is too short.")); @@ -128,10 +83,11 @@ namespace Components return; } - unsigned int hash2 = Utils::OneAtATime(data.data() + 8, length); + // Generate buffer and hash std::string compressedData(data.data() + 8, length); - Playlist::ReceivedPlaylistBuffer = Utils::Compression::ZLib::Decompress(compressedData); + unsigned int hash2 = Utils::OneAtATime(compressedData.data(), compressedData.size()); + //Validate hashes if (hash2 != hash) { Party::PlaylistError(Utils::VA("Received playlist response, but the checksum did not match (%d != %d).", hash, hash2)); @@ -139,9 +95,13 @@ namespace Components return; } - Logger::Print("Received playlist, loading and continuing connection...\n"); - Game::Live_ParsePlaylists(Playlist::ReceivedPlaylistBuffer.data()); - Party::PlaylistContinue(); + // Decompress buffer + Playlist::ReceivedPlaylistBuffer = Utils::Compression::ZLib::Decompress(compressedData); + + // Load and continue connection + Logger::Print("Received playlist, loading and continuing connection...\n"); + Game::Live_ParsePlaylists(Playlist::ReceivedPlaylistBuffer.data()); + Party::PlaylistContinue(); } } else