I am downloading a binary file (gsm object) with ClientConnection class.
When working in development environment with faster localhost server which serves gsm objects, everything works fine.
When I moved to a production server on different location, with slower internet access, I started getting partial downloads. After using a short sleep() in a loop, it works again but it is a bad solution.
Obviously BeginReceive() function works asynchronously. How can I check/wait if I've received all data?
ClientConnection clientConnection(connectionUrl);
try {
clientConnection.Connect();
Request getRequest(Method::Get, url);
clientConnection.Send(getRequest);
Response response;
GS::IChannelX channel(clientConnection.BeginReceive(response), GS::GetNetworkByteOrderIProtocolX());
GS::GSSize bufLen = 0;
if (response.GetStatusCode() == 200) {
IO::File* targetFile = new IO::File(dest, IO::File::Create);
targetFile->Open(IO::File::WriteMode);
while ((bufLen = (GS::GSSize)channel.GetAvailable()) > 0) {
GSPtr buffer = BMAllocatePtr(bufLen, ALLOCATE_CLEAR, 0);
channel.ReadBin((char *)buffer, bufLen);
targetFile->WriteBin((char *)buffer, bufLen);
BMKillPtr(&buffer);
Sleep(100);
}
targetFile->Close();
clientConnection.FinishReceive();
clientConnection.Close(false);
return targetFile;
}
clientConnection.FinishReceive();
clientConnection.Close(false);
}
catch (...) {
}