]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_netChannel.cxx
hla: Use HLADataElementIndices for HLAInteractionClass.
[simgear.git] / simgear / io / sg_netChannel.cxx
index d34a521bd465c4e6792ab56af0f097d84d33c092..b903a98082f4c31d6f853c168140c1d98f985dfa 100644 (file)
@@ -16,7 +16,7 @@
  
      You should have received a copy of the GNU Library General Public
      License along with this library; if not, write to the Free Software
-     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
  
      For further information visit http://plib.sourceforge.net
 
 #include <memory>
 #include <cassert>
 #include <cstring>
+#include <errno.h>
 
 #include <simgear/debug/logstream.hxx>
-#include <simgear/io/HostLookup.hxx>
+
 
 namespace simgear  {
 
@@ -46,6 +47,7 @@ NetChannel::NetChannel ()
 {
   closed = true ;
   connected = false ;
+  resolving_host  = false;
   accepting = false ;
   write_blocked = false ;
   should_delete = false ;
@@ -83,7 +85,6 @@ NetChannel::setHandle (int handle, bool is_connected)
   close () ;
   Socket::setHandle ( handle ) ;
   connected = is_connected ;
-  //if ( connected ) this->handleConnect();
   closed = false ;
 }
 
@@ -107,11 +108,12 @@ NetChannel::listen ( int backlog )
 }
 
 int
-NetChannel::connect ( const char* host, int p )
+NetChannel::connect ( const char* h, int p )
 {
-  host_lookup = HostLookup::lookup(host);
+  host = h;
   port = p;
-  return 0;
+  resolving_host = true;
+  return handleResolve();
 }
 
 int
@@ -179,12 +181,10 @@ NetChannel::handleReadEvent (void)
   if (accepting) {
     if (!connected) {
       connected = true ;
-      //this->handleConnect();
     }
     this->handleAccept();
   } else if (!connected) {
     connected = true ;
-    //this->handleConnect();
     this->handleRead();
   } else {
     this->handleRead();
@@ -196,28 +196,39 @@ NetChannel::handleWriteEvent (void)
 {
   if (!connected) {
     connected = true ;
-    //this->handleConnect();
   }
   write_blocked = false ;
   this->handleWrite();
 }
 
-void
-NetChannel::doConnect()
-{    
-    IPAddress addr( host_lookup->address() );
+int
+NetChannel::handleResolve()
+{
+    IPAddress addr;
+    if (!IPAddress::lookupNonblocking(host.c_str(), addr)) {
+        return 0; // not looked up yet, wait longer
+    }
+    
+    if (!addr.isValid()) {
+        SG_LOG(SG_IO, SG_WARN, "Network: host lookup failed:" << host);
+        handleError (0);
+        close();
+        return -1;
+    }
+    
+    resolving_host = false;
     addr.setPort(port);
-    int result = Socket::connect ( addr ) ;
-    host_lookup = NULL;
-
+    int result = Socket::connect ( &addr ) ;
     if (result == 0) {
         connected = true ;
+        return 0;
     } else if (isNonBlockingError ()) {
-
+        return 0;
     } else {
-    // some other error condition
+        // some other error condition
         handleError (result);
         close();
+        return -1;
     }
 }
 
@@ -245,17 +256,13 @@ NetChannel::poll (unsigned int timeout)
     }
     else if ( ! ch -> closed )
     {
-      nopen++ ;
-      if (ch->host_lookup) {
-          if (ch->host_lookup->resolved()) {
-              ch->doConnect();
-          } else if (ch->host_lookup->failed()) {
-              ch->handleError (-1);
-              ch->close();
-          }
+      if (ch -> resolving_host )
+      {
+          ch -> handleResolve();
           continue;
       }
       
+      nopen++ ;
       if (ch -> readable()) {
         assert(nreads<MAX_SOCKETS);
         reads[nreads++] = ch ;