Пример #1
0
static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
{
    stream_sys_t *p_sys = stream->p_sys;
    block_t *peeked = p_sys->peeked;
    size_t curlen = 0;
    int fd = p_sys->read_fd;

    if (peeked == NULL)
        peeked = block_Alloc (len);
    else if ((curlen = peeked->i_buffer) < len)
        peeked = block_Realloc (peeked, 0, len);

    if ((p_sys->peeked = peeked) == NULL)
        return 0;

    if (curlen < len)
    {
        ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
                                len - curlen, true);
        if (val >= 0)
        {
            curlen += val;
            peeked->i_buffer = curlen;
        }
    }
    *pbuf = peeked->p_buffer;
    return curlen;
}
Пример #2
0
static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

    return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
}
Пример #3
0
HtsMessage ReadMessageEx(vlc_object_t *obj, sys_common_t *sys)
{
	char *buf;
	uint32_t len;
	ssize_t readSize;

	if(sys->queue.size())
	{
		HtsMessage res = sys->queue.front();
		sys->queue.pop_front();
		return res;
	}

	if((readSize = net_Read(obj, sys->netfd, NULL, &len, sizeof(len), true)) != sizeof(len))
	{
		if(readSize == 0)
		{
			msg_Err(obj, "Size Read EOF!");
			return HtsMessage();
		}

		msg_Err(obj, "Error reading size: %m");
		return HtsMessage();
	}

	len = ntohl(len);
	if(len == 0)
		return HtsMessage();

	buf = (char*)malloc(len);

	if((readSize = net_Read(obj, sys->netfd, NULL, buf, len, true)) != len)
	{
		if(readSize == 0)
		{
			msg_Err(obj, "Data Read EOF!");
			return HtsMessage();
		}

		msg_Err(obj, "Error reading data: %m");
		return HtsMessage();
	}

	HtsMessage result = HtsMessage::Deserialize(len, buf);
	free(buf);
	return result;
}
Пример #4
0
static bool read_exact( filter_t *p_filter,
                        int i_socket,
                        char* p_readbuf,
                        int i_bytes )
{
    return i_bytes == net_Read( p_filter, i_socket, NULL,
                                  (unsigned char*)p_readbuf,
                                  i_bytes, true );
}
Пример #5
0
/**
 * Gets a framed RTP packet.
 * @param fd stream file descriptor
 * @return a block or NULL in case of fatal error
 */
static block_t *rtp_stream_recv (vlc_object_t *obj, int fd)
{
    ssize_t len = 0;
    uint8_t hdr[2]; /* frame header */

    /* Receives the RTP frame header */
    do
    {
        ssize_t val = net_Read (obj, fd, NULL, hdr + len, 2 - len, false);
        if (val <= 0)
            return NULL;
        len += val;
    }
    while (len < 2);

    block_t *block = block_Alloc (GetWBE (hdr));

    /* Receives the RTP packet */
    for (ssize_t i = 0; i < len;)
    {
        ssize_t val;

        block_cleanup_push (block);
        val = net_Read (obj, fd, NULL,
                        block->p_buffer + i, block->i_buffer - i, false);
        vlc_cleanup_pop ();

        if (val <= 0)
        {
            block_Release (block);
            return NULL;
        }
        i += val;
    }

    return block;
}
Пример #6
0
/**
 * Reads decompressed from the decompression program
 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
 */
static int Read (stream_t *stream, void *buf, unsigned int buflen)
{
    stream_sys_t *sys = stream->p_sys;
    unsigned ret = 0;

    if (buf == NULL) /* caller skips data, get big enough peek buffer */
        buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);

    block_t *peeked = sys->peeked;
    if (peeked != NULL)
    {   /* dequeue peeked data */
        size_t length = peeked->i_buffer;
        if (length > buflen)
            length = buflen;

        if (buf != NULL)
        {
            memcpy (buf, peeked->p_buffer, length);
            buf = ((char *)buf) + length;
        }
        buflen -= length;
        peeked->p_buffer += length;
        peeked->i_buffer -= length;

        if (peeked->i_buffer == 0)
        {
            block_Release (peeked);
            sys->peeked = NULL;
        }

        sys->offset += length;
        ret += length;
    }
    assert ((buf != NULL) || (buflen == 0));

    ssize_t val = net_Read (stream, sys->read_fd, NULL, buf, buflen, false);
    if (val > 0)
    {
        sys->offset += val;
        ret += val;
    }
    return ret;
}
Пример #7
0
char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
{
    char *psz_line = NULL, *ptr = NULL;
    size_t  i_line = 0, i_max = 0;


    for( ;; )
    {
        if( i_line == i_max )
        {
            i_max += 1024;
            psz_line = realloc( psz_line, i_max );
            ptr = psz_line + i_line;
        }

        if( net_Read( p_this, fd, p_vs, (uint8_t *)ptr, 1, VLC_TRUE ) != 1 )
        {
            if( i_line == 0 )
            {
                free( psz_line );
                return NULL;
            }
            break;
        }

        if ( *ptr == '\n' )
            break;

        i_line++;
        ptr++;
    }

    *ptr-- = '\0';

    if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
        *ptr = '\0';

    return psz_line;
}
Пример #8
0
/**
 * Reads decompressed from the decompression program
 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
 */
static int Read (stream_t *stream, void *buf, unsigned int buflen)
{
    stream_sys_t *p_sys = stream->p_sys;
    block_t *peeked;
    ssize_t length;

    if (buf == NULL) /* caller skips data, get big enough peek buffer */
        buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);

    if ((peeked = p_sys->peeked) != NULL)
    {   /* dequeue peeked data */
        length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
        if (buf != NULL)
        {
            memcpy (buf, peeked->p_buffer, length);
            buf = ((char *)buf) + length;
        }
        buflen -= length;
        peeked->p_buffer += length;
        peeked->i_buffer -= length;
        if (peeked->i_buffer == 0)
        {
            block_Release (peeked);
            p_sys->peeked = NULL;
        }
        p_sys->offset += length;

        if (buflen > 0)
            length += Read (stream, ((char *)buf) + length, buflen - length);
        return length;
    }
    assert ((buf != NULL) || (buflen == 0));

    length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
    if (length < 0)
        return 0;
    p_sys->offset += length;
    return length;
}
Пример #9
0
/**
 * Gets a datagram from the network.
 * @param fd datagram file descriptor
 * @return a block or NULL on fatal error (socket dead)
 */
static block_t *rtp_dgram_recv (vlc_object_t *obj, int fd)
{
    block_t *block = block_Alloc (0xffff);
    ssize_t len;

    block_cleanup_push (block);
    do
    {
        len = net_Read (obj, fd, NULL,
                        block->p_buffer, block->i_buffer, false);

        if (((len <= 0) && fd_dead (fd)) || !vlc_object_alive (obj))
        {   /* POLLHUP -> permanent (DCCP) socket error */
            block_Release (block);
            block = NULL;
            break;
        }
    }
    while (len == -1);
    vlc_cleanup_pop ();

    return block ? block_Realloc (block, 0, len) : NULL;
}
Пример #10
0
static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
{
    stream_sys_t *sys = stream->p_sys;
    block_t *peeked = sys->peeked;
    size_t curlen;

    if (peeked != NULL)
    {
        curlen = peeked->i_buffer;
        if (curlen < len)
           peeked = block_Realloc (peeked, 0, len);
    }
    else
    {
        curlen = 0;
        peeked = block_Alloc (len);
    }

    sys->peeked = peeked;
    if (unlikely(peeked == NULL))
        return 0;

    while (curlen < len)
    {
        ssize_t val;

        val = net_Read (stream, sys->read_fd, NULL,
                        peeked->p_buffer + curlen, len - curlen, false);
        if (val <= 0)
            break;
        curlen += val;
        peeked->i_buffer = curlen;
    }
    *pbuf = peeked->p_buffer;
    return curlen;
}
Пример #11
0
/*****************************************************************************
 * Run : call Handshake() then submit songs
 *****************************************************************************/
static void *Run(void *data)
{
    intf_thread_t          *p_intf = data;
    uint8_t                 p_buffer[1024];
    int                     canc = vlc_savecancel();
    bool                    b_handshaked = false;

    /* data about audioscrobbler session */
    mtime_t                 next_exchange = -1; /**< when can we send data  */
    unsigned int            i_interval = 0;     /**< waiting interval (secs)*/

    intf_sys_t *p_sys = p_intf->p_sys;

    /* main loop */
    for (;;)
    {
        vlc_restorecancel(canc);
        vlc_mutex_lock(&p_sys->lock);
        mutex_cleanup_push(&p_sys->lock);

        do
            vlc_cond_wait(&p_sys->wait, &p_sys->lock);
        while (mdate() < next_exchange);

        vlc_cleanup_run();
        canc = vlc_savecancel();

        /* handshake if needed */
        if (!b_handshaked)
        {
            msg_Dbg(p_intf, "Handshaking with last.fm ...");

            switch(Handshake(p_intf))
            {
                case VLC_ENOMEM:
                    goto out;

                case VLC_ENOVAR:
                    /* username not set */
                    dialog_Fatal(p_intf,
                        _("Last.fm username not set"),
                        "%s", _("Please set a username or disable the "
                        "audioscrobbler plugin, and restart VLC.\n"
                        "Visit http://www.last.fm/join/ to get an account."));
                    goto out;

                case VLC_SUCCESS:
                    msg_Dbg(p_intf, "Handshake successful :)");
                    b_handshaked = true;
                    i_interval = 0;
                    next_exchange = mdate();
                    break;

                case VLC_AUDIOSCROBBLER_EFATAL:
                    msg_Warn(p_intf, "Exiting...");
                    goto out;

                case VLC_EGENERIC:
                default:
                    /* protocol error : we'll try later */
                    HandleInterval(&next_exchange, &i_interval);
                    break;
            }
            /* if handshake failed let's restart the loop */
            if (!b_handshaked)
                continue;
        }

        msg_Dbg(p_intf, "Going to submit some data...");
        char *psz_submit;
        if (asprintf(&psz_submit, "s=%s", p_sys->psz_auth_token) == -1)
            break;

        /* forge the HTTP POST request */
        vlc_mutex_lock(&p_sys->lock);
        audioscrobbler_song_t *p_song;
        for (int i_song = 0 ; i_song < p_sys->i_songs ; i_song++)
        {
            char *psz_submit_song, *psz_submit_tmp;
            p_song = &p_sys->p_queue[i_song];
            if (asprintf(&psz_submit_song,
                    "&a%%5B%d%%5D=%s"
                    "&t%%5B%d%%5D=%s"
                    "&i%%5B%d%%5D=%u"
                    "&o%%5B%d%%5D=P"
                    "&r%%5B%d%%5D="
                    "&l%%5B%d%%5D=%d"
                    "&b%%5B%d%%5D=%s"
                    "&n%%5B%d%%5D=%s"
                    "&m%%5B%d%%5D=%s",
                    i_song, p_song->psz_a,
                    i_song, p_song->psz_t,
                    i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
                    i_song,
                    i_song,
                    i_song, p_song->i_l,
                    i_song, p_song->psz_b,
                    i_song, p_song->psz_n,
                    i_song, p_song->psz_m
           ) == -1)
            {   /* Out of memory */
                vlc_mutex_unlock(&p_sys->lock);
                goto out;
            }
            psz_submit_tmp = psz_submit;
            if (asprintf(&psz_submit, "%s%s",
                    psz_submit_tmp, psz_submit_song) == -1)
            {   /* Out of memory */
                free(psz_submit_tmp);
                free(psz_submit_song);
                vlc_mutex_unlock(&p_sys->lock);
                goto out;
            }
            free(psz_submit_song);
            free(psz_submit_tmp);
        }
        vlc_mutex_unlock(&p_sys->lock);

        int i_post_socket = net_ConnectTCP(p_intf, p_sys->p_submit_url.psz_host,
                                        p_sys->p_submit_url.i_port);

        if (i_post_socket == -1)
        {
            /* If connection fails, we assume we must handshake again */
            HandleInterval(&next_exchange, &i_interval);
            b_handshaked = false;
            free(psz_submit);
            continue;
        }

        /* we transmit the data */
        int i_net_ret = net_Printf(p_intf, i_post_socket, NULL,
            "POST %s HTTP/1.1\n"
            "Accept-Encoding: identity\n"
            "Content-length: %zu\n"
            "Connection: close\n"
            "Content-type: application/x-www-form-urlencoded\n"
            "Host: %s\n"
            "User-agent: VLC media player/"VERSION"\r\n"
            "\r\n"
            "%s\r\n"
            "\r\n",
            p_sys->p_submit_url.psz_path, strlen(psz_submit),
            p_sys->p_submit_url.psz_host, psz_submit
       );

        free(psz_submit);
        if (i_net_ret == -1)
        {
            /* If connection fails, we assume we must handshake again */
            HandleInterval(&next_exchange, &i_interval);
            b_handshaked = false;
            continue;
        }

        i_net_ret = net_Read(p_intf, i_post_socket, NULL,
                    p_buffer, sizeof(p_buffer) - 1, false);
        if (i_net_ret <= 0)
        {
            /* if we get no answer, something went wrong : try again */
            continue;
        }

        net_Close(i_post_socket);
        p_buffer[i_net_ret] = '\0';

        char *failed = strstr((char *) p_buffer, "FAILED");
        if (failed)
        {
            msg_Warn(p_intf, "%s", failed);
            HandleInterval(&next_exchange, &i_interval);
            continue;
        }

        if (strstr((char *) p_buffer, "BADSESSION"))
        {
            msg_Err(p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?");
            b_handshaked = false;
            HandleInterval(&next_exchange, &i_interval);
            continue;
        }

        if (strstr((char *) p_buffer, "OK"))
        {
            for (int i = 0; i < p_sys->i_songs; i++)
                DeleteSong(&p_sys->p_queue[i]);
            p_sys->i_songs = 0;
            i_interval = 0;
            next_exchange = mdate();
            msg_Dbg(p_intf, "Submission successful!");
        }
        else
        {
            msg_Err(p_intf, "Authentication failed, handshaking again (%s)",
                             p_buffer);
            b_handshaked = false;
            HandleInterval(&next_exchange, &i_interval);
        }
    }
out:
    vlc_restorecancel(canc);
    return NULL;
}
Пример #12
0
static bool read_exact( filter_t *obj, int fd, void *buf, size_t len )
{
    return (ssize_t)len == net_Read( obj, fd, buf, len );
}
Пример #13
0
/*****************************************************************************
 * Run : call Handshake() then submit songs
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    char                    *psz_submit, *psz_submit_song, *psz_submit_tmp;
    int                     i_net_ret;
    int                     i_song;
    uint8_t                 p_buffer[1024];
    char                    *p_buffer_pos;
    int                     i_post_socket;
    int                     canc = vlc_savecancel();

    intf_sys_t *p_sys = p_intf->p_sys;

    /* main loop */
    for( ;; )
    {
        bool b_wait = false;


        vlc_restorecancel( canc );
        vlc_mutex_lock( &p_sys->lock );
        mutex_cleanup_push( &p_sys->lock );

        if( mdate() < p_sys->next_exchange )
            /* wait until we can resubmit, i.e.  */
            b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock,
                                          p_sys->next_exchange ) == 0;
        else
            /* wait for data to submit */
            /* we are signaled each time there is a song to submit */
            vlc_cond_wait( &p_sys->wait, &p_sys->lock );
        vlc_cleanup_run();
        canc = vlc_savecancel();

        if( b_wait )
            continue; /* holding on until next_exchange */

        /* handshake if needed */
        if( !p_sys->b_handshaked )
        {
            msg_Dbg( p_intf, "Handshaking with last.fm ..." );

            switch( Handshake( p_intf ) )
            {
                case VLC_ENOMEM:
                    return;

                case VLC_ENOVAR:
                    /* username not set */
                    dialog_Fatal( p_intf,
                        _("Last.fm username not set"),
                        "%s", _("Please set a username or disable the "
                        "audioscrobbler plugin, and restart VLC.\n"
                        "Visit http://www.last.fm/join/ to get an account.")
                    );
                    return;

                case VLC_SUCCESS:
                    msg_Dbg( p_intf, "Handshake successfull :)" );
                    p_sys->b_handshaked = true;
                    p_sys->i_interval = 0;
                    p_sys->next_exchange = mdate();
                    break;

                case VLC_AUDIOSCROBBLER_EFATAL:
                    msg_Warn( p_intf, "Exiting..." );
                    return;

                case VLC_EGENERIC:
                default:
                    /* protocol error : we'll try later */
                    HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
                    break;
            }
            /* if handshake failed let's restart the loop */
            if( !p_sys->b_handshaked )
                continue;
        }

        msg_Dbg( p_intf, "Going to submit some data..." );

        /* The session may be invalid if there is a trailing \n */
        char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' );
        if( psz_ln )
            *psz_ln = '\0';

        if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
        {   /* Out of memory */
            return;
        }

        /* forge the HTTP POST request */
        vlc_mutex_lock( &p_sys->lock );
        audioscrobbler_song_t *p_song;
        for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
        {
            p_song = &p_sys->p_queue[i_song];
            if( !asprintf( &psz_submit_song,
                    "&a%%5B%d%%5D=%s"
                    "&t%%5B%d%%5D=%s"
                    "&i%%5B%d%%5D=%u"
                    "&o%%5B%d%%5D=P"
                    "&r%%5B%d%%5D="
                    "&l%%5B%d%%5D=%d"
                    "&b%%5B%d%%5D=%s"
                    "&n%%5B%d%%5D=%s"
                    "&m%%5B%d%%5D=%s",
                    i_song, p_song->psz_a,
                    i_song, p_song->psz_t,
                    i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
                    i_song,
                    i_song,
                    i_song, p_song->i_l,
                    i_song, p_song->psz_b,
                    i_song, p_song->psz_n,
                    i_song, p_song->psz_m
            ) )
            {   /* Out of memory */
                vlc_mutex_unlock( &p_sys->lock );
                return;
            }
            psz_submit_tmp = psz_submit;
            if( !asprintf( &psz_submit, "%s%s",
                    psz_submit_tmp, psz_submit_song ) )
            {   /* Out of memory */
                free( psz_submit_tmp );
                free( psz_submit_song );
                vlc_mutex_unlock( &p_sys->lock );
                return;
            }
            free( psz_submit_song );
            free( psz_submit_tmp );
        }
        vlc_mutex_unlock( &p_sys->lock );

        i_post_socket = net_ConnectTCP( p_intf,
            p_sys->psz_submit_host, p_sys->i_submit_port );

        if ( i_post_socket == -1 )
        {
            /* If connection fails, we assume we must handshake again */
            HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
            p_sys->b_handshaked = false;
            free( psz_submit );
            continue;
        }

        /* we transmit the data */
        i_net_ret = net_Printf(
            p_intf, i_post_socket, NULL,
            POST_REQUEST, p_sys->psz_submit_file,
            (unsigned)strlen( psz_submit ), p_sys->psz_submit_host,
            VERSION, psz_submit
        );

        free( psz_submit );
        if ( i_net_ret == -1 )
        {
            /* If connection fails, we assume we must handshake again */
            HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
            p_sys->b_handshaked = false;
            continue;
        }

        i_net_ret = net_Read( p_intf, i_post_socket, NULL,
                    p_buffer, 1023, false );
        if ( i_net_ret <= 0 )
        {
            /* if we get no answer, something went wrong : try again */
            continue;
        }

        net_Close( i_post_socket );
        p_buffer[i_net_ret] = '\0';

        p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
        if ( p_buffer_pos )
        {
            msg_Warn( p_intf, "%s", p_buffer_pos );
            HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
            continue;
        }

        p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
        if ( p_buffer_pos )
        {
            msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
            p_sys->b_handshaked = false;
            HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
            continue;
        }

        p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
        if ( p_buffer_pos )
        {
            int i;
            for( i = 0; i < p_sys->i_songs; i++ )
                DeleteSong( &p_sys->p_queue[i] );
            p_sys->i_songs = 0;
            p_sys->i_interval = 0;
            p_sys->next_exchange = mdate();
            msg_Dbg( p_intf, "Submission successful!" );
        }
        else
        {
            msg_Err( p_intf, "Authentication failed, handshaking again (%s)", 
                             p_buffer );
            p_sys->b_handshaked = false;
            HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
            continue;
        }
    }
    vlc_restorecancel( canc );
}
Пример #14
0
Файл: satip.c Проект: etix/vlc
static enum rtsp_result rtsp_handle(access_t *access, bool *interrupted) {
    access_sys_t *sys = access->p_sys;
    uint8_t buffer[512];
    int rtsp_result = 0;
    bool have_header = false;
    size_t content_length = 0;
    size_t read = 0;
    char *in, *val;

    /* Parse header */
    while (!have_header) {
        in = net_readln_timeout((vlc_object_t*)access, sys->tcp_sock, 5000,
                interrupted);
        if (in == NULL)
            break;

        if (strncmp(in, "RTSP/1.0 ", 9) == 0) {
            rtsp_result = atoi(in + 9);
        } else if (strncmp(in, "Content-Base:", 13) == 0) {
            free(sys->content_base);

            val = in + 13;
            skip_whitespace(val);

            sys->content_base = strdup(val);
        } else if (strncmp(in, "Content-Length:", 15) == 0) {
            val = in + 16;
            skip_whitespace(val);

            content_length = atoi(val);
        } else if (strncmp("Session:", in, 8) == 0) {
            val = in + 8;
            skip_whitespace(val);

            parse_session(val, sys->session_id, 64, &sys->keepalive_interval);
        } else if (strncmp("Transport:", in, 10) == 0) {
            val = in + 10;
            skip_whitespace(val);

            if (parse_transport(access, val) != 0) {
                rtsp_result = VLC_EGENERIC;
                break;
            }
        } else if (strncmp("com.ses.streamID:", in, 17) == 0) {
            val = in + 17;
            skip_whitespace(val);

            sys->stream_id = atoi(val);
        } else if (in[0] == '\0') {
            have_header = true;
        }

        free(in);
    }

    /* Discard further content */
    while (content_length > 0 &&
            (read = net_Read(access, sys->tcp_sock, buffer, __MIN(sizeof(buffer), content_length))))
        content_length -= read;

    return rtsp_result;
}
Пример #15
0
/*****************************************************************************
 * Run : call Handshake() then submit items
 *****************************************************************************/
static void Run(intf_thread_t *p_intf)
{

    msg_Dbg(p_intf, "Entering Run()");

    uint8_t                 p_buffer[1024];
    int                     parse_url_ret;
    int                     canc = vlc_savecancel();

    char                    *psz_scrobbler_url;

    mtime_t                 next_exchange;      /**< when can we send data  */
    unsigned int            i_interval;         /**< waiting interval (secs)*/

    intf_sys_t *p_sys = p_intf->p_sys;

    psz_scrobbler_url = var_InheritString(p_intf, "mediascrobbler-url");
    if(!psz_scrobbler_url)
      return;

    msg_Dbg(p_intf, "Scrobbler URL: %s", psz_scrobbler_url);

    /* main loop */
    for (;;)
    {
        vlc_restorecancel(canc);
        vlc_mutex_lock(&p_sys->lock);
        mutex_cleanup_push(&p_sys->lock);

        msg_Dbg(p_intf, "Next exchange %lld (current: %lld)",
            next_exchange, mdate());

        do {
            vlc_cond_wait(&p_sys->wait, &p_sys->lock);
        } while (mdate() < next_exchange);

        vlc_cleanup_run();
        canc = vlc_savecancel();

        msg_Dbg(p_intf, "Going to submit some data...");
        char *psz_submit;
        if (asprintf(&psz_submit, "s=%s", psz_scrobbler_url) == -1)
            return;

        msg_Dbg(p_intf, "Going to parse URL (%s)", psz_scrobbler_url);
        parse_url_ret = ParseURL(psz_scrobbler_url, &p_sys->psz_submit_host,
                                &p_sys->psz_submit_file, &p_sys->i_submit_port);

        if(parse_url_ret != VLC_SUCCESS) {
            msg_Err(p_intf, "Couldn't process URL, can't continue");
            return;
        }

        msg_Dbg(p_intf, "Submit Host: %s", p_sys->psz_submit_host);

        msg_Dbg(p_intf, "Preparing to submit %d items", p_sys->i_items);

        /* forge the HTTP POST request */
        vlc_mutex_lock(&p_sys->lock);
        mediascrobbler_item_t *p_item;
        for (int i_item = 0 ; i_item < p_sys->i_items ; i_item++)
        {
            char *psz_submit_item, *psz_submit_tmp;
            p_item = &p_sys->p_queue[i_item];
            if (asprintf(&psz_submit_item,
                    "&n%%5B%d%%5D=%s"
                    "&u%%5B%d%%5D=%s"
                    "&d%%5B%d%%5D=%ju"
                    "&l%%5B%d%%5D=%d",
                    i_item, p_item->psz_n,
                    i_item, p_item->psz_u,
                    i_item, p_item->date,
                    i_item, p_item->i_l
           ) == -1)
            {   /* Out of memory */
                vlc_mutex_unlock(&p_sys->lock);
                return;
            }
            psz_submit_tmp = psz_submit;
            if (asprintf(&psz_submit, "%s%s",
                    psz_submit_tmp, psz_submit_item) == -1)
            {   /* Out of memory */
                free(psz_submit_tmp);
                free(psz_submit_item);
                vlc_mutex_unlock(&p_sys->lock);
                return;
            }
            free(psz_submit_item);
            free(psz_submit_tmp);
        }
        vlc_mutex_unlock(&p_sys->lock);

        int i_post_socket = net_ConnectTCP(p_intf, p_sys->psz_submit_host, p_sys->i_submit_port);

        if (i_post_socket == -1)
        {
            msg_Warn(p_intf, "Couldn't talk to the API, waiting to try again. (%d)", i_interval);
            HandleInterval(&next_exchange, &i_interval);
            free(psz_submit);
            continue;
        }

        /* we transmit the data */
        int i_net_ret = net_Printf(p_intf, i_post_socket, NULL,
            "POST %s HTTP/1.1\r\n"
            "Accept-Encoding: identity\r\n"
            "Content-length: %zu\r\n"
            "Connection: close\r\n"
            "Content-type: application/x-www-form-urlencoded\r\n"
            "Host: %s\r\n"
            "User-agent: VLC media player/"VERSION"\r\n"
            "\r\n"
            "%s\r\n"
            "\r\n",
            p_sys->psz_submit_file, strlen(psz_submit),
            p_sys->psz_submit_host, psz_submit
       );

        free(psz_submit);
        if (i_net_ret == -1)
        {
            /* If connection fails, back off the timer, and try again */
            HandleInterval(&next_exchange, &i_interval);
            continue;
        }

        i_net_ret = net_Read(p_intf, i_post_socket, NULL,
                    p_buffer, sizeof(p_buffer) - 1, false);
        if (i_net_ret <= 0)
        {
            /* if we get no answer, something went wrong : try again */
            continue;
        }

        net_Close(i_post_socket);
        p_buffer[i_net_ret] = '\0';

        char *failed = strstr((char *) p_buffer, "FAILED");
        if (failed)
        {
            msg_Warn(p_intf, "%s", failed);
            HandleInterval(&next_exchange, &i_interval);
            continue;
        }

        if (strstr((char *) p_buffer, "BADAPIKEY"))
        {
            msg_Err(p_intf, "Authentication failed (BADAPIKEY), are you sure your API key is valid?");
            HandleInterval(&next_exchange, &i_interval);
            continue;
        }

        if (strstr((char *) p_buffer, "OK"))
        {
            for (int i = 0; i < p_sys->i_items; i++)
                DeleteItem(&p_sys->p_queue[i]);
            p_sys->i_items = 0;
            i_interval = 0;
            next_exchange = mdate();
            msg_Dbg(p_intf, "Submission successful!");
        }
        else
        {
            msg_Err(p_intf, "Authentication failed, trying again (%s)",
                             p_buffer);
            HandleInterval(&next_exchange, &i_interval);
        }
    }
    vlc_restorecancel(canc);
}
Пример #16
0
HtsMessage ReadMessageEx(vlc_object_t *obj, sys_common_t *sys)
{
    char *buf;
    uint32_t len;
    ssize_t readSize;

    if(sys->queue.size())
    {
        HtsMessage res = sys->queue.front();
        sys->queue.pop_front();
        return res;
    }

    if(sys->netfd < 0)
    {
        msg_Dbg(obj, "ReadMessage on closed netfd");
        return HtsMessage();
    }

    if((readSize = net_Read(obj, sys->netfd, &len, sizeof(len))) != sizeof(len))
    {
        net_Close(sys->netfd);
        sys->netfd = -1;

        if(readSize == 0)
        {
            msg_Err(obj, "Size Read EOF!");
            return HtsMessage();
        }
        else if(readSize < 0)
        {
            msg_Err(obj, "Data Read ERROR!");
            return HtsMessage();
        }

        msg_Err(obj, "Error reading size: %m");
        return HtsMessage();
    }

    len = ntohl(len);
    if(len == 0)
        return HtsMessage();

    buf = (char*)malloc(len);

    if((readSize = net_Read(obj, sys->netfd, buf, len)) != (ssize_t)len)
    {
        net_Close(sys->netfd);
        sys->netfd = -1;

        if(readSize == 0)
        {
            msg_Err(obj, "Data Read EOF!");
            return HtsMessage();
        }
        else if(readSize < 0)
        {
            msg_Err(obj, "Data Read ERROR!");
            return HtsMessage();
        }

        msg_Err(obj, "Error reading data: %m");
        return HtsMessage();
    }

    HtsMessage result = HtsMessage::Deserialize(len, buf);
    free(buf);
    return result;
}