From b604df5f671b5e7abfd37570d76927a6ebd45f98 Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Mon, 30 Oct 2006 15:51:25 +0000 Subject: [PATCH] libt2n: (gerd) add connect timeout --- src/socket_client.cpp | 133 ++++++++++++++++++++++++++++++++++++------------- src/socket_client.hxx | 16 +++++- test/comm.cpp | 4 +- test/hello.cpp | 59 +++++++++++++++++++++- 4 files changed, 170 insertions(+), 42 deletions(-) diff --git a/src/socket_client.cpp b/src/socket_client.cpp index 067b033..100ca54 100644 --- a/src/socket_client.cpp +++ b/src/socket_client.cpp @@ -43,74 +43,137 @@ using namespace std; namespace libt2n { -socket_client_connection::socket_client_connection(const std::string& _server, int _port, int _max_retries) +socket_client_connection::socket_client_connection(int _port, const std::string& _server, + long long _connect_timeout_usec, int _max_retries) : client_connection(), socket_handler(0,tcp_s) { max_retries=_max_retries; + connect_timeout_usec=_connect_timeout_usec; server=_server; port=_port; - connect(); + tcp_connect(max_retries); } -socket_client_connection::socket_client_connection(const std::string& _path, int _max_retries) +socket_client_connection::socket_client_connection(const std::string& _path, + long long _connect_timeout_usec, int _max_retries) : client_connection(), socket_handler(0,unix_s) { max_retries=_max_retries; + connect_timeout_usec=_connect_timeout_usec; path=_path; - connect(); + unix_connect(max_retries); } -void socket_client_connection::connect() +void socket_client_connection::tcp_connect(int max_retries) { - if (get_type() == unix_s) + struct sockaddr_in sock_addr; + + sock_addr.sin_family = AF_INET; + sock_addr.sin_port = htons(port); + + // find the target ip + if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0) { - struct sockaddr_un unix_addr; + struct hostent *server_hent; + server_hent=gethostbyname(server.c_str()); + if (server_hent == NULL) + throw t2n_connect_error(string("can't find server ")+server); - unix_addr.sun_family = AF_UNIX; - strcpy (unix_addr.sun_path, path.c_str()); + memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr)); + } - sock = socket(PF_UNIX, SOCK_STREAM, 0); - if (!sock) - throw t2n_connect_error(string("socket() error: ")+strerror(errno)); + sock = socket(PF_INET, SOCK_STREAM, 0); + if (!sock) + throw t2n_connect_error(string("socket() error: ")+strerror(errno)); - if (::connect(sock,(struct sockaddr *) &unix_addr, sizeof(unix_addr))) - throw t2n_connect_error(string("connect() error: ")+strerror(errno)); + try + { + connect_with_timeout((struct sockaddr *) &sock_addr,sizeof(sock_addr)); } - else if (get_type() == tcp_s) + catch (t2n_connect_error &e) { - struct sockaddr_in sock_addr; + // recurse if retries left + if (max_retries > 0) + tcp_connect(max_retries-1); + } - sock_addr.sin_family = AF_INET; - sock_addr.sin_port = htons(port); + do_callbacks(new_connection); +} - // find the target ip - if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0) - { - struct hostent *server_hent; - server_hent=gethostbyname(server.c_str()); - if (server_hent == NULL) - throw t2n_connect_error(string("can't find server ")+server); +void socket_client_connection::unix_connect(int max_retries) +{ + struct sockaddr_un unix_addr; - memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr)); - } + unix_addr.sun_family = AF_UNIX; + strcpy (unix_addr.sun_path, path.c_str()); - sock = socket(PF_INET, SOCK_STREAM, 0); - if (!sock) - throw t2n_connect_error(string("socket() error: ")+strerror(errno)); + sock = socket(PF_UNIX, SOCK_STREAM, 0); + if (!sock) + throw t2n_connect_error(string("socket() error: ")+strerror(errno)); - if (::connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr))) - throw t2n_connect_error(string("connect() error: ")+strerror(errno)); + try + { + connect_with_timeout((struct sockaddr *) &unix_addr, sizeof(unix_addr)); + } + catch (t2n_connect_error &e) + { + // recurse if retries left + if (max_retries > 0) + tcp_connect(max_retries-1); } - else - throw t2n_connect_error(string("invalid connection type")); + do_callbacks(new_connection); +} + +void socket_client_connection::connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size) +{ set_socket_options(sock); - do_callbacks(new_connection); + int ret=::connect(sock,sock_addr, sockaddr_size); + + if (ret < 0) + { + if (errno==EINPROGRESS) + { + /* set timeout */ + struct timeval tval; + struct timeval *timeout_ptr; + + if (connect_timeout_usec == -1) + timeout_ptr = NULL; + else + { + timeout_ptr = &tval; + + // convert timeout from long long usec to int sec + int usec + tval.tv_sec = connect_timeout_usec / 1000000; + tval.tv_usec = connect_timeout_usec % 1000000; + } + + fd_set connect_socket_set; + FD_ZERO(&connect_socket_set); + FD_SET(sock,&connect_socket_set); + + int ret; + while ((ret=select(FD_SETSIZE, NULL, &connect_socket_set, NULL, timeout_ptr)) && + ret < 0 && errno==EINTR); + + if (ret < 0) + throw t2n_connect_error(string("connect() error (select): ")+strerror(errno)); + + socklen_t sopt=sizeof(int); + int valopt; + ret=getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &sopt); + if (ret < 0 || valopt) + throw t2n_connect_error(string("connect() error (getsockopt): ")+strerror(errno)); + } + else + throw t2n_connect_error(string("connect() error: ")+strerror(errno)); + } } void socket_client_connection::close() diff --git a/src/socket_client.hxx b/src/socket_client.hxx index 1d82400..7160e0d 100644 --- a/src/socket_client.hxx +++ b/src/socket_client.hxx @@ -22,6 +22,8 @@ #include "client.hxx" #include "socket_handler.hxx" +struct sockaddr; + namespace libt2n { /** @brief a connection from client to server using sockets. @@ -32,14 +34,18 @@ class socket_client_connection : public client_connection, public socket_handler { public: static const int max_retries_default=3; + static const long long connect_timeout_usec_default=30000000; private: void real_write(const std::string& data) { socket_write(data); } - void connect(); + void tcp_connect(int max_retries); + void unix_connect(int max_retries); + void connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size); int max_retries; + long long connect_timeout_usec; std::string path; std::string server; @@ -49,8 +55,12 @@ class socket_client_connection : public client_connection, public socket_handler { return client_connection::get_logstream(level); } public: - socket_client_connection(const std::string& _server, int _port, int _max_retries=max_retries_default); - socket_client_connection(const std::string& _path, int _max_retries=max_retries_default); + socket_client_connection(int _port, const std::string& _server="127.0.0.1", + long long _connect_timeout_usec=connect_timeout_usec_default, + int _max_retries=max_retries_default); + socket_client_connection(const std::string& _path, + long long _connect_timeout_usec=connect_timeout_usec_default, + int _max_retries=max_retries_default); /** @brief read data from the socket and copy it into buffer @param usec_timeout wait until new data is found, max timeout usecs. diff --git a/test/comm.cpp b/test/comm.cpp index 6044693..eb4de8c 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -173,7 +173,7 @@ class test_comm : public TestFixture { // wait till server is up sleep(1); - socket_client_connection sc("localhost",6666,socket_client_connection::max_retries_default); + socket_client_connection sc(6666); sc.write("hello"); // don't call atexit and stuff _exit(0); @@ -246,7 +246,7 @@ class test_comm : public TestFixture // wait till server is up sleep(1); - socket_client_connection sc("localhost",6666,socket_client_connection::max_retries_default); + socket_client_connection sc(6666); sc.write("ABC"); sc.fill_buffer(1000000); diff --git a/test/hello.cpp b/test/hello.cpp index a0cc532..80bfec6 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -51,7 +51,8 @@ class test_hello : public TestFixture CPPUNIT_TEST(BadVersion); CPPUNIT_TEST(SeparatorMissing); CPPUNIT_TEST(WrongByteOrder); - CPPUNIT_TEST(OtherServer); + CPPUNIT_TEST(OtherServerBig); + CPPUNIT_TEST(OtherServerSmall); CPPUNIT_TEST_SUITE_END(); @@ -353,7 +354,7 @@ class test_hello : public TestFixture } } - void OtherServer() + void OtherServerBig() { pid_t pid; @@ -407,6 +408,60 @@ class test_hello : public TestFixture } } + void OtherServerSmall() + { + pid_t pid; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + + ostringstream hello; + // hmm, we got the wrong socket + hello << "READY"; + + ss.add_callback(new_connection,bind(&test_hello::send_raw_socket, boost::ref(*this), hello.str(),&ss, _1)); + + // max 3 sec + for (int i=0; i < 3; i++) + ss.fill_buffer(1000000); + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + string errormsg; + + try + { + command_client cc(sc); + } + catch(t2n_version_mismatch &e) + { errormsg=e.what(); } + catch(...) + { throw; } + + CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg); + } + } + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(test_hello); -- 1.7.1