1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| #include "stdafx.h" #include <assert.h> #include "http_download.h"
typedef enum { CTX_T_REQ_HDR, CTX_T_RES_HDR, CTX_T_CONTENT_LENGTH, CTX_T_PARTIAL_LENGTH, CTX_T_END } ctx_t;
struct DOWN_CTX { ctx_t type; long long int length; };
static double stamp_sub(const struct timeval *from, const struct timeval *sub_by) { struct timeval res;
memcpy(&res, from, sizeof(struct timeval));
res.tv_usec -= sub_by->tv_usec; if (res.tv_usec < 0) { --res.tv_sec; res.tv_usec += 1000000; }
res.tv_sec -= sub_by->tv_sec; return (res.tv_sec * 1000.0 + res.tv_usec/1000.0); }
void http_download::rpc_run() { acl::http_request req(addr_); req.request_header().set_url(url_.c_str()) .set_content_type("text/html") .set_host(addr_.c_str()) .set_method(acl::HTTP_METHOD_GET);
req.request_header().build_request(req_hdr_); DOWN_CTX* ctx = new DOWN_CTX; ctx->type = CTX_T_REQ_HDR; rpc_signal(ctx);
struct timeval begin, end;; gettimeofday(&begin, NULL);
if (req.request(NULL, 0) == false) { logger_error("send request error"); error_ = false; gettimeofday(&end, NULL); total_spent_ = stamp_sub(&end, &begin); return; }
acl::http_client* conn = req.get_client(); assert(conn);
(void) conn->get_respond_head(&res_hdr_); ctx = new DOWN_CTX; ctx->type = CTX_T_RES_HDR; rpc_signal(ctx);
ctx = new DOWN_CTX; ctx->type = CTX_T_CONTENT_LENGTH; ctx->length = conn->body_length(); content_length_ = ctx->length; rpc_signal(ctx);
acl::string buf(8192); int real_size; while (true) { int ret = req.read_body(buf, true, &real_size); if (ret <= 0) { ctx = new DOWN_CTX; ctx->type = CTX_T_END; ctx->length = ret; rpc_signal(ctx); break; } ctx = new DOWN_CTX; ctx->type = CTX_T_PARTIAL_LENGTH; ctx->length = real_size; rpc_signal(ctx); }
gettimeofday(&end, NULL); total_spent_ = stamp_sub(&end, &begin);
}
http_download::http_download(const char* addr, const char* url, rpc_callback* callback) : addr_(addr) , url_(url) , callback_(callback) , error_(false) , total_read_(0) , content_length_(0) , total_spent_(0) {
}
void http_download::rpc_onover() { logger("http download(%s) over, 共 %I64d 字节,耗时 %.3f 毫秒", url_.c_str(), total_read_, total_spent_); callback_->OnDownloadOver(total_read_, total_spent_); delete this; }
void http_download::rpc_wakeup(void* ctx) { DOWN_CTX* down_ctx = (DOWN_CTX*) ctx;
switch (down_ctx->type) { case CTX_T_REQ_HDR: callback_->SetRequestHdr(req_hdr_.c_str()); break; case CTX_T_RES_HDR: callback_->SetResponseHdr(res_hdr_.c_str()); break; case CTX_T_CONTENT_LENGTH: break; case CTX_T_PARTIAL_LENGTH: total_read_ += down_ctx->length; callback_->OnDownloading(content_length_, total_read_); break; case CTX_T_END: logger("%s: read over", addr_.c_str()); break; default: logger_error("%s: ERROR", addr_.c_str()); break; }
delete down_ctx; }
|