bool IBConnection::connect() { LBASSERT( _description->type == CONNECTIONTYPE_IB ); if( _state != STATE_CLOSED ) return false; _state = STATE_CONNECTING; _fireStateChanged(); _socketConnection = new SocketConnection(); ConnectionDescriptionPtr description = new ConnectionDescription( *_description ); description->type = CONNECTIONTYPE_TCPIP; _socketConnection->setDescription( description ); // 1 : TCPIP Connection if ( !_socketConnection->connect() || !_preRegister() || !_establish( false )) { _socketConnection->close(); _socketConnection = 0; return false; } LBINFO << "Connected " << _description << std::endl; return true; }
void PGMConnection::close() { if( _state == STATE_CLOSED ) return; _printReadStatistics(); _printSendStatistics(); if( isListening( )) _exitAIOAccept(); else if( isConnected( )) _exitAIORead(); _state = STATE_CLOSED; EQASSERT( _readFD > 0 ); if( _readFD > 0 ) { const std::string& iName = _description->getInterface(); if( !iName.empty( )) { unsigned long interface; if( !_parseHostname( iName, interface ) || ::setsockopt( _readFD, IPPROTO_RM, RM_DEL_RECEIVE_IF, (char*)&interface, sizeof(uint32_t)) == SOCKET_ERROR ) { EQWARN << "can't delete recv interface " << base::sysError << std::endl; } } #ifdef _WIN32 const bool closed = ( ::closesocket( _readFD ) == 0 ); #else const bool closed = ( ::close( _readFD ) == 0 ); #endif if( !closed ) EQWARN << "Could not close read socket: " << base::sysError << std::endl; } if( _writeFD > 0 && isListening( )) { #ifdef _WIN32 const bool closed = ( ::closesocket( _writeFD ) == 0 ); #else const bool closed = ( ::close( _writeFD ) == 0 ); #endif if( !closed ) EQWARN << "Could not close write socket: " << base::sysError << std::endl; } _readFD = INVALID_SOCKET; _writeFD = INVALID_SOCKET; _fireStateChanged(); }
void MCIPConnection::notifyStateChanged( Connection* connection ) { EQASSERT( _impl == connection ); if( _state == connection->getState( )) return; _state = connection->getState(); _fireStateChanged(); }
bool IBConnection::_establish( bool isServer ) { for ( int i = 0; i < EQ_NUMCONN_IB; i++ ) if ( !_establish( isServer, i )) { _socketConnection->close(); _socketConnection = 0; return false; } _socketConnection->close(); _socketConnection = 0; _state = STATE_CONNECTED; _fireStateChanged(); return true; }
void IBConnection::_close() { if( !(_state == STATE_CONNECTED || _state == STATE_LISTENING )) return; if( _socketConnection.isValid( )) _socketConnection->close(); _socketConnection = 0; for ( int i = 0; i < EQ_NUMCONN_IB; i++ ) { _interfaces[i]->close(); _completionQueues[i]->close(); } _adapter.close(); _state = STATE_CLOSED; _fireStateChanged(); }
bool IBConnection::listen() { LBASSERT( _description->type == CONNECTIONTYPE_IB ); _socketConnection = new SocketConnection(); ConnectionDescription* description = new ConnectionDescription( *_description ); description->type = CONNECTIONTYPE_TCPIP; _socketConnection->setDescription( description ); if( !_socketConnection->listen()) { close(); return false; } _state = _socketConnection->getState(); _fireStateChanged(); return ( _state == STATE_LISTENING ); }
//---------------------------------------------------------------------- // listen //---------------------------------------------------------------------- bool PGMConnection::listen() { EQASSERT( _description->type == CONNECTIONTYPE_PGM ); if( _state != STATE_CLOSED ) return false; _state = STATE_CONNECTING; _fireStateChanged(); sockaddr_in address; const size_t size = sizeof( sockaddr_in ); if( !_parseAddress( address )) { EQWARN << "Can't parse connection parameters" << std::endl; return false; } //-- create a 'listening' read socket _readFD = _initSocket( address ); if( _readFD == INVALID_SOCKET || !_setupReadSocket( )) { close(); return false; } const bool listening = (::listen( _readFD, 10 ) == 0); if( !listening ) { EQWARN << "Could not listen on socket: " << base::sysError << std::endl; close(); return false; } // get listening socket parameters socklen_t used = size; getsockname( _readFD, (struct sockaddr *)&address, &used ); _description->port = ntohs( address.sin_port ); const std::string& hostname = _description->getHostname(); if( hostname.empty( )) { if( address.sin_addr.s_addr == INADDR_ANY ) { char cHostname[256]; gethostname( cHostname, 256 ); _description->setHostname( cHostname ); } else _description->setHostname( inet_ntoa( address.sin_addr )); } _initAIOAccept(); //-- create a connected 'write' socket address.sin_family = AF_INET; address.sin_addr.s_addr = htonl( INADDR_ANY ); address.sin_port = htons( 0 ); memset( &(address.sin_zero), 0, 8 ); // zero the rest _writeFD = _initSocket( address ); if( _writeFD == INVALID_SOCKET || !_setupSendSocket( )) { close(); return false; } _parseAddress( address ); #ifdef _WIN32 const bool connected = WSAConnect( _writeFD, (sockaddr*)&address, size, 0, 0, 0, 0 ) == 0; #else const bool connected = (::connect( _readFD, (sockaddr*)&address, size ) == 0); #endif if( !connected ) { EQWARN << "Could not connect to '" << _description->getHostname() << ":" << _description->port << "': " << base::sysError << std::endl; close(); return false; } _state = STATE_LISTENING; _fireStateChanged(); EQINFO << "Listening on " << _description->getHostname() << "[" << inet_ntoa( address.sin_addr ) << "]:" << _description->port << " (" << _description->toString() << ")" << std::endl; return true; }