瀏覽代碼

implement timeouts via non-blocking connects

David Rose 17 年之前
父節點
當前提交
00a3bd8462
共有 1 個文件被更改,包括 10 次插入8 次删除
  1. 10 8
      panda/src/net/connectionManager.cxx

+ 10 - 8
panda/src/net/connectionManager.cxx

@@ -147,8 +147,8 @@ PT(Connection) ConnectionManager::
 open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
 open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
   Socket_TCP *socket = new Socket_TCP;
   Socket_TCP *socket = new Socket_TCP;
 
 
-#if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
-  // In the simple-threads case, use a non-blocking connect.
+  // We always open the connection with non-blocking mode first, so we
+  // can implement the timeout.
   bool okflag = socket->ActiveOpenNonBlocking(address.get_addr());
   bool okflag = socket->ActiveOpenNonBlocking(address.get_addr());
   if (okflag && socket->GetLastError() == LOCAL_CONNECT_BLOCKING) {
   if (okflag && socket->GetLastError() == LOCAL_CONNECT_BLOCKING) {
     // Now wait for the socket to connect.
     // Now wait for the socket to connect.
@@ -171,12 +171,6 @@ open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
     }
     }
   }
   }
 
 
-#else
-  // In the normal case, we can just do a blocking connect.
-  bool okflag = socket->ActiveOpen(address.get_addr(), false);
-
-#endif  // SIMPLE_THREADS
-
   if (!okflag) {
   if (!okflag) {
     net_cat.error()
     net_cat.error()
       << "Unable to open TCP connection to server "
       << "Unable to open TCP connection to server "
@@ -185,6 +179,14 @@ open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
     return PT(Connection)();
     return PT(Connection)();
   }
   }
 
 
+#if !defined(HAVE_THREADS) || !defined(SIMPLE_THREADS)
+  // Now we have opened the socket in nonblocking mode.  Unless we're
+  // using SIMPLE_THREADS, though, we really want the socket in
+  // blocking mode (since that's what we support here).  Change it.
+  socket->SetBlocking();
+
+#endif  // SIMPLE_THREADS
+
   net_cat.info()
   net_cat.info()
     << "Opened TCP connection to server " << address.get_ip_string() << " "
     << "Opened TCP connection to server " << address.get_ip_string() << " "
     << " on port " << address.get_port() << "\n";
     << " on port " << address.get_port() << "\n";