On 11/21/2012 11:55 PM, Christian Hägele wrote:

However, I have 2 additional comments (I haven't tested the code):

1. In the CURLOPT_OPENSOCKETFUNCTION-callback you call connect on the new 
socket.
That call is *blocking*! You should not do this when using boost::asio. There is
no need to call connect on the socket yourself. The curl-multi-handle will do
that for you in non-blocking manner and just give you the socket to listen on.

Thank you for your suggestion. Though I was aware that this connect was blocking, I thought, I had no other option. I have removed this connect.

2. In the CURLOPT_CLOSESOCKETFUNCTION you have to be aware that the
CURLMOPT_SOCKETFUNCTION might be called afterwards with the *same* socket you
just closed with action CURL_POLL_REMOVE. In my implementation that had some
weird issues and was very hard to find.
In your implementation this should not have any issues, but you should add a
comment about it somewhere in you blog-post or in your code. I wrote about that
issue a couple of time on the mailing-list.

I have added this point as a note in my blog post.


I have updated the sample program to avoid connect. As a result, sockopt callback has been removed and the program became smaller as well. Please find the patch attached.

-lijo
>From f4839103f4b1c43272b8893bc5a4a10b36cf1ee6 Mon Sep 17 00:00:00 2001
From: Lijo Antony <[email protected]>
Date: Sun, 25 Nov 2012 10:00:58 +0400
Subject: [PATCH] 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.
---
 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

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to