Пример #1
0
void _Callback2(struct UPnPService *sender, int ErrorCode, void *user,
			    char* Result, unsigned int NumberReturned, 
			    unsigned int TotalMatches, unsigned int UpdateID)
{
	PlayListManager_PC state = (PlayListManager_PC)user;
	struct ILibXMLNode* root = NULL;
	int count = (int)NumberReturned;

	state->Error = -ErrorCode;
	if(ErrorCode != 0)
	{
		sem_post(&state->LockObject);
		return;
	}

	ClearMetadata(state);

	if((int)TotalMatches != state->Parent->TrackCount)
	{
		state->Parent->TrackCount = (int)TotalMatches;
		BitArray_ChangeSize(state->Parent->ShuffleArray, state->Parent->TrackCount);

		if(state->Parent->OnTrackCountChanged != NULL)
		{
			state->Parent->OnTrackCountChanged(state->Parent);
		}
	}

	root = ILibParseXML(Result, 0, (int)strlen(Result));
	if(root != NULL)
	{
		BOOL failure = FALSE;
		if(ILibProcessXMLNodeList(root) == 0)
		{
			int i = 0;
			struct ILibXMLNode* item = root->Next;
            ILibXML_BuildNamespaceLookupTable(root);
			while(item != NULL && i < count)
			{
				struct ILibXMLAttribute* attrs = ILibGetXMLAttributes(item);
				if(attrs != NULL)
				{
					state->Objects[i] = CDS_DeserializeDidlToObject(item, attrs, 1, Result, Result + strlen(Result));
					ILibDestructXMLAttributeList(attrs);
				}
				else
				{
					failure = TRUE;
					state->Objects[i] = NULL;
				}
				i++;
				item = item->Peer;
			}
		}

		ILibDestructXMLNodeList(root);

		if(failure == TRUE)
		{
			int i;
			state->Count = 0;
			for(i = 0; i < count; i++)
			{
				if(state->Objects[i] != NULL)
				{
					CDS_ObjRef_Release(state->Objects[i]);
					state->Objects[i] = NULL;
				}
			}
			state->Error = 1;
		}

		state->Count = count;
	}
	else
	{
		state->Error = 2;
	}

	sem_post(&state->LockObject);
}
Пример #2
0
void _ProcessBuffer(PlayListManager_S state, int done)
{
    int i;
    char* usableBuffer = NULL;
    int streamOffset = 0;
    int trackCount = 0;
    int bufferLength = CircularBuffer_GetLength(state->_buffer);
    int startOfFirstItem = CircularBuffer_FindPattern(state->_buffer, 0, "<item", 5);
    int lengthOfUsableBuffer = -1;

    if(startOfFirstItem >= 0)
    {
        lengthOfUsableBuffer = (CircularBuffer_FindLastPattern(state->_buffer, 0, "</item>", 7) + 7) - startOfFirstItem;
        trackCount++;
    }
    else
    {
        CircularBuffer_ConsumeBytes(state->_buffer, bufferLength - 4);
        state->_streamOffset += (bufferLength - 4);
        if(done == 1)
        {
            sem_post(&state->BlocksFinished);
        }
        return;
    }

    streamOffset = state->_streamOffset + startOfFirstItem;

    usableBuffer = (char*)malloc(lengthOfUsableBuffer);
    CircularBuffer_CopyFrom(state->_buffer, usableBuffer, 0, startOfFirstItem, lengthOfUsableBuffer - startOfFirstItem);
    CircularBuffer_ConsumeBytes(state->_buffer, lengthOfUsableBuffer + startOfFirstItem);
    state->_streamOffset += startOfFirstItem;
    
    for(i = 5; i < (lengthOfUsableBuffer - 5); i++)
    {
        if(memcmp(usableBuffer + i, "<item", (size_t)5) == 0)
        {
            trackCount++;
        }
    }

    IndexBlocks_AddBlock(state->Blocks, streamOffset, lengthOfUsableBuffer, trackCount);
    state->_streamOffset += lengthOfUsableBuffer;

    state->Parent->TrackCount = IndexBlocks_GetTrackCount(state->Blocks);

    BitArray_ChangeSize(state->Parent->ShuffleArray, state->Parent->TrackCount);

    free(usableBuffer);


    sem_post(&state->FirstBlockFinished);
    if(done == 1)
    {
        sem_post(&state->BlocksFinished);
    }

    if(state->Parent->OnTrackCountChanged != NULL)
    {
        state->Parent->OnTrackCountChanged(state->Parent);
    }
}