From a97b7ce701d1804d650fd8f4e92a541fdffcdb2f Mon Sep 17 00:00:00 2001 From: "771730766@qq.com" Date: Tue, 30 Jan 2018 11:47:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 51 ++++++++++++++++++++++++++++++--------- src/Http/HttpSession.h | 1 + tests/test_rtmpPusher.cpp | 2 +- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 164674b1..912dd2c5 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -355,7 +355,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { //send completed! //FatalL << "send completed!"; if(iRead>0) { - strongSelf->send(pacSendBuf.get(), iRead); + strongSelf->sock->send(pacSendBuf.get(), iRead,SOCKET_DEFAULE_FLAGS | FLAG_MORE); } if(bClose) { strongSelf->shutdown(); @@ -363,7 +363,7 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { return false; } - int iSent=strongSelf->send(pacSendBuf.get(), iRead); + int iSent = strongSelf->sock->send(pacSendBuf.get(), iRead,SOCKET_DEFAULE_FLAGS | FLAG_MORE); if(iSent == -1) { //send error //FatalL << "send error"; @@ -378,6 +378,8 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { } return false; }; + //关闭tcp_nodelay ,优化性能 + SockUtil::setNoDelay(sock->rawFD(),false); onFlush(); sock->setOnFlush(onFlush); return Http_success; @@ -594,7 +596,7 @@ void HttpSession::onSendMedia(const RtmpPacket::Ptr &pkt) { CLEAR_ARR(m_aui32FirstStamp); modifiedStamp = 0; } - sendRtmp(pkt->typeId, pkt->strBuf, modifiedStamp); + sendRtmp(pkt, modifiedStamp); } #if defined(_WIN32) @@ -614,22 +616,49 @@ public: #pragma pack(pop) #endif // defined(_WIN32) -void HttpSession::sendRtmp(uint8_t ui8Type, const std::string& strBuf, uint32_t ui32TimeStamp) { +class BufferRtmp : public Socket::Buffer{ +public: + typedef std::shared_ptr Ptr; + BufferRtmp(const RtmpPacket::Ptr & pkt):_rtmp(pkt){} + virtual ~BufferRtmp(){} + + char *data() override { + return (char *)_rtmp->strBuf.data(); + } + uint32_t size() const override { + return _rtmp->strBuf.size(); + } +private: + RtmpPacket::Ptr _rtmp; +}; + +void HttpSession::sendRtmp(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp) { auto size = htonl(m_previousTagSize); send((char *)&size,4);//send PreviousTagSize - RtmpTagHeader header; - header.type = ui8Type; - set_be24(header.data_size, strBuf.size()); - + header.type = pkt->typeId; + set_be24(header.data_size, pkt->strBuf.size()); header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff); set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF); - send((char *)&header, sizeof(header));//send tag header - send(strBuf);//send tag data - m_previousTagSize += (strBuf.size() + sizeof(header) + 4); + send(std::make_shared(pkt));//send tag data + m_previousTagSize += (pkt->strBuf.size() + sizeof(header) + 4); m_ticker.resetTime(); } +void HttpSession::sendRtmp(uint8_t ui8Type, const std::string& strBuf, uint32_t ui32TimeStamp) { + auto size = htonl(m_previousTagSize); + send((char *)&size,4);//send PreviousTagSize + RtmpTagHeader header; + header.type = ui8Type; + set_be24(header.data_size, strBuf.size()); + header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff); + set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF); + send((char *)&header, sizeof(header));//send tag header + send(strBuf);//send tag data + m_previousTagSize += (strBuf.size() + sizeof(header) + 4); + m_ticker.resetTime(); +} + } /* namespace Http */ } /* namespace ZL */ diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index e169fcff..26704fdd 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -78,6 +78,7 @@ private: uint32_t m_previousTagSize = 0; RingBuffer::RingReader::Ptr m_pRingReader; void onSendMedia(const RtmpPacket::Ptr &pkt); + void sendRtmp(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp); void sendRtmp(uint8_t ui8Type, const std::string& strBuf, uint32_t ui32TimeStamp); inline HttpCode parserHttpReq(const string &); diff --git a/tests/test_rtmpPusher.cpp b/tests/test_rtmpPusher.cpp index 80e36cb2..d0ed0ce5 100644 --- a/tests/test_rtmpPusher.cpp +++ b/tests/test_rtmpPusher.cpp @@ -121,7 +121,7 @@ int domain(int argc, const char *argv[]) { int main(int argc,char *argv[]){ - const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks","rtmp://jizan.iok.la/live/test"}; + const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks",argv[1]}; return domain(3,argList); }