Logo Search packages:      
Sourcecode: darkice version File versions

bool IceCast2::sendLogin ( void   )  throw ( Exception ) [protected, virtual]

Log in to the server using the socket avialable.

Returns:
true if login was successful, false otherwise.
Exceptions:
Exception 

Implements CastSink.

Definition at line 122 of file IceCast2.cpp.

References Util::base64Encode(), Source::canRead(), Sink::flush(), format, CastSink::getBitRate(), getDescription(), CastSink::getGenre(), CastSink::getIsPublic(), getMountPoint(), CastSink::getName(), CastSink::getPassword(), CastSink::getSink(), CastSink::getSocket(), CastSink::getUrl(), Sink::isOpen(), Source::isOpen(), Source::read(), Util::strCat(), Util::strCpy(), Util::strEq(), Util::strLen(), and Sink::write().

{
    Sink          * sink   = getSink();
    Source        * source = getSocket();
    const char    * str;
    char            resp[STRBUF_SIZE];
    unsigned int    len;
    unsigned int    lenExpected;

    if ( !source->isOpen() ) {
        return false;
    }
    if ( !sink->isOpen() ) {
        return false;
    }

    // send the request, a string like:
    // "SOURCE <mountpoint> ICE/1.0"
    str = "SOURCE /";
    sink->write( str, strlen( str));
    str = getMountPoint();
    sink->write( str, strlen( str));
    str = " HTTP/1.0";
    sink->write( str, strlen( str));

    // send the content type, Ogg Vorbis
    str = "\nContent-type: ";
    sink->write( str, strlen( str));
    switch ( format ) {
        case mp3:
            str = "audio/mpeg";
            break;

        case oggVorbis:
            str = "application/x-ogg";
            break;

        default:
            throw Exception( __FILE__, __LINE__,
                             "unsupported stream format", format);
            break;
    }
    sink->write( str, strlen( str));

    // send the authentication info
    str = "\nAuthorization: Basic ";
    sink->write( str, strlen(str));
    {
        // send source:<password> encoded as base64
        char        * source = "source:";
        const char  * pwd    = getPassword();
        char        * tmp    = new char[Util::strLen(source) +
                                        Util::strLen(pwd) + 1];
        Util::strCpy( tmp, source);
        Util::strCat( tmp, pwd);
        char  * base64 = Util::base64Encode( tmp);
        delete[] tmp;
        sink->write( base64, strlen(base64));
        delete[] base64;
    }

    // send user agent info
    str = "\nUser-Agent: DarkIce/" VERSION " (http://darkice.sourceforge.net/)";
    sink->write( str, strlen( str));

    // send the ice- headers
    str = "\nice-bitrate: ";
    sink->write( str, strlen( str));
    if ( log10(getBitRate()) >= (STRBUF_SIZE-2) ) {
        throw Exception( __FILE__, __LINE__,
                         "bitrate does not fit string buffer", getBitRate());
    }
    sprintf( resp, "%d", getBitRate());
    sink->write( resp, strlen( resp));

    str = "\nice-public: ";
    sink->write( str, strlen( str));
    str = getIsPublic() ? "1" : "0";
    sink->write( str, strlen( str));

    if ( getName() ) {
        str = "\nice-name: ";
        sink->write( str, strlen( str));
        str = getName();
        sink->write( str, strlen( str));
    }

    if ( getDescription() ) {
        str = "\nice-description: ";
        sink->write( str, strlen( str));
        str = getDescription();
        sink->write( str, strlen( str));
    }

    if ( getUrl() ) {
        str = "\nice-url: ";
        sink->write( str, strlen( str));
        str = getUrl();
        sink->write( str, strlen( str));
    }

    if ( getGenre() ) {
        str = "\nice-genre: ";
        sink->write( str, strlen( str));
        str = getGenre();
        sink->write( str, strlen( str));
    }

    str = "\n\n";
    sink->write( str, strlen( str));
    sink->flush();

    // read the response, expected response begins with responseOK
    lenExpected = Util::strLen( responseOK);
    if ( (len = source->read( resp, STRBUF_SIZE-1)) < lenExpected ) {
        return false;
    }
    resp[lenExpected] = 0;
    if ( !Util::strEq( resp, responseOK) ) {
        return false;
    }
    
    // suck anything that the other side has to say
    while ( source->canRead( 0, 0) && 
           (len = source->read( resp, STRBUF_SIZE-1)) );

    return true;
}


Generated by  Doxygen 1.6.0   Back to index