Skip to content

Commit

Permalink
destroy CURL* when stopping, create when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperair committed Jan 14, 2010
1 parent 1fe5db4 commit e97f3ab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
70 changes: 42 additions & 28 deletions src/yatta/curl/chunk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Yatta
{
// constructor
Private (Download &parent, size_t offset, size_t total) :
handle (curl_easy_init ()),
handle (NULL),
parent (parent),
offset (offset),
downloaded (0),
Expand Down Expand Up @@ -88,19 +88,35 @@ namespace Yatta
Chunk::Chunk (Download &parent, size_t offset, size_t total) :
_priv (new Private (parent, offset, total))
{
// set some curl options...
}

// destructor
Chunk::~Chunk ()
{
// we're screwed if this happens
g_assert (!_priv->curl_callback_running);

stop ();
}

//member functions
void Chunk::start ()
{
if (running ()) return;

// create chunk
_priv->handle = curl_easy_init ();
curl_easy_setopt (handle (), CURLOPT_URL,
parent.url ().c_str ());
curl_easy_setopt (handle (), CURLOPT_RESUME_FROM, offset);
_priv->parent.url ().c_str ());
curl_easy_setopt (handle (), CURLOPT_RESUME_FROM, tell ());

// hack to make libcurl pass the range anyway..
if (offset == 0) {
// hack to tel libcurl to pass the range anyway to induce a 206
if (tell () == 0) {
struct curl_slist *headers = NULL;
std::ostringstream ss;
ss << "Range: bytes=" << offset << "-";
headers = curl_slist_append (
headers,
ss.str ().c_str ());
ss << "Range: bytes=" << tell () << "-";
headers = curl_slist_append (headers,
ss.str ().c_str ());
curl_easy_setopt (handle (), CURLOPT_HTTPHEADER, headers);
}

Expand All @@ -116,36 +132,29 @@ namespace Yatta
&on_curl_progress);
curl_easy_setopt (handle (), CURLOPT_HEADERFUNCTION,
&on_curl_header);
}

// destructor
Chunk::~Chunk ()
{
// we're screwed if this happens
g_assert (!_priv->curl_callback_running);

stop ();
curl_easy_cleanup (handle ());
}

//member functions
void Chunk::start ()
{
if (running ()) return;
Manager::get ()->add_handle (this);
_priv->running = true;
_priv->signal_started.emit ();
}

void Chunk::stop ()
{
if (!running ()) return;
if (_priv->curl_callback_running)

// we're in the middle of a callback, so set cancelled, and wait for
// curl to get rid of us. then stop_finished will be called which
// will call us again
if (_priv->curl_callback_running) {
_priv->cancelled = true;
return;
}

Manager::get ()->remove_handle (this);
_priv->running = false;
_priv->signal_stopped.emit ();
curl_easy_cleanup (_priv->handle);
_priv->handle = NULL;
_priv->cancelled = false;
}

void Chunk::stop_finished (CURLcode result)
Expand Down Expand Up @@ -219,7 +228,7 @@ namespace Yatta
// I/O status accessors
bool Chunk::running () const
{
return _priv->running;
return !_priv->cancelled && handle ();
}

size_t Chunk::offset() const
Expand Down Expand Up @@ -253,6 +262,11 @@ namespace Yatta
return _priv->handle;
}

const CURL *Chunk::handle () const
{
return _priv->handle;
}

// static CURL callbacks
size_t Chunk::on_curl_header (void *data, size_t size,
size_t nmemb, void *obj)
Expand Down
1 change: 1 addition & 0 deletions src/yatta/curl/chunk.hh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace Yatta
size_t tell () const;

CURL * handle ();
const CURL * handle () const;

protected:
// static callbacks for CURL C library
Expand Down

0 comments on commit e97f3ab

Please sign in to comment.