wxWidgets / wxWidgets

Cross-Platform C++ GUI Library
https://www.wxwidgets.org/
6.17k stars 1.77k forks source link

Downloading file is very slow by wxURL? #22569

Open pigLoveRabbit520 opened 2 years ago

pigLoveRabbit520 commented 2 years ago
int downloadFile(wxString myFile) {
    wxURL url(remoteFile);
    if (url.GetError() == wxURL_NOERR) {
        wxInputStream* in = url.GetInputStream();
        if (in && in->IsOk())
        {
            wxFileOutputStream outFile(myFile);
            outFile.Write(*in);
        }
        delete in;
        return 0;
    }
    else {
        return -1;
    }
}

My remoteFile is using cdn, I can download it soon in the browser. However, I have to wait about a minute in wxWidgets.

Platform and version information

vadz commented 2 years ago

Use wxWebRequest if you need to just download the files, it's much more feature-rich and almost surely much faster than wx socket streams.

pigLoveRabbit520 commented 2 years ago

ok

pigLoveRabbit520 commented 2 years ago

So what is the best practice to notify outside function? My download function is here:

wxWebRequest request = wxWebSession::GetDefault().CreateRequest(this, remoteFile);
Bind(wxEVT_WEBREQUEST_STATE, [localZipFile](wxWebRequestEvent& evt) {
    switch (evt.GetState())
    {
        // Request completed
        case wxWebRequest::State_Completed:
        {
            wxFileOutputStream outFile(localZipFile);
            wxInputStream* in = evt.GetResponse().GetStream();
            if (in && in->IsOk())
            {
                wxFileOutputStream outFile(localZipFile);
                outFile.Write(*in);
            }
            delete in;
            break;
        }
        // Request failed
        case wxWebRequest::State_Failed:
            .....
            break;
    }
});
request.Start();

request.Start() is async, so how to await the downloading task? There are too little tutorials about wxWidgets.

PBfordev commented 2 years ago

JFYI, GIT Issues are not for asking questions about user code, they are for reporting bugs or proposing enhancements to wxWidgets.

Use one of wx-users, wxForum, StackOverflow or similar for questions like this...

BTW, did you check the bundled webrequest sample, which also shows how to download a file?

pigLoveRabbit520 commented 2 years ago

@PBfordev thks.