1515
1616
1717static int
18- connect_remote (const std::string &path, int timeout)
18+ connect_remote (const std::string &path, float timeout)
1919{
2020 // TCP Socket. Two syntaxes:
2121 // - tcp://host:port -> connect to "host" on "port" and read
@@ -25,13 +25,11 @@ connect_remote(const std::string &path, int timeout)
2525
2626 std::string host = path.substr (path.find (" ://" ) + 3 );
2727 if (host.find (" :" ) == host.npos )
28- log_fatal (" Could not open URL %s: unspecified port" ,
29- path.c_str ());
28+ log_fatal (" Could not open URL %s: unspecified port" , path.c_str ());
3029 std::string port = host.substr (host.find (" :" ) + 1 );
3130 host = host.substr (0 , host.find (" :" ));
3231
33- log_debug (" Opening connection to %s, port %s" , host.c_str (),
34- port.c_str ());
32+ log_debug (" Opening connection to %s, port %s" , host.c_str (), port.c_str ());
3533
3634 int fd = -1 ;
3735
@@ -66,56 +64,58 @@ connect_remote(const std::string &path, int timeout)
6664 fd = accept (lfd, NULL , NULL );
6765 log_debug (" Accepted connection on port %s" , port.c_str ());
6866 close (lfd);
69- } else {
70- // Connect to a listening host elsewhere
71-
72- struct addrinfo hints, *info, *r;
73- int err;
74-
75- bzero (&hints, sizeof (hints));
76- hints.ai_family = AF_UNSPEC;
77- hints.ai_socktype = SOCK_STREAM;
78-
79- err = getaddrinfo (host.c_str (), port.c_str (), &hints, &info);
80- if (err != 0 )
81- log_fatal (" Could not find host %s (%s)" ,
82- host.c_str (), gai_strerror (err));
83-
84- // Loop through possible addresses until we find one
85- // that works.
86- fd = -1 ;
87- for (r = info; r != NULL ; r = r->ai_next ) {
88- fd = socket (r->ai_family , r->ai_socktype , r->ai_protocol );
89- if (fd == -1 )
90- continue ;
91-
92- if (connect (fd, r->ai_addr , r->ai_addrlen ) == -1 ) {
93- close (fd);
94- fd = -1 ;
95- continue ;
96- }
9767
98- break ;
99- }
68+ return fd;
69+ }
70+
71+ // Connect to a listening host elsewhere
72+
73+ struct addrinfo hints, *info, *r;
74+ int err;
10075
76+ bzero (&hints, sizeof (hints));
77+ hints.ai_family = AF_UNSPEC;
78+ hints.ai_socktype = SOCK_STREAM;
79+
80+ err = getaddrinfo (host.c_str (), port.c_str (), &hints, &info);
81+ if (err != 0 )
82+ log_fatal (" Could not find host %s (%s)" ,
83+ host.c_str (), gai_strerror (err));
84+
85+ // Loop through possible addresses until we find one
86+ // that works.
87+ fd = -1 ;
88+ for (r = info; r != NULL ; r = r->ai_next ) {
89+ fd = socket (r->ai_family , r->ai_socktype , r->ai_protocol );
10190 if (fd == -1 )
102- log_fatal (" Could not connect to %s (%s)" ,
103- path.c_str (), strerror (errno));
91+ continue ;
10492
105- if (timeout >= 0 ) {
106- struct timeval tv;
107- tv.tv_sec = (int )timeout;
108- tv.tv_usec = (int )(1e6 * (timeout - tv.tv_sec ));
109- if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO,
110- (char *)&tv, sizeof (tv)) < 0 )
111- log_fatal (" Failed to set timeout on socket; errno=%i" ,
112- errno);
93+ if (connect (fd, r->ai_addr , r->ai_addrlen ) == -1 ) {
94+ close (fd);
95+ fd = -1 ;
96+ continue ;
11397 }
11498
115- if (info != NULL )
116- freeaddrinfo (info);
99+ break ;
117100 }
118101
102+ if (fd == -1 )
103+ log_fatal (" Could not connect to %s (%s)" ,
104+ path.c_str (), strerror (errno));
105+
106+ if (timeout >= 0 ) {
107+ struct timeval tv;
108+ tv.tv_sec = (int )timeout;
109+ tv.tv_usec = (int )(1e6 * (timeout - tv.tv_sec ));
110+ if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO,
111+ (char *)&tv, sizeof (tv)) < 0 )
112+ log_fatal (" Failed to set timeout on socket; errno=%i" ,
113+ errno);
114+ }
115+
116+ if (info != NULL )
117+ freeaddrinfo (info);
118+
119119 return fd;
120120}
121121
0 commit comments