From f801e16b787f033cea4e473d69b54fe65248a439 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 25 Mar 2012 11:50:29 +0300 Subject: Texture cache -> Media cache WIP --- src/server.cpp | 224 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 113 insertions(+), 111 deletions(-) (limited to 'src/server.cpp') diff --git a/src/server.cpp b/src/server.cpp index 745e55f83..d6bb14f95 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -957,7 +957,7 @@ Server::Server( } // Read Textures and calculate sha1 sums - PrepareTextures(); + fillMediaCache(); // Apply item aliases in the node definition manager m_nodedef->updateAliases(m_itemdef); @@ -2183,7 +2183,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) SendNodeDef(m_con, peer_id, m_nodedef); // Send texture announcement - SendTextureAnnouncement(peer_id); + sendMediaAnnouncement(peer_id); // Send player info to all players //SendPlayerInfos(); @@ -2842,29 +2842,28 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // ActiveObject is added to environment in AsyncRunStep after // the previous addition has been succesfully removed } - else if(command == TOSERVER_REQUEST_TEXTURES) { + else if(command == TOSERVER_REQUEST_MEDIA) { std::string datastring((char*)&data[2], datasize-2); std::istringstream is(datastring, std::ios_base::binary); - - core::list tosend; - u16 numtextures = readU16(is); + core::list tosend; + u16 numfiles = readU16(is); - infostream<<"Sending "<definitions_sent = true; } else if(command == TOSERVER_INTERACT) @@ -3928,32 +3927,32 @@ void Server::SendBlocks(float dtime) } } -void Server::PrepareTextures() +void Server::fillMediaCache() { DSTACK(__FUNCTION_NAME); - infostream<<"Server: Calculating texture checksums"<::Iterator i = m_mods.begin(); i != m_mods.end(); i++){ const ModSpec &mod = *i; - std::string texturepath = mod.path + DIR_DELIM + "textures"; - std::vector dirlist = fs::GetDirListing(texturepath); + std::string filepath = mod.path + DIR_DELIM + "textures"; + std::vector dirlist = fs::GetDirListing(filepath); for(u32 j=0; jm_Textures[tname] = TextureInformation(tpath,digest_string); + this->m_media[tname] = MediaInfo(tpath,digest_string); verbosestream<<"Server: sha1 for "< texture_announcements; - - for (std::map::iterator i = m_Textures.begin();i != m_Textures.end(); i++ ) { + core::list file_announcements; + for(std::map::iterator i = m_media.begin(); + i != m_media.end(); i++){ // Put in list - texture_announcements.push_back( - SendableTextureAnnouncement(i->first, i->second.sha1_digest)); + file_announcements.push_back( + SendableMediaAnnouncement(i->first, i->second.sha1_digest)); } - //send announcements + // Make packet + std::ostringstream os(std::ios_base::binary); /* u16 command - u32 number of textures + u32 number of files for each texture { u16 length of name string name - u16 length of digest string + u16 length of sha1_digest string sha1_digest } */ - std::ostringstream os(std::ios_base::binary); - - writeU16(os, TOCLIENT_ANNOUNCE_TEXTURES); - writeU16(os, texture_announcements.size()); + + writeU16(os, TOCLIENT_ANNOUNCE_MEDIA); + writeU16(os, file_announcements.size()); - for(core::list::Iterator - j = texture_announcements.begin(); - j != texture_announcements.end(); j++){ + for(core::list::Iterator + j = file_announcements.begin(); + j != file_announcements.end(); j++){ os<name); os<sha1_digest); } @@ -4058,13 +4057,13 @@ void Server::SendTextureAnnouncement(u16 peer_id){ } -struct SendableTexture +struct SendableMedia { std::string name; std::string path; std::string data; - SendableTexture(const std::string &name_="", const std::string path_="", + SendableMedia(const std::string &name_="", const std::string path_="", const std::string &data_=""): name(name_), path(path_), @@ -4072,36 +4071,40 @@ struct SendableTexture {} }; -void Server::SendTexturesRequested(u16 peer_id,core::list tosend) { +void Server::sendRequestedMedia(u16 peer_id, + const core::list &tosend) +{ DSTACK(__FUNCTION_NAME); - verbosestream<<"Server::SendTexturesRequested(): " - <<"Sending textures to client"< > texture_bunches; - texture_bunches.push_back(core::list()); + core::array< core::list > file_bunches; + file_bunches.push_back(core::list()); - u32 texture_size_bunch_total = 0; + u32 file_size_bunch_total = 0; - for(core::list::Iterator i = tosend.begin(); i != tosend.end(); i++) { - if(m_Textures.find(i->name) == m_Textures.end()){ - errorstream<<"Server::SendTexturesRequested(): Client asked for " - <<"unknown texture \""<<(i->name)<<"\""<::ConstIterator i = tosend.begin(); + i != tosend.end(); i++) + { + if(m_media.find(i->name) == m_media.end()){ + errorstream<<"Server::sendRequestedMedia(): Client asked for " + <<"unknown file \""<<(i->name)<<"\""< tosend fis.read(buf, 1024); std::streamsize len = fis.gcount(); tmp_os.write(buf, len); - texture_size_bunch_total += len; + file_size_bunch_total += len; if(fis.eof()) break; if(!fis.good()){ @@ -4121,67 +4124,66 @@ void Server::SendTexturesRequested(u16 peer_id,core::list tosend } } if(bad){ - errorstream<<"Server::SendTexturesRequested(): Failed to read \"" + errorstream<<"Server::sendRequestedMedia(): Failed to read \"" <<(*i).name<<"\""<= bytes_per_bunch){ - texture_bunches.push_back(core::list()); - texture_size_bunch_total = 0; + if(file_size_bunch_total >= bytes_per_bunch){ + file_bunches.push_back(core::list()); + file_size_bunch_total = 0; } } /* Create and send packets */ - u32 num_bunches = texture_bunches.size(); - for(u32 i=0; i::Iterator - j = texture_bunches[i].begin(); - j != texture_bunches[i].end(); j++){ - os<name); - os<data); + /* + u16 command + u16 total number of texture bunches + u16 index of this bunch + u32 number of files in this bunch + for each file { + u16 length of name + string name + u32 length of data + data } + */ - // Make data buffer - std::string s = os.str(); - verbosestream<<"Server::SendTexturesRequested(): bunch " - <