httpdownloader.cpp 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * MIT License
 *
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "httpdownloader.h"

#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Http/HttpDownloader.h"
#include "cleaner.h"

using namespace std;
xiongziliang committed
36
using namespace toolkit;
37 38 39 40 41 42 43 44 45 46 47 48 49 50

static recursive_mutex s_mtxMapDownloader;
static unordered_map<void *, HttpDownloader::Ptr> s_mapDownloader;

static onceToken s_token([](){
	cleaner::Instance().push_front([](){
		lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
		s_mapDownloader.clear();
		DebugL << "clear httpdownloader" << endl;
	});
},nullptr);



51
API_EXPORT HttpDownloaderContex API_CALL createDownloader(){
52 53 54 55 56
	HttpDownloader::Ptr ret(new HttpDownloader());
	lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
	s_mapDownloader.emplace(ret.get(),ret);
	return ret.get();
}
57
API_EXPORT void API_CALL downloader_startDownload(HttpDownloaderContex ctx,const char *url,downloader_onResult cb,void *userData){
xiongziliang committed
58 59 60 61 62
    downloader_startDownload_l(ctx,url,"",cb,userData);
}


API_EXPORT void API_CALL downloader_startDownload_l(HttpDownloaderContex ctx,const char *url,const  char *file,downloader_onResult cb,void *userData){
63 64
	HttpDownloader *ptr = (HttpDownloader *)ctx;
	string urlTmp(url);
xiongziliang committed
65
	ptr->setOnResult([cb,userData,urlTmp](int code,const char *errMsg,const char *filePath){
66 67 68 69 70
		if(cb){
			InfoL << code << " " << errMsg << " " << filePath << " " << urlTmp;
			cb(userData,code,errMsg,filePath);
		}
	});
xiongziliang committed
71
	ptr->startDownload(url,file,false);
72
}
73
API_EXPORT void API_CALL releaseDownloader(HttpDownloaderContex ctx){
74 75 76 77
	lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
	s_mapDownloader.erase(ctx);
}