コード例 #1
0
    static size_t onWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
    {
        WebRequestInternalState *is_(reinterpret_cast<WebRequestInternalState*>(userdata));
        is_->state = HTTP_OPEN;
        if (is_->isAborted)
        {
            is_->state = HTTP_CLOSED;
            // This should probably be CURL_WRITEFUNC_ABORT, but that doesn't
            // exist. It probably would be the same numeric value, if it did.
            // The docs say that it just has to be a number of bytes that is
            // not "size * nmemb" to abort.
            return CURL_READFUNC_ABORT;
        }

        // Find the size in bytes.
        size_t real_size(size * nmemb);

        // Write the date into the download buffer queue.
        Serializer* download(dynamic_cast<Serializer*>(is_->download.Get()));
        download->Write(ptr, (unsigned int)real_size);

        // Emit a "download_chunk" event.
        VariantMap eventData;
        eventData.Insert(MakePair(StringHash("download"), Variant(is_->download)));
        eventData.Insert(MakePair(StringHash("size"), Variant((unsigned int)real_size)));
        is_->es.SendEvent("download_chunk", eventData);

        return real_size;
    }
コード例 #2
0
    static size_t onRead(char *buffer, size_t size, size_t nitems, void *instream)
    {
        WebRequestInternalState *is_(reinterpret_cast<WebRequestInternalState*>(instream));
        is_->state = HTTP_OPEN;
        if (is_->isAborted)
        {
            is_->state = HTTP_CLOSED;
            return CURL_READFUNC_ABORT;
        }

        // Find the size in bytes.
        size_t real_size(size * nitems);

        // Read as much as we can from the upload buffer queue.
        Deserializer* upload(dynamic_cast<Deserializer*>(is_->upload.Get()));
        size_t size_queued(upload->GetSize());
        size_t size_left(real_size);
        if ((size_left > 0) && (size_queued > 0))
        {
            size_t read_size(std::min(size_queued, size_left));
            upload->Read(buffer, (unsigned int)read_size);
            size_left -= read_size;
        }

        // If we still have bytes to fill, then emit a "upload_chunk" event.
        if (size_left > 0)
        {
            VariantMap eventData;
            eventData.Insert(MakePair(StringHash("upload"), Variant(is_->upload)));
            eventData.Insert(MakePair(StringHash("size"), Variant((unsigned int)size_left)));
            is_->es.SendEvent("upload_chunk", eventData);
        }

        // Read as much as we can from the upload buffer queue (again).
        size_queued = upload->GetSize();
        size_left = real_size;
        if ((size_left > 0) && (size_queued > 0))
        {
            size_t read_size(std::min(size_queued, size_left));
            upload->Read(buffer, (unsigned int)read_size);
            size_left -= read_size;
        }

        // If we still have bytes to fill, then something went wrong, so we should abort.
        if (size_left > 0)
        {
            is_->isAborted = true;
            return CURL_READFUNC_ABORT;
        }

        return real_size;
    }
コード例 #3
0
 void onEnd(int code)
 {
     VariantMap eventData;
     if (code != CURLE_OK)
     {
         state = HTTP_ERROR;
         eventData.Insert(MakePair(StringHash("error"), Variant(String(error, (unsigned int)strnlen(error, sizeof(error))))));
     }
     else
     {
         state = HTTP_CLOSED;
         eventData.Insert(MakePair(StringHash("download"), Variant(download)));
         eventData.Insert(MakePair(StringHash("upload"), Variant(upload)));
     }
     es.SendEvent("complete", eventData);
 }
コード例 #4
0
    static int onProgress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
    {
        WebRequestInternalState *is_(reinterpret_cast<WebRequestInternalState*>(clientp));
        if (is_->isAborted)
        {
            // This should probably be CURL_XFERINFO_ABORT, but that doesn't
            // exist. It probably would be the same numeric value, if it did.
            // The docs say that it just has to be a nonzero to abort.
            return CURL_READFUNC_ABORT;
        }

        VariantMap eventData;
        eventData.Insert(MakePair(StringHash("down_total"), Variant((double)dltotal)));
        eventData.Insert(MakePair(StringHash("down_loaded"), Variant((double)dlnow)));
        eventData.Insert(MakePair(StringHash("up_total"), Variant((double)ultotal)));
        eventData.Insert(MakePair(StringHash("up_loaded"), Variant((double)ulnow)));
        is_->es.SendEvent("progress", eventData);
        return 0;
    }