diff options
author | Lijo Antony <lta@one.com> | 2012-11-25 10:00:58 +0400 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2012-11-25 18:37:07 +0100 |
commit | 616a0099d14887b933c3b5bc5c253e6b8b1139f5 (patch) | |
tree | bf3f80e04c1d519f31c8a8fd11a6e1ddaac8ebe5 /docs | |
parent | 665adcd4b7bcdb7deb638cdc499fbe71f8d777f2 (diff) |
examples: Updated asiohiper.cpp to remove connect from opensocket
Blocking connect on the socket has been removed from opensocket
callback. opensocket just opens a new socket and gives it back to
libcurl and libcurl will take care of the connect. sockopt_callback has
also been removed, as it is no longer required.
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples/asiohiper.cpp | 59 |
1 files changed, 23 insertions, 36 deletions
diff --git a/docs/examples/asiohiper.cpp b/docs/examples/asiohiper.cpp index 1ea350253..44836fdc1 100644 --- a/docs/examples/asiohiper.cpp +++ b/docs/examples/asiohiper.cpp @@ -336,51 +336,39 @@ static curl_socket_t opensocket(void *clientp, curl_socket_t sockfd = CURL_SOCKET_BAD; - struct sockaddr_in * addr = (struct sockaddr_in *)&(address->addr); - char * ip_addr_str = inet_ntoa(addr->sin_addr); - unsigned short port = ntohs(addr->sin_port); - - /* create a tcp socket object */ - boost::asio::ip::address ip_addr = boost::asio::ip::address::from_string(ip_addr_str); - boost::asio::ip::tcp::endpoint endpoint(ip_addr, port); - boost::asio::ip::tcp::socket * tcp_socket = new boost::asio::ip::tcp::socket(io_service); + /* restrict to ipv4 */ + 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); - /* connect */ - boost::system::error_code ec; - tcp_socket->connect(endpoint, ec); + /* open it and get the native handle*/ + boost::system::error_code ec; + tcp_socket->open(boost::asio::ip::tcp::v4(), ec); - if (ec) - { - //An error occurred - std::cout << std::endl << "Couldn't connect to remote endpoint '" << endpoint << "' [" << ec << "][" << ec.message() << "]"; - fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error"); - } - else - { - sockfd = tcp_socket->native_handle(); - std::cout << std::endl << "Connected to remote endpoint '" << endpoint << "', with socket : " << sockfd; + if (ec) + { + //An error occurred + std::cout << std::endl << "Couldn't open socket [" << ec << "][" << ec.message() << "]"; + fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error"); + } + else + { + sockfd = tcp_socket->native_handle(); + fprintf(MSG_OUT, "\nOpened socket %d", sockfd); - /* save it for monitoring */ - socket_map.insert(std::pair<curl_socket_t, boost::asio::ip::tcp::socket *>(sockfd, tcp_socket)); + /* save it for monitoring */ + socket_map.insert(std::pair<curl_socket_t, boost::asio::ip::tcp::socket *>(sockfd, tcp_socket)); + } } return sockfd; } -/* CURLOPT_SOCKOPTFUNCTION */ -static int sockopt_callback(void *clientp, curl_socket_t curlfd, - curlsocktype purpose) -{ - fprintf(MSG_OUT, "\nsockopt_callback :"); - - /* This return code was added in libcurl 7.21.5 */ - return CURL_SOCKOPT_ALREADY_CONNECTED; -} - /* CURLOPT_CLOSESOCKETFUNCTION */ static int closesocket(void *clientp, curl_socket_t item) { - fprintf(MSG_OUT, "\nclosesocket :"); + fprintf(MSG_OUT, "\nclosesocket : %d", item); std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(item); @@ -427,8 +415,7 @@ static void new_conn(char *url, GlobalInfo *g ) /* call this function to get a socket */ curl_easy_setopt(conn->easy, CURLOPT_OPENSOCKETFUNCTION, opensocket); - /* call this function to set options for the socket */ - curl_easy_setopt(conn->easy, CURLOPT_SOCKOPTFUNCTION, sockopt_callback); + /* call this function to close a socket */ curl_easy_setopt(conn->easy, CURLOPT_CLOSESOCKETFUNCTION, closesocket); fprintf(MSG_OUT, |