X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChannel.cxx;h=817650ac482b04c5da9b835d3809e15530f65e8d;hb=adb7db9229db1d869b254ac18f1471bed464c508;hp=d34a521bd465c4e6792ab56af0f097d84d33c092;hpb=878b504f8e044bc0e59903caa8641492421b76d8;p=simgear.git diff --git a/simgear/io/sg_netChannel.cxx b/simgear/io/sg_netChannel.cxx index d34a521b..817650ac 100644 --- a/simgear/io/sg_netChannel.cxx +++ b/simgear/io/sg_netChannel.cxx @@ -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 @@ -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,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 (ENOENT); + 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