cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] examples: Updated asiohiper.cpp to remove connect from opensocket

From: Lijo Antony <lta_at_one.com>
Date: Sun, 25 Nov 2012 10:00:58 +0400

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.

---
 docs/examples/asiohiper.cpp |   59 +++++++++++++++++--------------------------
 1 file changed, 23 insertions(+), 36 deletions(-)
diff --git a/docs/examples/asiohiper.cpp b/docs/examples/asiohiper.cpp
index 1ea3502..44836fd 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,
-- 
1.7.10.4
--------------020500080705010307070204
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
--------------020500080705010307070204--
Received on 2001-09-17