mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-03-22 22:10:53 +08:00
适配ZLToolKit代码,简化程序退出流程代码
This commit is contained in:
@@ -41,93 +41,132 @@ using namespace mediakit;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
|
||||
signal(SIGINT, [](int) { EventPollerPool::Instance().shutdown(); });
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
{
|
||||
///////////////////////////////http downloader///////////////////////
|
||||
//下载器map
|
||||
map<string, HttpDownloader::Ptr> downloaderMap;
|
||||
//下载两个文件,一个是http下载,一个https下载
|
||||
auto urlList = {"https://timgsa.baidu.com/timg?image&quality=80&"
|
||||
"size=b9999_10000&sec=1537717640404&"
|
||||
"di=f602efbebbc1e7f6b9ccb0bf0def89d0&"
|
||||
"imgtype=0&"
|
||||
"src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F241f95cad1c8a786ff65052a6d09c93d70cf5042.jpg",};
|
||||
|
||||
for (auto &url : urlList) {
|
||||
//创建下载器
|
||||
HttpDownloader::Ptr downloader(new HttpDownloader());
|
||||
downloader->setOnResult([](ErrCode code, const char *errMsg, const char *filePath) {
|
||||
DebugL << "=====================HttpDownloader result=======================";
|
||||
//下载结果回调
|
||||
if (code == Err_success) {
|
||||
//文件下载成功
|
||||
InfoL << "download file success:" << filePath;
|
||||
} else {
|
||||
//下载失败
|
||||
WarnL << "code:" << code << " msg:" << errMsg;
|
||||
}
|
||||
});
|
||||
//断点续传功能,开启后可能会遇到416的错误(因为上次文件已经下载完全)
|
||||
downloader->startDownload(url, exeDir() + MD5(url).hexdigest() + ".jpg", true);
|
||||
//下载器必须被强引用,否则作用域一失效就会导致对象销毁
|
||||
downloaderMap.emplace(url, downloader);
|
||||
}
|
||||
///////////////////////////////http downloader///////////////////////
|
||||
//下载器map
|
||||
map<string, HttpDownloader::Ptr> downloaderMap;
|
||||
//下载两个文件,一个是http下载,一个https下载
|
||||
auto urlList = {"https://timgsa.baidu.com/timg?image&quality=80&"
|
||||
"size=b9999_10000&sec=1537717640404&"
|
||||
"di=f602efbebbc1e7f6b9ccb0bf0def89d0&"
|
||||
"imgtype=0&"
|
||||
"src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F241f95cad1c8a786ff65052a6d09c93d70cf5042.jpg",};
|
||||
|
||||
///////////////////////////////http get///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterGet(new HttpRequester());
|
||||
//使用GET方式请求
|
||||
requesterGet->setMethod("GET");
|
||||
//设置http请求头,我们假设设置cookie,当然你也可以设置其他http头
|
||||
requesterGet->addHeader("Cookie", "SESSIONID=e1aa89b3-f79f-4ac6-8ae2-0cea9ae8e2d7");
|
||||
//开启请求,该api会返回当前主机外网ip等信息
|
||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
DebugL << "=====================HttpRequester GET===========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
for (auto &url : urlList) {
|
||||
//创建下载器
|
||||
HttpDownloader::Ptr downloader(new HttpDownloader());
|
||||
downloader->setOnResult([](ErrCode code, const char *errMsg, const char *filePath) {
|
||||
DebugL << "=====================HttpDownloader result=======================";
|
||||
//下载结果回调
|
||||
if (code == Err_success) {
|
||||
//文件下载成功
|
||||
InfoL << "download file success:" << filePath;
|
||||
} else {
|
||||
//下载失败
|
||||
WarnL << "code:" << code << " msg:" << errMsg;
|
||||
}
|
||||
});
|
||||
//断点续传功能,开启后可能会遇到416的错误(因为上次文件已经下载完全)
|
||||
downloader->startDownload(url, exeDir() + MD5(url).hexdigest() + ".jpg", true);
|
||||
//下载器必须被强引用,否则作用域一失效就会导致对象销毁
|
||||
downloaderMap.emplace(url, downloader);
|
||||
}
|
||||
|
||||
///////////////////////////////http get///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterGet(new HttpRequester());
|
||||
//使用GET方式请求
|
||||
requesterGet->setMethod("GET");
|
||||
//设置http请求头,我们假设设置cookie,当然你也可以设置其他http头
|
||||
requesterGet->addHeader("Cookie", "SESSIONID=e1aa89b3-f79f-4ac6-8ae2-0cea9ae8e2d7");
|
||||
//开启请求,该api会返回当前主机外网ip等信息
|
||||
requesterGet->startRequester("http://pv.sohu.com/cityjson?ie=utf-8",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
DebugL << "=====================HttpRequester GET===========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
});
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
}
|
||||
});
|
||||
|
||||
///////////////////////////////http post///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterPost(new HttpRequester());
|
||||
//使用POST方式请求
|
||||
requesterPost->setMethod("POST");
|
||||
//设置http请求头
|
||||
requesterPost->addHeader("X-Requested-With", "XMLHttpRequest");
|
||||
requesterPost->addHeader("Origin", "http://fanyi.baidu.com");
|
||||
//设置POST参数列表
|
||||
HttpArgs args;
|
||||
args["query"] = "test";
|
||||
args["from"] = "en";
|
||||
args["to"] = "zh";
|
||||
args["transtype"] = "translang";
|
||||
args["simple_means_flag"] = "3";
|
||||
requesterPost->setBody(args.make());
|
||||
//开启请求
|
||||
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
///////////////////////////////http post///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterPost(new HttpRequester());
|
||||
//使用POST方式请求
|
||||
requesterPost->setMethod("POST");
|
||||
//设置http请求头
|
||||
requesterPost->addHeader("X-Requested-With", "XMLHttpRequest");
|
||||
requesterPost->addHeader("Origin", "http://fanyi.baidu.com");
|
||||
//设置POST参数列表
|
||||
HttpArgs args;
|
||||
args["query"] = "test";
|
||||
args["from"] = "en";
|
||||
args["to"] = "zh";
|
||||
args["transtype"] = "translang";
|
||||
args["simple_means_flag"] = "3";
|
||||
requesterPost->setBody(args.make());
|
||||
//开启请求
|
||||
requesterPost->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
DebugL << "=====================HttpRequester POST==========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
}
|
||||
});
|
||||
|
||||
///////////////////////////////http upload///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterUploader(new HttpRequester());
|
||||
//使用POST方式请求
|
||||
requesterUploader->setMethod("POST");
|
||||
//设置http请求头
|
||||
HttpArgs argsUploader;
|
||||
argsUploader["query"] = "test";
|
||||
argsUploader["from"] = "en";
|
||||
argsUploader["to"] = "zh";
|
||||
argsUploader["transtype"] = "translang";
|
||||
argsUploader["simple_means_flag"] = "3";
|
||||
|
||||
static string boundary = "0xKhTmLbOuNdArY";
|
||||
HttpMultiFormBody::Ptr body(new HttpMultiFormBody(argsUploader, exePath(), boundary));
|
||||
requesterUploader->setBody(body);
|
||||
requesterUploader->addHeader("Content-Type", HttpMultiFormBody::multiFormContentType(boundary));
|
||||
//开启请求
|
||||
requesterUploader->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
DebugL << "=====================HttpRequester POST==========================";
|
||||
DebugL << "=====================HttpRequester Uploader==========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
@@ -143,52 +182,8 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
});
|
||||
|
||||
///////////////////////////////http upload///////////////////////
|
||||
//创建一个Http请求器
|
||||
HttpRequester::Ptr requesterUploader(new HttpRequester());
|
||||
//使用POST方式请求
|
||||
requesterUploader->setMethod("POST");
|
||||
//设置http请求头
|
||||
HttpArgs argsUploader;
|
||||
argsUploader["query"] = "test";
|
||||
argsUploader["from"] = "en";
|
||||
argsUploader["to"] = "zh";
|
||||
argsUploader["transtype"] = "translang";
|
||||
argsUploader["simple_means_flag"] = "3";
|
||||
|
||||
static string boundary = "0xKhTmLbOuNdArY";
|
||||
HttpMultiFormBody::Ptr body(new HttpMultiFormBody(argsUploader, exePath(), boundary));
|
||||
requesterUploader->setBody(body);
|
||||
requesterUploader->addHeader("Content-Type", HttpMultiFormBody::multiFormContentType(boundary));
|
||||
//开启请求
|
||||
requesterUploader->startRequester("http://fanyi.baidu.com/langdetect",//url地址
|
||||
[](const SockException &ex, //网络相关的失败信息,如果为空就代表成功
|
||||
const string &status, //http回复的状态码,比如说200/404
|
||||
const HttpClient::HttpHeader &header, //http回复头
|
||||
const string &strRecvBody) { //http回复body
|
||||
DebugL << "=====================HttpRequester Uploader==========================";
|
||||
if (ex) {
|
||||
//网络相关的错误
|
||||
WarnL << "network err:" << ex.getErrCode() << " " << ex.what();
|
||||
} else {
|
||||
//打印http回复信息
|
||||
_StrPrinter printer;
|
||||
for (auto &pr: header) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << status << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << strRecvBody;
|
||||
}
|
||||
});
|
||||
|
||||
//事件轮询
|
||||
EventPoller::Instance().runLoop();
|
||||
}
|
||||
//程序开始退出
|
||||
EventPoller::Destory();
|
||||
AsyncTaskThread::Destory();
|
||||
Logger::Destory();
|
||||
//事件轮询
|
||||
EventPollerPool::Instance().wait();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user