rsked/mpdclient.cc
/// Attempt to connect if not connected; it will throw an
/// Mpd_connect_exception unless a connection can be opened after
/// MAX_TRIES attempts. If it returns the connection m_conn is valid.
///
/// * May throw.
///
Attempt to connect if not connected; it will throw an
Mpd_connect_exception unless a connection can be opened after
MAX_TRIES attempts. If it returns the connection m_conn is valid.

May throw.
Attempt to connect if not connected ; it will throw an Mpd_connect_exception unless a connection can be opened after MAX_TRIES attempts . If it returns the connection m_conn is valid . May throw .
void Mpd_client::connect()
{
    constexpr const unsigned MAX_TRIES=2;
    for (unsigned ntries=MAX_TRIES; ntries; ntries--) {
        if (not m_conn) {
            m_conn = mpd_connection_new( m_socket_path.c_str(), 
                                         m_port, m_timeout_ms );
            if (!m_conn) { 
                throw Mpd_connect_exception();
            }
        }
        auto err = mpd_connection_get_error( m_conn );
        if (err == MPD_ERROR_SUCCESS) {
            const unsigned *vnums =  mpd_connection_get_server_version(m_conn);
            if (vnums) {
                if (!m_server_vers[3]) {
                    m_server_vers[0] = vnums[0];
                    m_server_vers[1] = vnums[1];
                    m_server_vers[2] = vnums[2];
                    m_server_vers[3] = 1;
                    LOG_DEBUG(Lgr) << "Mpd_client: server version is " << vnums[0]
                                   << "." << vnums[1] << "." << vnums[2];
                }
            } else {
                LOG_DEBUG(Lgr) << "Mpd_client: server version is unknown";
            }
            return; 
        }
        enum mpd_server_error serr;
        diag_error("Mpd_client::connect: ",serr);
        if (ntries) sleep(1);  
    }
    disconnect(); 
    throw Mpd_connect_exception(); 
}
void Mpd_client :: connect ( ) { constexpr const unsigned MAX_TRIES = 2 ; for ( unsigned ntries = MAX_TRIES ; ntries ; ntries -- ) { if ( not m_conn ) { m_conn = mpd_connection_new ( m_socket_path . c_str ( ) , m_port , m_timeout_ms ) ; if ( ! m_conn ) { throw Mpd_connect_exception ( ) ; } } auto err = mpd_connection_get_error ( m_conn ) ; if ( err == MPD_ERROR_SUCCESS ) { const unsigned * vnums = mpd_connection_get_server_version ( m_conn ) ; if ( vnums ) { if ( ! m_server_vers [ 3 ] ) { m_server_vers [ 0 ] = vnums [ 0 ] ; m_server_vers [ 1 ] = vnums [ 1 ] ; m_server_vers [ 2 ] = vnums [ 2 ] ; m_server_vers [ 3 ] = 1 ; LOG_DEBUG ( Lgr ) << " " << vnums [ 0 ] << " " << vnums [ 1 ] << " " << vnums [ 2 ] ; } } else { LOG_DEBUG ( Lgr ) << " " ; } return ; } enum mpd_server_error serr ; diag_error ( " " , serr ) ; if ( ntries ) sleep ( 1 ) ; } disconnect ( ) ; throw Mpd_connect_exception ( ) ; }
Attempt to connect if not connected; it will throw an
Mpd_connect_exception unless a connection can be opened after
MAX_TRIES attempts.
Attempt to connect if not connected ; it will throw an Mpd_connect_exception unless a connection can be opened after MAX_TRIES attempts .
// get a connection object if we have none // this means: out of memory // check (and clear) errors on the connection, determine server version // I will print this only once // Success return // diagnose connection problem // will clear the error but may also set m_conn=nullptr; // give it a little time before retry // dispose of connection object // Failure--throw