Re: Pausing, stopping and resuming downloads

On Wed, Feb 12, 2003 at 06:34:29PM +0100, Richard Atterer wrote:
> Pausing a download while leaving the connection open
> [...]

The code I posted was incorrect. It worked most of the time, but it failed
if you paused the download, then continued it, and then stopped it (by
deleting the request object), because the wrong HTEvent object was
re-registered when continuing.

The following code is hopefully correct - it Works For Me...
The part for finding the net object is the same:

  /* The HTNet object whose socket we'll unregister from the event loop. This
     will prevent more data from being delivered to it, effectively pausing
     the request. */
  HTNet* net = HTRequest_net(request);
  unsigned protocol = HTProtocol_id(HTNet_protocol(net));
  if (protocol == 21) {
    /* Protocol is FTP, which uses a control connection (which corresponds to
       the main HTNet object) and a data connection. We need the HTNet object
       for the latter. */
    ftp_ctrl* ctrl = static_cast<ftp_ctrl*>(HTNet_context(net));
    net = ctrl->dnet;
  }

But to pause the download, better use:

  HTChannel* channel = HTHost_channel(HTNet_host(net));
  HTEvent_unregister(HTChannel_socket(channel), HTEvent_READ);

And to continue downloading, use:

  HTHost* host = HTNet_host(net);
  HTHost_unregister(host, net, HTEvent_READ);
  HTHost_register(host, net, HTEvent_READ);



Why this weird "unregister, then register" thing on continuing? Well, I 
first tried

  pause:    HTHost_unregister(host, net, HTEvent_READ);
  continue: HTHost_register(host, net, HTEvent_READ);

but it turns out that HTHost_unregister() only works for everything
*except* HTEvent_READ - the source says this is in order to be notified of 
a closed connection %-(. My next attempt

  pause:    HTEvent_unregister(HTChannel_socket(channel), HTEvent_READ);
  continue: HTHost_register(host, net, HTEvent_READ);

failed because HTHost_register() checks a flag within the host object to
avoid unregistering twice, and that flag is not cleared by the first call. 
So I take advantage of a bug: HTHost_unregister() clears the flag in
question even though it subsequently refuses to unregister HTEvent_READ!

Who says that libwww bugs can't be useful sometimes! ;-)

Cheers,

  Richard

-- 
  __   _
  |_) /|  Richard Atterer     |  CS student at the Technische  |  GnuPG key:
  | \/¯|  http://atterer.net  |  Universität München, Germany  |  0x888354F7
  ¯ '` ¯

Received on Sunday, 2 March 2003 20:15:09 UTC