HttpDownloader.h 1.69 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9 10 11 12 13 14 15
 */

#ifndef SRC_HTTP_HTTPDOWNLOADER_H_
#define SRC_HTTP_HTTPDOWNLOADER_H_

#include "HttpClientImp.h"

xiongziliang committed
16
namespace mediakit {
xiongziliang committed
17 18 19

class HttpDownloader: public HttpClientImp {
public:
20 21 22 23 24 25 26 27 28 29 30 31 32
    typedef std::shared_ptr<HttpDownloader> Ptr;
    typedef std::function<void(ErrCode code,const string &errMsg,const string &filePath)> onDownloadResult;
    HttpDownloader();
    virtual ~HttpDownloader();
    //开始下载文件,默认断点续传方式下载
    void startDownload(const string &url,const string &filePath = "",bool bAppend = false, float timeOutSecond = 10 );
    void startDownload(const string &url,const onDownloadResult &cb,float timeOutSecond = 10){
        setOnResult(cb);
        startDownload(url,"",false,timeOutSecond);
    }
    void setOnResult(const onDownloadResult &cb){
        _onResult = cb;
    }
xiongziliang committed
33
private:
34
    ssize_t onResponseHeader(const string &status, const HttpHeader &headers) override;
35
    void onResponseBody(const char *buf, size_t size, size_t recvedSize, size_t totalSize) override;
36 37
    void onResponseCompleted() override;
    void onDisconnect(const SockException &ex) override;
38
    void closeFile();
39

40
private:
41 42 43 44
    FILE *_saveFile = nullptr;
    string _filePath;
    onDownloadResult _onResult;
    bool _bDownloadSuccess = false;
xiongziliang committed
45 46
};

xiongziliang committed
47
} /* namespace mediakit */
xiongziliang committed
48 49

#endif /* SRC_HTTP_HTTPDOWNLOADER_H_ */