[Cluster-devel] conga/ricci common/ClientSocket.cpp common/Soc ...

rmccabe at sourceware.org rmccabe at sourceware.org
Wed Mar 21 22:51:23 UTC 2007


CVSROOT:	/cvs/cluster
Module name:	conga
Changes by:	rmccabe at sourceware.org	2007-03-21 22:51:21

Modified files:
	ricci/common   : ClientSocket.cpp Socket.cpp 
	ricci/ricci    : Ricci.cpp 

Log message:
	- Cleanup for the socket hang patch

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/ClientSocket.cpp.diff?cvsroot=cluster&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Socket.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/Ricci.cpp.diff?cvsroot=cluster&r1=1.23&r2=1.24

--- conga/ricci/common/ClientSocket.cpp	2007/03/21 20:12:58	1.5
+++ conga/ricci/common/ClientSocket.cpp	2007/03/21 22:51:20	1.6
@@ -23,6 +23,7 @@
 
 #include "Socket.h"
 #include "Logger.h"
+#include "Time.h"
 
 #include <unistd.h>
 #include <errno.h>
@@ -73,11 +74,11 @@
 {
   _sock = socket(PF_INET, SOCK_STREAM, 0);
   if (_sock == -1)
-    throw String("ClientSocket(hostname, port): socket() failed");
+    throw String("ClientSocket(hostname, port, timeout): socket() failed");
   
   nonblocking(true);
 
-  char hostbuf[2048];
+  char hostbuf[4096];
   struct hostent hostent_result;
   struct hostent *ent = NULL;
   int ret = 0;
@@ -85,7 +86,7 @@
   if (gethostbyname2_r(hostname.c_str(), AF_INET, &hostent_result,
                        hostbuf, sizeof(hostbuf),
                        &ent, &ret) || !ent)
-    throw String("ClientSocket(hostname, port): gethostbyname() failed");
+    throw String("ClientSocket(hostname, port, timeout): gethostbyname() failed");
   
   char** addrs = ent->h_addr_list;
   for (int i=0; addrs[i]; i++) {
@@ -96,15 +97,29 @@
 
     ret = connect(_sock, (struct sockaddr*) &addr_in, sizeof(addr_in));
     if (ret != -1 || errno != EINPROGRESS)
-      throw String("ClientSocket(hostname, port): connect() failed");
-    bool can_read, can_write;
-    poll(can_read, can_write, timeout_ms);
-    if (can_write && !can_read) {
-      _addr = addr_in.sin_addr.s_addr;
-      return;
-    }
+      continue;
+
+    unsigned int end = time_mil() + timeout_ms;
+    do {
+      bool can_read = false;
+      bool can_write = true;
+      poll(can_read, can_write, timeout_ms);
+      if (can_write && can_read) {
+        /* an error occurred */
+        throw String("ClientSocket(hostname, port, timeout): connect() failed");
+      }
+
+      if (can_write && !can_read) {
+        _addr = addr_in.sin_addr.s_addr;
+        return;
+      }
+
+    } while (time_mil() < end);
+
+    throw String("ClientSocket(hostname, port, timeout): connect() timed out");
   }
-  throw String("ClientSocket(hostname, port): connect() failed");
+
+  throw String("ClientSocket(hostname, port, timeout): connect() failed");
 }
 
 ClientSocket::ClientSocket(const String& hostname, unsigned short port) :
@@ -114,17 +129,14 @@
   if (_sock == -1)
     throw String("ClientSocket(hostname, port): socket() failed");
   
-  char hostbuf[2048];
+  char hostbuf[4096];
   struct hostent hostent_result;
   struct hostent *ent = NULL;
   int ret = 0;
 
   if (gethostbyname2_r(hostname.c_str(), AF_INET, &hostent_result,
                        hostbuf, sizeof(hostbuf),
-                       &ent, &ret))
-    throw String("ClientSocket(hostname, port): gethostbyname() failed");
-  
-  if (!ent)
+                       &ent, &ret) || !ent)
     throw String("ClientSocket(hostname, port): gethostbyname() failed");
   
   char** addrs = ent->h_addr_list;
@@ -168,17 +180,14 @@
 bool 
 ClientSocket::connected_to(const String& hostname)
 {
-  char hostbuf[2048];
+  char hostbuf[4096];
   struct hostent hostent_result;
   struct hostent *ent = NULL;
   int ret = 0;
 
   if (gethostbyname2_r(hostname.c_str(), AF_INET, &hostent_result,
                        hostbuf, sizeof(hostbuf),
-                       &ent, &ret))
-    return false;
-
-  if (!ent)
+                       &ent, &ret) || !ent)
     return false;
   
   char** addrs = ent->h_addr_list;
--- conga/ricci/common/Socket.cpp	2007/03/21 20:12:58	1.4
+++ conga/ricci/common/Socket.cpp	2007/03/21 22:51:20	1.5
@@ -189,18 +189,16 @@
 {
   char buff[INET_ADDRSTRLEN+1];
   std::vector<String> addrs;
-  char hostbuf[2048];
+  char hostbuf[4096];
   struct hostent hostent_result;
   struct hostent *ent;
   int ret;
 
   if (gethostbyname2_r(hostname.c_str(), AF_INET, &hostent_result,
                        hostbuf, sizeof(hostbuf),
-                       &ent, &ret))
+                       &ent, &ret) || !ent)
     return addrs;
 
-  if (!ent)
-    return addrs;
   char** addrs_b = ent->h_addr_list;
   for (int i=0; addrs_b[i]; i++) {
     struct in_addr addr;
--- conga/ricci/ricci/Ricci.cpp	2007/03/21 20:12:58	1.23
+++ conga/ricci/ricci/Ricci.cpp	2007/03/21 22:51:21	1.24
@@ -467,7 +467,7 @@
   if (gethostname(name, sizeof(name)))
     return "";
 
-  char hostbuf[2048];
+  char hostbuf[4096];
   struct hostent hostent_result;
   struct hostent *ent = NULL;
   int ret = 0;




More information about the Cluster-devel mailing list