diff mbox

[v5,05/11] curl: add timer to BDRVCURLState

Message ID 1369280289-20928-6-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng May 23, 2013, 3:38 a.m. UTC
libcurl uses timer to manage ongoing sockets, it needs us to supply
timer. This patch introduce QEMUTimer to BDRVCURLState and handles
timeouts as libcurl expects (curl_multi_timer_cb sets given timeout
value on the timer and curl_timer_cb calls curl_multi_socket_action on
triggered).

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/curl.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

Comments

Stefan Hajnoczi May 23, 2013, 1:55 p.m. UTC | #1
On Thu, May 23, 2013 at 11:38:03AM +0800, Fam Zheng wrote:
> diff --git a/block/curl.c b/block/curl.c
> index fc464ad..4fd5bb9 100644
> --- a/block/curl.c
> +++ b/block/curl.c
> @@ -89,6 +89,7 @@ typedef struct BDRVCURLState {
>      QLIST_HEAD(, CURLSockInfo) socks;
>      char *url;
>      size_t readahead_size;
> +    QEMUTimer *timer;
>      /* Whether http server accept range in header */
>      bool accept_range;
>  } BDRVCURLState;
> @@ -148,6 +149,38 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
>      return realsize;
>  }
>  
> +static void curl_timer_cb(void *opaque)
> +{
> +    int running;
> +    BDRVCURLState *bs = (BDRVCURLState *)opaque;

Please call it 's'.  'bs' is for BlockDriverState*.

Also, there is no need to cast void* to BDRVCURLState*, the conversion
is implicit.

> +    DPRINTF("curl timeout!\n");
> +    curl_multi_socket_action(bs->multi, CURL_SOCKET_TIMEOUT, 0, &running);
> +}
> +
> +/* Call back for curl_multi interface */
> +static int curl_multi_timer_cb(CURLM *multi, long timeout_ms, void *s)
> +{
> +    BDRVCURLState *bs = (BDRVCURLState *)s;

Same here.
Fam Zheng May 24, 2013, 2:59 a.m. UTC | #2
On Thu, 05/23 15:55, Stefan Hajnoczi wrote:
> On Thu, May 23, 2013 at 11:38:03AM +0800, Fam Zheng wrote:
> > diff --git a/block/curl.c b/block/curl.c
> > index fc464ad..4fd5bb9 100644
> > --- a/block/curl.c
> > +++ b/block/curl.c
> > @@ -89,6 +89,7 @@ typedef struct BDRVCURLState {
> >      QLIST_HEAD(, CURLSockInfo) socks;
> >      char *url;
> >      size_t readahead_size;
> > +    QEMUTimer *timer;
> >      /* Whether http server accept range in header */
> >      bool accept_range;
> >  } BDRVCURLState;
> > @@ -148,6 +149,38 @@ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
> >      return realsize;
> >  }
> >  
> > +static void curl_timer_cb(void *opaque)
> > +{
> > +    int running;
> > +    BDRVCURLState *bs = (BDRVCURLState *)opaque;
> 
> Please call it 's'.  'bs' is for BlockDriverState*.
> 
> Also, there is no need to cast void* to BDRVCURLState*, the conversion
> is implicit.
> 
> > +    DPRINTF("curl timeout!\n");
> > +    curl_multi_socket_action(bs->multi, CURL_SOCKET_TIMEOUT, 0, &running);
> > +}
> > +
> > +/* Call back for curl_multi interface */
> > +static int curl_multi_timer_cb(CURLM *multi, long timeout_ms, void *s)
> > +{
> > +    BDRVCURLState *bs = (BDRVCURLState *)s;
> 
> Same here.
> 

OK.
diff mbox

Patch

diff --git a/block/curl.c b/block/curl.c
index fc464ad..4fd5bb9 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -89,6 +89,7 @@  typedef struct BDRVCURLState {
     QLIST_HEAD(, CURLSockInfo) socks;
     char *url;
     size_t readahead_size;
+    QEMUTimer *timer;
     /* Whether http server accept range in header */
     bool accept_range;
 } BDRVCURLState;
@@ -148,6 +149,38 @@  static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
     return realsize;
 }
 
+static void curl_timer_cb(void *opaque)
+{
+    int running;
+    BDRVCURLState *bs = (BDRVCURLState *)opaque;
+    DPRINTF("curl timeout!\n");
+    curl_multi_socket_action(bs->multi, CURL_SOCKET_TIMEOUT, 0, &running);
+}
+
+/* Call back for curl_multi interface */
+static int curl_multi_timer_cb(CURLM *multi, long timeout_ms, void *s)
+{
+    BDRVCURLState *bs = (BDRVCURLState *)s;
+    DPRINTF("curl multi timer cb, timeout: %ld (ms)\n", timeout_ms);
+    if (timeout_ms < 0) {
+        if (bs->timer) {
+            qemu_del_timer(bs->timer);
+            qemu_free_timer(bs->timer);
+            bs->timer = NULL;
+        }
+    } else if (timeout_ms == 0) {
+        curl_timer_cb(bs);
+    } else {
+        if (!bs->timer) {
+            bs->timer = qemu_new_timer_ms(host_clock, curl_timer_cb, s);
+            assert(bs->timer);
+        }
+        qemu_mod_timer(bs->timer, qemu_get_clock_ms(host_clock) + timeout_ms);
+    }
+
+    return 0;
+}
+
 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
 {
     CURLState *s = ((CURLState*)opaque);
@@ -509,6 +542,8 @@  static int curl_open(BlockDriverState *bs, QDict *options, int flags)
     }
     curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s);
     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
+    curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
+    curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_multi_timer_cb);
     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
 
     qemu_opts_del(opts);
@@ -634,6 +669,13 @@  static void curl_close(BlockDriverState *bs)
     int i;
 
     DPRINTF("CURL: Close\n");
+
+    if (s->timer) {
+        qemu_del_timer(s->timer);
+        qemu_free_timer(s->timer);
+        s->timer = NULL;
+    }
+
     for (i=0; i<CURL_NUM_STATES; i++) {
         if (s->states[i].in_use)
             curl_clean_state(&s->states[i]);