mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-19 06:42:21 +08:00
全面整理代码,去除编译警告
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
||||
*
|
||||
* 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
|
||||
@@ -33,52 +33,33 @@ namespace mediakit {
|
||||
class HttpBody : public std::enable_shared_from_this<HttpBody>{
|
||||
public:
|
||||
typedef std::shared_ptr<HttpBody> Ptr;
|
||||
HttpBody(){
|
||||
// _async_read_thread = WorkThreadPool::Instance().getPoller();
|
||||
}
|
||||
HttpBody(){}
|
||||
|
||||
virtual ~HttpBody(){}
|
||||
|
||||
/**
|
||||
* 剩余数据大小,如果返回>=INT64_MAX, 那么就不设置content-length
|
||||
* 剩余数据大小,如果返回-1, 那么就不设置content-length
|
||||
*/
|
||||
virtual uint64_t remainSize() { return 0;};
|
||||
virtual size_t remainSize() { return 0;};
|
||||
|
||||
/**
|
||||
* 读取一定字节数,返回大小可能小于size
|
||||
* @param size 请求大小
|
||||
* @return 字节对象,如果读完了,那么请返回nullptr
|
||||
*/
|
||||
virtual Buffer::Ptr readData(uint32_t size) { return nullptr;};
|
||||
virtual Buffer::Ptr readData(size_t size) { return nullptr;};
|
||||
|
||||
/**
|
||||
* 异步请求读取一定字节数,返回大小可能小于size
|
||||
* @param size 请求大小
|
||||
* @param cb 回调函数
|
||||
*/
|
||||
virtual void readDataAsync(uint32_t size,const function<void(const Buffer::Ptr &buf)> &cb){
|
||||
#if 0
|
||||
if(size >= remainSize()){
|
||||
//假如剩余数据很小,那么同步获取(为了优化性能)
|
||||
cb(readData(size));
|
||||
return;
|
||||
}
|
||||
//如果是大文件,那么后台读取
|
||||
weak_ptr<HttpBody> weakSelf = shared_from_this();
|
||||
_async_read_thread->async([cb,size,weakSelf](){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(strongSelf){
|
||||
cb(strongSelf->readData(size));
|
||||
}
|
||||
});
|
||||
#else
|
||||
virtual void readDataAsync(size_t size,const function<void(const Buffer::Ptr &buf)> &cb){
|
||||
//由于unix和linux是通过mmap的方式读取文件,所以把读文件操作放在后台线程并不能提高性能
|
||||
//反而会由于频繁的线程切换导致性能降低以及延时增加,所以我们默认同步获取文件内容
|
||||
//(其实并没有读,拷贝文件数据时在内核态完成文件读)
|
||||
cb(readData(size));
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
// EventPoller::Ptr _async_read_thread;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -89,11 +70,12 @@ public:
|
||||
typedef std::shared_ptr<HttpStringBody> Ptr;
|
||||
HttpStringBody(const string &str);
|
||||
virtual ~HttpStringBody(){}
|
||||
uint64_t remainSize() override ;
|
||||
Buffer::Ptr readData(uint32_t size) override ;
|
||||
size_t remainSize() override;
|
||||
Buffer::Ptr readData(size_t size) override ;
|
||||
|
||||
private:
|
||||
size_t _offset = 0;
|
||||
mutable string _str;
|
||||
uint64_t _offset = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -109,18 +91,20 @@ public:
|
||||
* @param offset 相对文件头的偏移量
|
||||
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
||||
*/
|
||||
HttpFileBody(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size);
|
||||
HttpFileBody(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
|
||||
HttpFileBody(const string &file_path);
|
||||
~HttpFileBody(){};
|
||||
|
||||
uint64_t remainSize() override ;
|
||||
Buffer::Ptr readData(uint32_t size) override;
|
||||
size_t remainSize() override ;
|
||||
Buffer::Ptr readData(size_t size) override;
|
||||
|
||||
private:
|
||||
void init(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size);
|
||||
void init(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
|
||||
|
||||
private:
|
||||
size_t _max_size;
|
||||
size_t _offset = 0;
|
||||
std::shared_ptr<FILE> _fp;
|
||||
uint64_t _max_size;
|
||||
uint64_t _offset = 0;
|
||||
std::shared_ptr<char> _map_addr;
|
||||
ResourcePool<BufferRaw> _pool;
|
||||
};
|
||||
@@ -142,18 +126,20 @@ public:
|
||||
*/
|
||||
HttpMultiFormBody(const HttpArgs &args,const string &filePath,const string &boundary = "0xKhTmLbOuNdArY");
|
||||
virtual ~HttpMultiFormBody(){}
|
||||
uint64_t remainSize() override ;
|
||||
Buffer::Ptr readData(uint32_t size) override;
|
||||
size_t remainSize() override ;
|
||||
Buffer::Ptr readData(size_t size) override;
|
||||
|
||||
public:
|
||||
static string multiFormBodyPrefix(const HttpArgs &args,const string &boundary,const string &fileName);
|
||||
static string multiFormBodySuffix(const string &boundary);
|
||||
static uint64_t fileSize(FILE *fp);
|
||||
static size_t fileSize(FILE *fp);
|
||||
static string multiFormContentType(const string &boundary);
|
||||
|
||||
private:
|
||||
size_t _offset = 0;
|
||||
size_t _totalSize;
|
||||
string _bodyPrefix;
|
||||
string _bodySuffix;
|
||||
uint64_t _offset = 0;
|
||||
uint64_t _totalSize;
|
||||
HttpFileBody::Ptr _fileBody;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user