Search the Knowledge Base Report a bug
Post new topic   Reply to topic   printer-friendly view   .    ArchiCAD-Talk Forum Index >>> Developer Forum >>> ClientConnection partial download
View previous topic :: View next topic
Author Message
vuego
New member
New member

Joined: 19 Sep 2006
Posts: 28

PostPosted: Fri Sep 22, 2017 7:33 am    Post subject: ClientConnection partial download Reply with quote

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?



Code:

   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 (...) {

   }

_________________
ARCHICAD 21, Dell Studio i7, 16Gb Ram, Win10 64bit
Back to top
View user's profile Send private message    
Display posts from previous:   
View previous topic :: View next topic
Post new topic   Reply to topic   printer-friendly view       ArchiCAD-Talk Forum Index >>> Developer Forum >>> ClientConnection partial download All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2001, 2005 phpBB Group
Copyright 2012 - Graphisoft SE. All rights reserved worldwide. Terms of Use | Privacy Policy