X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChannel.cxx;h=86839e71a707384661f9a5555cdd1f7d38e3e0d2;hb=427d6c3316e59aa86bbf4d3e51f522e0aba9c4fd;hp=d34a521bd465c4e6792ab56af0f097d84d33c092;hpb=878b504f8e044bc0e59903caa8641492421b76d8;p=simgear.git diff --git a/simgear/io/sg_netChannel.cxx b/simgear/io/sg_netChannel.cxx index d34a521b..86839e71 100644 --- a/simgear/io/sg_netChannel.cxx +++ b/simgear/io/sg_netChannel.cxx @@ -34,9 +34,10 @@ #include #include #include +#include #include -#include + 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,32 @@ 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 + } + + 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 +249,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