X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=docs%2Fexamples%2Fasiohiper.cpp;h=d6065e55491f81c97721cad4825412db305cfbb7;hb=88d478478059f53e6de0d1267c3a1e6b02c4273f;hp=ced4ef419aaa1e757a7f4a94c4ce81533bfa9eb6;hpb=e9bdad71c8277e20607fa1eaf0027d53a0dc1f37;p=platform%2Fupstream%2Fcurl.git diff --git a/docs/examples/asiohiper.cpp b/docs/examples/asiohiper.cpp index ced4ef4..d6065e5 100644 --- a/docs/examples/asiohiper.cpp +++ b/docs/examples/asiohiper.cpp @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2012 - 2017, Daniel Stenberg, , et al. + * Copyright (C) 2012 - 2015, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -40,8 +40,8 @@ * Note: * For the sake of simplicity, URL is hard coded to "www.google.com" * - * This is purely a demo app, all retrieved data is simply discarded by the - * write callback. + * This is purely a demo app, all retrieved data is simply discarded by the write + * callback. */ @@ -85,12 +85,14 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) /* cancel running timer */ timer.cancel(); - if(timeout_ms > 0) { + if(timeout_ms > 0) + { /* update timer */ timer.expires_from_now(boost::posix_time::millisec(timeout_ms)); timer.async_wait(boost::bind(&timer_cb, _1, g)); } - else { + else + { /* call timeout function immediately */ boost::system::error_code error; /*success*/ timer_cb(error, g); @@ -102,9 +104,11 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) /* Die if we get a bad CURLMcode somewhere */ static void mcode_or_die(const char *where, CURLMcode code) { - if(CURLM_OK != code) { + if(CURLM_OK != code) + { const char *s; - switch(code) { + switch(code) + { case CURLM_CALL_MULTI_PERFORM: s = "CURLM_CALL_MULTI_PERFORM"; break; @@ -154,8 +158,10 @@ static void check_multi_info(GlobalInfo *g) fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running); - while((msg = curl_multi_info_read(g->multi, &msgs_left))) { - if(msg->msg == CURLMSG_DONE) { + while((msg = curl_multi_info_read(g->multi, &msgs_left))) + { + if(msg->msg == CURLMSG_DONE) + { easy = msg->easy_handle; res = msg->data.result; curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); @@ -171,55 +177,33 @@ static void check_multi_info(GlobalInfo *g) /* Called by asio when there is an action on a socket */ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket, - int action, const boost::system::error_code & error, - int *fdp) + int action) { fprintf(MSG_OUT, "\nevent_cb: action=%d", action); - /* make sure the event matches what are wanted */ - if(*fdp == action || *fdp == CURL_POLL_INOUT) { - curl_socket_t s = tcp_socket->native_handle(); - CURLMcode rc; - if(error) - action = CURL_CSELECT_ERR; - rc = curl_multi_socket_action(g->multi, s, action, &g->still_running); - - mcode_or_die("event_cb: curl_multi_socket_action", rc); - check_multi_info(g); + CURLMcode rc; + rc = curl_multi_socket_action(g->multi, tcp_socket->native_handle(), action, + &g->still_running); - if(g->still_running <= 0) { - fprintf(MSG_OUT, "\nlast transfer done, kill timeout"); - timer.cancel(); - } + mcode_or_die("event_cb: curl_multi_socket_action", rc); + check_multi_info(g); - /* keep on watching. - * the socket may have been closed and/or fdp may have been changed - * in curl_multi_socket_action(), so check them both */ - if(!error && socket_map.find(s) != socket_map.end() && - (*fdp == action || *fdp == CURL_POLL_INOUT)) { - if(action == CURL_POLL_IN) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - action, _1, fdp)); - } - if(action == CURL_POLL_OUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - action, _1, fdp)); - } - } + if(g->still_running <= 0) + { + fprintf(MSG_OUT, "\nlast transfer done, kill timeout"); + timer.cancel(); } } /* Called by asio when our timeout expires */ static void timer_cb(const boost::system::error_code & error, GlobalInfo *g) { - if(!error) { + if(!error) + { fprintf(MSG_OUT, "\ntimer_cb: "); CURLMcode rc; - rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, - &g->still_running); + rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); mcode_or_die("timer_cb: curl_multi_socket_action", rc); check_multi_info(g); @@ -231,21 +215,22 @@ static void remsock(int *f, GlobalInfo *g) { fprintf(MSG_OUT, "\nremsock: "); - if(f) { + if(f) + { free(f); } } -static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact, - GlobalInfo *g) +static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, GlobalInfo*g) { fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp); - std::map::iterator it = - socket_map.find(s); + std::map::iterator it = socket_map.find(s); - if(it == socket_map.end()) { + if(it == socket_map.end()) + { fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s); + return; } @@ -253,34 +238,29 @@ static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact, *fdp = act; - if(act == CURL_POLL_IN) { + if(act == CURL_POLL_IN) + { fprintf(MSG_OUT, "\nwatching for socket to become readable"); - if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_IN, _1, fdp)); - } + + tcp_socket->async_read_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } - else if(act == CURL_POLL_OUT) { + else if (act == CURL_POLL_OUT) + { fprintf(MSG_OUT, "\nwatching for socket to become writable"); - if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_OUT, _1, fdp)); - } + + tcp_socket->async_write_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } - else if(act == CURL_POLL_INOUT) { + else if(act == CURL_POLL_INOUT) + { fprintf(MSG_OUT, "\nwatching for socket to become readable & writable"); - if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_IN, _1, fdp)); - } - if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_OUT, _1, fdp)); - } + + tcp_socket->async_read_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); + + tcp_socket->async_write_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } } @@ -289,7 +269,7 @@ static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) /* fdp is used to store current action */ int *fdp = (int *) calloc(sizeof(int), 1); - setsock(fdp, s, easy, action, 0, g); + setsock(fdp, s, easy, action, g); curl_multi_assign(g->multi, s, fdp); } @@ -305,20 +285,24 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) fprintf(MSG_OUT, "\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); - if(what == CURL_POLL_REMOVE) { + if(what == CURL_POLL_REMOVE) + { fprintf(MSG_OUT, "\n"); remsock(actionp, g); } - else { - if(!actionp) { + else + { + if(!actionp) + { fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]); addsock(s, e, what, g); } - else { + else + { fprintf(MSG_OUT, "\nChanging action from %s to %s", whatstr[*actionp], whatstr[what]); - setsock(actionp, s, e, what, *actionp, g); + setsock(actionp, s, e, what, g); } } @@ -328,8 +312,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) /* CURLOPT_WRITEFUNCTION */ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) { + size_t written = size * nmemb; - char *pBuffer = (char *)malloc(written + 1); + char* pBuffer = (char *) malloc(written + 1); strncpy(pBuffer, (const char *)ptr, written); pBuffer[written] = '\0'; @@ -365,28 +350,28 @@ static curl_socket_t opensocket(void *clientp, curlsocktype purpose, curl_socket_t sockfd = CURL_SOCKET_BAD; /* restrict to IPv4 */ - if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) { + if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) + { /* create a tcp socket object */ - boost::asio::ip::tcp::socket *tcp_socket = - new boost::asio::ip::tcp::socket(io_service); + boost::asio::ip::tcp::socket *tcp_socket = new boost::asio::ip::tcp::socket(io_service); /* open it and get the native handle*/ boost::system::error_code ec; tcp_socket->open(boost::asio::ip::tcp::v4(), ec); - if(ec) { + if(ec) + { /* An error occurred */ - std::cout << std::endl << "Couldn't open socket [" << ec << "][" << - ec.message() << "]"; + std::cout << std::endl << "Couldn't open socket [" << ec << "][" << ec.message() << "]"; fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error"); } - else { + else + { sockfd = tcp_socket->native_handle(); fprintf(MSG_OUT, "\nOpened socket %d", sockfd); /* save it for monitoring */ - socket_map.insert(std::pair(sockfd, tcp_socket)); + socket_map.insert(std::pair(sockfd, tcp_socket)); } } @@ -398,10 +383,10 @@ static int close_socket(void *clientp, curl_socket_t item) { fprintf(MSG_OUT, "\nclose_socket : %d", item); - std::map::iterator it = - socket_map.find(item); + std::map::iterator it = socket_map.find(item); - if(it != socket_map.end()) { + if(it != socket_map.end()) + { delete it->second; socket_map.erase(it); } @@ -418,8 +403,10 @@ static void new_conn(char *url, GlobalInfo *g) conn = (ConnInfo *) calloc(1, sizeof(ConnInfo)); conn->easy = curl_easy_init(); - if(!conn->easy) { + if(!conn->easy) + { fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!"); + exit(2); }