Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Fixed curl example #31

Merged
merged 3 commits into from Mar 28, 2013
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 52 additions & 21 deletions code/uvwget/main.c
@@ -1,13 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <uv.h>
#include <curl/curl.h>

uv_loop_t *loop;
CURLM *curl_handle;
uv_timer_t timeout;

typedef struct curl_context_s {
uv_poll_t poll_handle;
curl_socket_t sockfd;
} curl_context_t;

curl_context_t* create_curl_context(curl_socket_t sockfd) {
int r;
curl_context_t* context;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pointer style 'context' and not ' context'


context = (curl_context_t*) malloc(sizeof *context);

context->sockfd = sockfd;

r = uv_poll_init_socket(loop, &context->poll_handle, sockfd);
context->poll_handle.data = context;

return context;
}

void curl_close_cb(uv_handle_t* handle) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again pointer notation

curl_context_t* context = (curl_context_t*) handle->data;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

free(context);
}

void destroy_curl_context(curl_context_t* context) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

uv_close((uv_handle_t*) &context->poll_handle, curl_close_cb);
}


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent this whole block to 4 spaces please.

void add_download(const char *url, int num) {
char filename[50];
sprintf(filename, "%d.download", num);
Expand All @@ -33,25 +61,29 @@ void curl_perform(uv_poll_t *req, int status, int events) {
if (events & UV_READABLE) flags |= CURL_CSELECT_IN;
if (events & UV_WRITABLE) flags |= CURL_CSELECT_OUT;

curl_multi_socket_action(curl_handle, req->io_watcher.fd, flags, &running_handles);
curl_context_t *context;

context = (curl_context_t*)req;

curl_multi_socket_action(curl_handle, context->sockfd, flags, &running_handles);

char *done_url;

CURLMsg *message;
int pending;
while ((message = curl_multi_info_read(curl_handle, &pending))) {
switch (message->msg) {
case CURLMSG_DONE:
curl_easy_getinfo(message->easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
printf("%s DONE\n", done_url);
case CURLMSG_DONE:
curl_easy_getinfo(message->easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
printf("%s DONE\n", done_url);

curl_multi_remove_handle(curl_handle, message->easy_handle);
curl_easy_cleanup(message->easy_handle);
curl_multi_remove_handle(curl_handle, message->easy_handle);
curl_easy_cleanup(message->easy_handle);

break;
default:
fprintf(stderr, "CURLMSG default\n");
abort();
break;
default:
fprintf(stderr, "CURLMSG default\n");
abort();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'case' should be indented one more level than 'switch'

}
}
}
Expand All @@ -68,29 +100,28 @@ void start_timeout(CURLM *multi, long timeout_ms, void *userp) {
}

int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp) {
uv_poll_t *poll_fd;
curl_context_t *curl_context;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces

if (action == CURL_POLL_IN || action == CURL_POLL_OUT) {
if (socketp) {
poll_fd = (uv_poll_t*) socketp;
curl_context = (curl_context_t*) socketp;
}
else {
poll_fd = (uv_poll_t*) malloc(sizeof(uv_poll_t));
uv_poll_init(loop, poll_fd, s);
curl_context = create_curl_context(s);
}
curl_multi_assign(curl_handle, s, (void *) poll_fd);
curl_multi_assign(curl_handle, s, (void *) curl_context);
}

switch (action) {
case CURL_POLL_IN:
uv_poll_start(poll_fd, UV_READABLE, curl_perform);
uv_poll_start(&curl_context->poll_handle, UV_READABLE, curl_perform);
break;
case CURL_POLL_OUT:
uv_poll_start(poll_fd, UV_WRITABLE, curl_perform);
uv_poll_start(&curl_context->poll_handle, UV_WRITABLE, curl_perform);
break;
case CURL_POLL_REMOVE:
if (socketp) {
uv_poll_stop((uv_poll_t*) socketp);
uv_close((uv_handle_t*) socketp, (uv_close_cb) free);
uv_poll_stop(&((curl_context_t*)socketp)->poll_handle);
destroy_curl_context((curl_context_t*) socketp);
curl_multi_assign(curl_handle, s, NULL);
}
break;
Expand Down Expand Up @@ -125,4 +156,4 @@ int main(int argc, char **argv) {
uv_run(loop, UV_RUN_DEFAULT);
curl_multi_cleanup(curl_handle);
return 0;
}
}