欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

updatemanager.cpp

这是一个mp3的源代码
CPP
第 1 页 / 共 3 页
字号:
                        if(!buffer)
                        {
                            result = kError_OutOfMemory;
                            break;
                        }
                    }

                    count = recv(s, buffer + total, bufferSize - total - 1, 0);

                    if(count > 0)
                        total += count;
                    else
                    {
                        result = kError_IOError;
                    }

                    if(function)
                    {
                        UMEvent event;

                        memset(&event, 0x00, sizeof(UMEvent));
                        event.type = kUMEvent_Status;
                        event.eventString = "Downloading ";
                        event.eventString += item->GetLocalFileName();
                        bool ok = function(&event, cookie);

                        if(!ok)
                            result = kError_UserCancel;
                    }


                }while(!IsHTTPHeaderComplete(buffer, total) && IsntError(result));
            }

            // parse header
            if(IsntError(result))
            {
                uint32 returnCode = atoi(buffer+9);
                buffer[total] = 0x00;
                //cout << buffer << endl;

                cout << returnCode << endl;

                switch(buffer[9])
                {
                    // 1xx: Informational - Request received, continuing process
                    case '1':
                    {
                        // not sure what to do here... continue receiving???
                    }    

                    // 2xx: Success - The action was successfully received,
                    // understood, and accepted
                    case '2':
                    {
                        result = kError_UnknownErr;

                        cout << destPath << endl;

                        int32 fileSize = GetContentLengthFromHeader(buffer);

                        int openFlags = O_BINARY|O_CREAT|O_RDWR|O_TRUNC;

                        int fd = open(destPath, openFlags, S_IREAD | S_IWRITE);

                        if(fd >= 0)
                        {
                            result = kError_NoErr;

                            char* cp = strstr(buffer, "\n\n");

                            if(cp)
                                cp += 2;
                            else
                            {
                                cp = strstr(buffer, "\r\n\r\n");

                                if(cp)
                                    cp += 4;
                            }

                            if(cp)
                            {
                                if(cp - buffer < (int)total)
                                {
                                    write(fd, cp, total - (cp - buffer));
                                    total -= (cp - buffer);

                                    if(function)
                                    {
                                        UMEvent event;

                                        memset(&event, 0x00, sizeof(UMEvent));
                                        event.type = kUMEvent_Progress;
                                        event.data.progressData.item = item;
                                        event.data.progressData.position = total;
                                        event.data.progressData.total = fileSize;

                                        bool ok = function(&event, cookie);

                                        if(!ok)
                                            result = kError_UserCancel;
                                    }
                                }
                            }

                            do
                            {
                                count = recv(s, buffer, bufferSize, 0);

                                if(count > 0)
                                {
                                    write(fd, buffer, count);
                                    total += count;
                                }

                                if(count < 0)
                                    result = kError_IOError;
                                
                                if(function)
                                {
                                    UMEvent event;

                                    memset(&event, 0x00, sizeof(UMEvent));
                                    event.type = kUMEvent_Progress;
                                    event.data.progressData.item = item;
                                    event.data.progressData.position = total;
                                    event.data.progressData.total = fileSize;

                                    bool ok = function(&event, cookie);

                                    if(!ok)
                                        result = kError_UserCancel;
                                }

                                //cout << "bytes recvd:" << count << endl;

                            }while(count > 0 && IsntError(result));
                            

                            close(fd);  
                            
                            if(IsntError(result))
                            {

                                // now that we have it we need to uncompress it
                                // old dest is new src...
                                strcpy(srcPath, destPath); 
                                char* cp = strrchr(destPath, '.');

                                if(cp)
                                    *cp = 0x00;
                                                          
                                gzFile gzfd = gzopen(srcPath, "rb");

                                if (gzfd == NULL)
                                    result = kError_FileNotFound;
                                else if((fd = open(destPath, openFlags, S_IREAD | S_IWRITE)) >=0)
                                {
                                    do
                                    {
                                        count = gzread(gzfd, buffer, bufferSize);

                                        if(count > 0)
                                        {
                                            write(fd, buffer, count);
                                        }

                                    }while(count > 0);

                                    close(fd);
                                }

                                if(gzfd != NULL)
                                    gzclose(gzfd);

                                remove(srcPath);
                            }
                        }
                        else
                        {
                            switch(errno)
                            {
                                case EEXIST:
                                    result = kError_FileExists;
                                    break;

                                case EACCES:
                                    result = kError_FileNoAccess;
                                    break;

                                case ENOENT:
                                    result = kError_FileNotFound;
                                    break;

                                case EMFILE:
                                    result = kError_FileNoHandles;
                                    break;

                                case EINVAL:
                                    result = kError_FileInvalidArg;
                                    break;
                
                            }
                        }
                        
                        break;
                    }

                    // 3xx: Redirection - Further action must be taken in order to
                    // complete the request
                    case '3':
                    {
                        char* cp = strstr(buffer, "Location:");
                        //int32 length;

                        if(cp)
                        {
                            cp += 9;

                            if(*cp == 0x20)
                                cp++;

                            char *end;
                            for(end = cp; end < buffer + total; end++)
                                if(*end=='\r' || *end == '\n') break;

                            *end = 0x00;

                            //cout << cp << endl;

                            if(305 == returnCode) // proxy
                            {
                                char* proxy = new char[strlen(cp) + 
                                                       strlen(item->GetCurrentFileURL().c_str()) + 1];

                                sprintf(proxy, "%s%s", cp, item->GetCurrentFileURL().c_str());

                                item->SetCurrentFileURL(proxy);

                                delete [] proxy;
                            }
                            else // redirect of some type
                            {
                                item->SetCurrentFileURL(cp);
                            }

                            result = DownloadItem(item, function, cookie);
                        }
                        
                        break;
                    }

                    // 4xx: Client Error - The request contains bad syntax or cannot
                    // be fulfilled
                    case '4':
                    {
                        switch(returnCode)
                        {
                            case 400:
                                result = kError_BadHTTPRequest;
                                break;

                            case 401:
                                result = kError_AccessNotAuthorized;
                                break;                           

                            case 403:
                                result = kError_AccessForbidden;
                                break;

                            case 404:
                                result = kError_FileNotFound;
                                break;

                            default:
                                result = kError_UnknownErr;
                                break;
                        }

                        break;
                    }

                    // 5xx: Server Error - The server failed to fulfill an apparently
                    // valid request
                    case '5':
                    {
                        result = kError_UnknownServerError;
                        break;
                    }
                }

            }

            // cleanup
            if(buffer)
                free(buffer);            
        }

        // cleanup
        if(s > 0)
            closesocket(s);

        if(destPath)
            delete [] destPath;

        if(srcPath)
            delete [] srcPath;
    }

    return result;

}

// Utility Functions
bool UpdateManager::IsEmpty()
{
    bool result;
    //m_mutex.Acquire();

    result = m_itemList.empty();

    //m_mutex.Release();
    return result;
}

uint32 UpdateManager::CountItems()
{
    uint32 result;
    //m_mutex.Acquire();

    result = m_itemList.size();

    //m_mutex.Release();
    return result;
}

UpdateItem* UpdateManager::ItemAt(uint32 index)
{
    UpdateItem* result = NULL;
    //m_mutex.Acquire();
    
    index = CheckIndex(index);

    if(index != kInvalidIndex)
    {
        result = m_itemList[index];
    }
    
    //m_mutex.Release();
    return result;
}

uint32 UpdateManager::IndexOf(UpdateItem* item)
{
    uint32 result = kInvalidIndex;
    uint32 index = 0;
    uint32 size = 0;

    assert(item);

    if(item)
    {
        size = m_itemList.size();

        for(index = 0; index < size; index++)
        {
            if(item == m_itemList[index])
            {
                result = index;
                break;
            }
        }
    }
    
    return result;
}

bool UpdateManager::HasItem(UpdateItem* item)
{
    return (IndexOf(item) != kInvalidIndex);
}

// Internal functions
inline uint32 UpdateManager::CheckIndex(uint32 index)
{
	// If we're dealing with a bogus index then set it to -1.
	if(index >= CountItems())
    {
		index = kInvalidIndex;
    }

	return index;
}

// parsing code
Error UpdateManager::BeginElement(string &element, AttrMap &attrMap)
{
	m_path += string("/") + element;

	if(m_path == "/VERSIONINFO/PLATFORM")
	{
        m_versionPlatform = attrMap["NAME"];
        m_versionArchitecture = attrMap["ARCHITECTURE"];
    }

    if(m_path == "/VERSIONINFO/PLATFORM/COMPONENT" &&
       m_versionPlatform == m_currentPlatform &&
       m_versionArchitecture == m_currentArchitecture)
    {
        UpdateItem* item;
        bool foundComponent = false;

        vector<UpdateItem*>::iterator i = m_itemList.begin();

        // is there a matching component on this machine
        for (; i != m_itemList.end(); i++)
        {
            item = *i;

            if(!strcasecmp(attrMap["NAME"].c_str(), 
                           item->GetLocalFileName().c_str()))
            {
                foundComponent = true;
                break;
            }
        }

        // need to add a new component to the list
        if(!foundComponent)
        {
            item = new UpdateItem();   
            m_itemList.push_back(item);
            item->SetLocalFileName(attrMap["NAME"]);
        }

        item->SetCurrentFileLocation(attrMap["LOCATION"]);

        string url = "http://";

        // where is the latest version located?
        url += kUpdateServer;
        url += kUpdatePath;
        url += attrMap["LOCATION"];

        item->SetCurrentFileURL(url);

        item->SetCurrentFileVersion(attrMap["VERSION"]);
        item->SetFileDescription(attrMap["DESCRIPTION"]);

        if(attrMap.find("NAME") != attrMap.end())
            cout << "Name: " << attrMap["NAME"] << endl;

        if(attrMap.find("DESCRIPTION") != attrMap.end())
            cout << "Description: " << attrMap["DESCRIPTION"] << endl;

        if(attrMap.find("VERSION") != attrMap.end())
            cout << "Version: " << attrMap["VERSION"] << endl;

        if(attrMap.find("LOCATION") != attrMap.end())
            cout << "Location: " << attrMap["LOCATION"] << endl;
    }

    return kError_NoErr;
}

Error UpdateManager::PCData(string &data)
{
    //cout << "PCData: " << data << endl;

	return kError_NoErr;
}


Error UpdateManager::EndElement(string &element)
{
    //cout << "EndElement: " << element << endl;

	char *ptr;
    int   offset;
    
    ptr = strrchr(m_path.c_str(), '/');
    if (ptr == NULL)
       return kError_NoErr;
       
    offset = ptr - m_path.c_str();
    m_path.erase(offset, m_path.length() - offset);
     
	return kError_NoErr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -