diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 9cb316d7..71081a3f 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -115,6 +115,9 @@ void HttpClient::onConnect(const SockException &ex) { return; } + //先假设http客户端只会接收一点点数据(只接受http头,节省内存) + _sock->setReadBuffer(std::make_shared(1 * 1024)); + _totalBodySize = 0; _recvedBodySize = 0; HttpRequestSplitter::reset(); @@ -155,6 +158,9 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) { } if(_parser["Transfer-Encoding"] == "chunked"){ + //我们认为这种情况下后面应该有大量的数据过来,加大接收缓存提高性能 + _sock->setReadBuffer(std::make_shared(256 * 1024)); + //如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的 _totalBodySize = -1; _chunkedSplitter = std::make_shared([this](const char *data,uint64_t len){ @@ -179,6 +185,8 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) { //但是由于我们没必要等content接收完毕才回调onRecvContent(因为这样浪费内存并且要多次拷贝数据) //所以返回-1代表我们接下来分段接收content _recvedBodySize = 0; + //根据_totalBodySize设置接收缓存大小 + _sock->setReadBuffer(std::make_shared(MAX(_totalBodySize + 1,256 * 1024))); return -1; } diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index d64438f7..bbe5a27a 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -130,6 +130,9 @@ void RtmpPusher::onConnect(const SockException &err){ onPublishResult(err); return; } + //推流器不需要多大的接收缓存,节省内存占用 + _sock->setReadBuffer(std::make_shared(1 * 1024)); + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); startClientSession([weakSelf](){ auto strongSelf=weakSelf.lock(); diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 17f2ae11..4b96de81 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -139,6 +139,8 @@ void RtspPusher::onConnect(const SockException &err) { onPublishResult(err); return; } + //推流器不需要多大的接收缓存,节省内存占用 + _sock->setReadBuffer(std::make_shared(1 * 1024)); sendAnnounce(); }