//----------------------------------------------------------------------
// connect
//----------------------------------------------------------------------
bool NamedPipeConnection::connect()
{
    LBASSERT( getDescription()->type == CONNECTIONTYPE_NAMEDPIPE );

    if( !isClosed( ))
        return false;

    _setState( STATE_CONNECTING );

    const std::string filename = _getFilename();
    if ( !WaitNamedPipe( filename.c_str(), 20000 ))
    {
        LBERROR << "Can't create named pipe: " << lunchbox::sysError << std::endl;
        return false;
    }

    if( !_connectNamedPipe( ))
        return false;

    _initAIORead();
    _setState( STATE_CONNECTED );

    LBINFO << "Connected " << getDescription()->toString() << std::endl;
    return true;
}
Example #2
0
void SocketConnection::_initAIOAccept()
{
    _initAIORead();
    _overlappedAcceptData = malloc( 2*( sizeof( sockaddr_in ) + 16 ));
}
void NamedPipeConnection::_initAIOAccept()
{
    _initAIORead();
}
Example #4
0
//----------------------------------------------------------------------
// connect
//----------------------------------------------------------------------
bool SocketConnection::connect()
{
    ConnectionDescriptionPtr description = _getDescription();
    LBASSERT( description->type == CONNECTIONTYPE_TCPIP );
    if( !isClosed( ))
        return false;

    if( description->port == 0 )
        return false;

    if( description->getHostname().empty( ))
        description->setHostname( "127.0.0.1" );

    sockaddr_in address;
    if( !_parseAddress( description, address ))
    {
        LBWARN << "Can't parse connection parameters" << std::endl;
        return false;
    }

    _setState( STATE_CONNECTING );

    if( !_createSocket( ))
        return false;

    if( address.sin_addr.s_addr == 0 )
    {
        LBWARN << "Refuse to connect to 0.0.0.0" << std::endl;
        close();
        return false;
    }

#ifdef _WIN32
    const bool connected = WSAConnect( _readFD, (sockaddr*)&address,
                                       sizeof( address ), 0, 0, 0, 0 ) == 0;
#else
    int nTries = 10;
    while( nTries-- )
    {
        const bool connected = (::connect( _readFD, (sockaddr*)&address,
                                           sizeof( address )) == 0);
        if( connected )
            break;

        switch( errno )
        {
          case EINTR: // Happens sometimes, but looks harmless
              LBDEBUG << "connect: " << lunchbox::sysError << ", retrying"
                     << std::endl;
              lunchbox::sleep( 5 /*ms*/ );
              break;

          default:
              nTries = 0;
              break;
        }
    }
    const bool connected = nTries > 0;
#endif

    if( !connected )
    {
        LBDEBUG << "Could not connect to '" << description->getHostname() << ":"
                << description->port << "': " << lunchbox::sysError << std::endl;
        close();
        return false;
    }

    _initAIORead();
    _setState( STATE_CONNECTED );
    LBDEBUG << "Connected " << description->toString() << std::endl;
    return true;
}