浏览代码

Changed semantics of cancelled http requests. Before they were reported

as cancelled, or possibly ok or failed if the request had come that far.
Now cancelled requests are silently discarded and doesn't execute the
callback. This makes the http api easier to use. Updated the tracker code
for the new http semantics.
master
Richard Nyberg 19 年前
父节点
当前提交
8aed86f7d2
共有 2 个文件被更改,包括 12 次插入49 次删除
  1. +3
    -2
      btpd/http.c
  2. +9
    -47
      btpd/tracker_req.c

+ 3
- 2
btpd/http.c 查看文件

@@ -94,7 +94,7 @@ http_cancel(struct http *http)
pthread_mutex_lock(&m_httpq_lock); pthread_mutex_lock(&m_httpq_lock);
if (http->state == HS_ADD) if (http->state == HS_ADD)
http->state = HS_NOADD; http->state = HS_NOADD;
else if (http->state == HS_ACTIVE) else
http->state = HS_CANCEL; http->state = HS_CANCEL;
pthread_mutex_unlock(&m_httpq_lock); pthread_mutex_unlock(&m_httpq_lock);
} }
@@ -115,7 +115,8 @@ http_td_cb(void *arg)
btpd_log(BTPD_L_ERROR, "Http error for url '%s' (%s).\n", h->url, btpd_log(BTPD_L_ERROR, "Http error for url '%s' (%s).\n", h->url,
curl_easy_strerror(h->res.code)); curl_easy_strerror(h->res.code));
} }
h->cb(h, &h->res, h->cb_arg); if (h->state != HS_CANCEL)
h->cb(h, &h->res, h->cb_arg);
curl_easy_cleanup(h->curlh); curl_easy_cleanup(h->curlh);
if (h->res.content != NULL) if (h->res.content != NULL)
free(h->res.content); free(h->res.content);


+ 9
- 47
btpd/tracker_req.c 查看文件

@@ -25,16 +25,8 @@ enum timer_type {
TIMER_RETRY TIMER_RETRY
}; };


enum http_type {
HTTP_NONE,
HTTP_NORMAL,
HTTP_RETRY,
HTTP_NEW
};

struct tracker { struct tracker {
enum timer_type ttype; enum timer_type ttype;
enum http_type htype;
enum tr_event event; enum tr_event event;
int interval; int interval;
unsigned nerrors; unsigned nerrors;
@@ -135,26 +127,14 @@ http_cb(struct http *req, struct http_res *res, void *arg)
{ {
struct torrent *tp = arg; struct torrent *tp = arg;
struct tracker *tr = tp->tr; struct tracker *tr = tp->tr;
switch (tr->htype) { assert(tr->ttype == TIMER_TIMEOUT);
case HTTP_NORMAL: if ((http_succeeded(res) &&
if ((http_succeeded(res) && parse_reply(tp, res->content, res->length) == 0)) {
parse_reply(tp, res->content, res->length) == 0)) { tr->ttype = TIMER_INTERVAL;
tr->htype = HTTP_NONE; event_add(&tr->timer, (& (struct timeval) { tr->interval, 0 }));
tr->ttype = TIMER_INTERVAL; } else {
event_add(&tr->timer, (& (struct timeval) { tr->interval, 0 }));
break;
}
case HTTP_RETRY:
tr->htype = HTTP_NONE;
tr->ttype = TIMER_RETRY; tr->ttype = TIMER_RETRY;
event_add(&tr->timer, RETRY_WAIT); event_add(&tr->timer, RETRY_WAIT);
break;
case HTTP_NEW:
tr->htype = HTTP_NONE;
tr_send(tp, tr->event);
break;
default:
abort();
} }
} }


@@ -165,16 +145,12 @@ timer_cb(int fd, short type, void *arg)
struct tracker *tr = tp->tr; struct tracker *tr = tp->tr;
switch (tr->ttype) { switch (tr->ttype) {
case TIMER_TIMEOUT: case TIMER_TIMEOUT:
http_cancel(tr->req); case TIMER_RETRY:
tr->htype = HTTP_RETRY; tr_send(tp, tp->tr->event);
tr->ttype = TIMER_NONE;
break; break;
case TIMER_INTERVAL: case TIMER_INTERVAL:
tr_send(tp, TR_EV_EMPTY); tr_send(tp, TR_EV_EMPTY);
break; break;
case TIMER_RETRY:
tr_send(tp, tr->event);
break;
default: default:
abort(); abort();
} }
@@ -188,22 +164,8 @@ tr_send(struct torrent *tp, enum tr_event event)


struct tracker *tr = tp->tr; struct tracker *tr = tp->tr;
tr->event = event; tr->event = event;
switch (tr->htype) { if (tr->ttype == TIMER_TIMEOUT)
case HTTP_NORMAL:
tr->htype = HTTP_NEW;
tr->ttype = TIMER_NONE;
event_del(&tr->timer);
http_cancel(tr->req); http_cancel(tr->req);
return;
case HTTP_RETRY:
tr->htype = HTTP_NEW;
return;
case HTTP_NEW:
return;
default:
tr->htype = HTTP_NORMAL;
}

tr->ttype = TIMER_TIMEOUT; tr->ttype = TIMER_TIMEOUT;
event_add(&tr->timer, REQ_TIMEOUT); event_add(&tr->timer, REQ_TIMEOUT);




||||||
x
 
000:0
正在加载...
取消
保存