fixed mod download

This commit is contained in:
Anonymous275 2020-12-20 16:04:25 +02:00
parent bb70054293
commit 62f319a90e
2 changed files with 21 additions and 16 deletions

View File

@ -1,5 +1,7 @@
Copyright (c) 2020 BeamMP-Server & BeamMP-Launcher Developers. // Copyright (c) 2019-present Anonymous275.
BeamMP Server code is not in the public domain and is not free software. One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. // BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
/// ///
/// Created by Anonymous275 on 7/17/2020 /// Created by Anonymous275 on 7/17/2020
/// ///

View File

@ -118,11 +118,11 @@ void AsyncUpdate(uint64_t& Rcv,uint64_t Size,const std::string& Name){
}while(!Terminate && Rcv < Size); }while(!Terminate && Rcv < Size);
} }
std::string TCPRcvRaw(SOCKET Sock,uint64_t& GRcv, uint64_t Size){ char* TCPRcvRaw(SOCKET Sock,uint64_t& GRcv, uint64_t Size){
if(Sock == -1){ if(Sock == -1){
Terminate = true; Terminate = true;
UUl("Invalid Socket"); UUl("Invalid Socket");
return ""; return nullptr;
} }
char* File = new char[Size]; char* File = new char[Size];
uint64_t Rcv = 0; uint64_t Rcv = 0;
@ -137,14 +137,12 @@ std::string TCPRcvRaw(SOCKET Sock,uint64_t& GRcv, uint64_t Size){
KillSocket(Sock); KillSocket(Sock);
Terminate = true; Terminate = true;
delete[] File; delete[] File;
return ""; return nullptr;
} }
Rcv += Temp; Rcv += Temp;
GRcv += Temp; GRcv += Temp;
}while(Rcv < Size && !Terminate); }while(Rcv < Size && !Terminate);
std::string Ret = std::string(File,Size); return File;
delete[] File;
return Ret;
} }
void MultiKill(SOCKET Sock,SOCKET Sock1){ void MultiKill(SOCKET Sock,SOCKET Sock1){
KillSocket(Sock1); KillSocket(Sock1);
@ -182,29 +180,34 @@ std::string MultiDownload(SOCKET MSock,SOCKET DSock, uint64_t Size, const std::s
std::thread Au(AsyncUpdate,std::ref(GRcv),Size,Name); std::thread Au(AsyncUpdate,std::ref(GRcv),Size,Name);
Au.detach(); Au.detach();
std::packaged_task<std::string()> task([&] { return TCPRcvRaw(MSock,GRcv,MSize); }); std::packaged_task<char*()> task([&] { return TCPRcvRaw(MSock,GRcv,MSize); });
std::future<std::string> f1 = task.get_future(); std::future<char*> f1 = task.get_future();
std::thread Dt(std::move(task)); std::thread Dt(std::move(task));
Dt.detach(); Dt.detach();
std::string Ret = TCPRcvRaw(DSock,GRcv,DSize); char* DData = TCPRcvRaw(DSock,GRcv,DSize);
if(Ret.empty()){ if(!DData){
MultiKill(MSock,DSock); MultiKill(MSock,DSock);
return ""; return "";
} }
f1.wait(); f1.wait();
std::string Temp = f1.get(); char* MData = f1.get();
if(Temp.empty()){ if(!MData){
MultiKill(MSock,DSock); MultiKill(MSock,DSock);
return ""; return "";
} }
if(Au.joinable())Au.join(); if(Au.joinable())Au.join();
std::string Ret(Size,0);
memcpy_s(&Ret[0],MSize,MData,MSize);
delete[]MData;
memcpy_s(&Ret[MSize],DSize,DData,DSize);
delete[]DData;
Ret += Temp;
return Ret; return Ret;
} }
@ -286,7 +289,7 @@ void SyncResources(SOCKET Sock){
std::ofstream LFS; std::ofstream LFS;
LFS.open(a.c_str(), std::ios_base::app | std::ios::binary); LFS.open(a.c_str(), std::ios_base::app | std::ios::binary);
if (LFS.is_open()) { if (LFS.is_open()) {
LFS.write(Data.c_str(), Data.size()); LFS.write(&Data[0], Data.size());
LFS.close(); LFS.close();
} }