X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChannel.cxx;h=86839e71a707384661f9a5555cdd1f7d38e3e0d2;hb=1cb9a79fd48ce6151f6a2cf8d07ef23e002a81b1;hp=c65025652b5181bab89456e882ac32b3bd49d6f8;hpb=1ae91097ba2859e70d29fea683982b847809e6cf;p=simgear.git diff --git a/simgear/io/sg_netChannel.cxx b/simgear/io/sg_netChannel.cxx index c6502565..86839e71 100644 --- a/simgear/io/sg_netChannel.cxx +++ b/simgear/io/sg_netChannel.cxx @@ -34,9 +34,11 @@ #include #include #include +#include #include + namespace simgear { static NetChannel* channels = 0 ; @@ -45,6 +47,7 @@ NetChannel::NetChannel () { closed = true ; connected = false ; + resolving_host = false; accepting = false ; write_blocked = false ; should_delete = false ; @@ -82,7 +85,6 @@ NetChannel::setHandle (int handle, bool is_connected) close () ; Socket::setHandle ( handle ) ; connected = is_connected ; - //if ( connected ) this->handleConnect(); closed = false ; } @@ -106,21 +108,12 @@ NetChannel::listen ( int backlog ) } int -NetChannel::connect ( const char* host, int port ) +NetChannel::connect ( const char* h, int p ) { - int result = Socket::connect ( host, port ) ; - if (result == 0) { - connected = true ; - //this->handleConnect(); - return 0; - } else if (isNonBlockingError ()) { - return 0; - } else { - // some other error condition - this->handleError (result); - close(); - return -1; - } + host = h; + port = p; + resolving_host = true; + return handleResolve(); } int @@ -188,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(); @@ -205,12 +196,35 @@ NetChannel::handleWriteEvent (void) { if (!connected) { connected = true ; - //this->handleConnect(); } write_blocked = false ; this->handleWrite(); } +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 ) ; + if (result == 0) { + connected = true ; + return 0; + } else if (isNonBlockingError ()) { + return 0; + } else { + // some other error condition + handleError (result); + close(); + return -1; + } +} + bool NetChannel::poll (unsigned int timeout) { @@ -235,6 +249,12 @@ NetChannel::poll (unsigned int timeout) } else if ( ! ch -> closed ) { + if (ch -> resolving_host ) + { + ch -> handleResolve(); + continue; + } + nopen++ ; if (ch -> readable()) { assert(nreads