减少内存拷贝,提高websocket服务器性能

This commit is contained in:
xiongziliang 2019-08-13 12:00:21 +08:00
parent 0931d82a2a
commit 453994f26b
5 changed files with 24 additions and 27 deletions

View File

@ -954,12 +954,12 @@ void HttpSession::onWrite(const Buffer::Ptr &buffer) {
header._reserved = 0; header._reserved = 0;
header._opcode = WebSocketHeader::BINARY; header._opcode = WebSocketHeader::BINARY;
header._mask_flag = false; header._mask_flag = false;
WebSocketSplitter::encode(header,(uint8_t *)buffer->data(),buffer->size()); WebSocketSplitter::encode(header,buffer);
} }
void HttpSession::onWebSocketEncodeData(const uint8_t *ptr,uint64_t len){ void HttpSession::onWebSocketEncodeData(const Buffer::Ptr &buffer){
_ui64TotalBytes += len; _ui64TotalBytes += buffer->size();
send((char *)ptr,len); send(buffer);
} }
void HttpSession::onDetach() { void HttpSession::onDetach() {

View File

@ -104,10 +104,9 @@ protected:
/** /**
* websocket协议打包后回调 * websocket协议打包后回调
* @param ptr * @param buffer
* @param len
*/ */
void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override; void onWebSocketEncodeData(const Buffer::Ptr &buffer) override;
private: private:
inline void Handle_Req_GET(int64_t &content_len); inline void Handle_Req_GET(int64_t &content_len);
inline void Handle_Req_POST(int64_t &content_len); inline void Handle_Req_POST(int64_t &content_len);

View File

@ -89,7 +89,7 @@ protected:
header._reserved = 0; header._reserved = 0;
header._opcode = WebSocketHeader::TEXT; header._opcode = WebSocketHeader::TEXT;
header._mask_flag = false; header._mask_flag = false;
strongSelf->WebSocketSplitter::encode(header,(uint8_t *)buf->data(),buf->size()); strongSelf->WebSocketSplitter::encode(header,buf);
} }
return buf->size(); return buf->size();
}); });
@ -118,12 +118,12 @@ protected:
switch (header._opcode){ switch (header._opcode){
case WebSocketHeader::CLOSE:{ case WebSocketHeader::CLOSE:{
HttpSessionType::encode(header,nullptr,0); HttpSessionType::encode(header,nullptr);
} }
break; break;
case WebSocketHeader::PING:{ case WebSocketHeader::PING:{
const_cast<WebSocketHeader&>(header)._opcode = WebSocketHeader::PONG; const_cast<WebSocketHeader&>(header)._opcode = WebSocketHeader::PONG;
HttpSessionType::encode(header,(uint8_t *)_remian_data.data(),_remian_data.size()); HttpSessionType::encode(header,std::make_shared<BufferString>(_remian_data));
} }
break; break;
case WebSocketHeader::CONTINUATION:{ case WebSocketHeader::CONTINUATION:{
@ -132,8 +132,7 @@ protected:
break; break;
case WebSocketHeader::TEXT: case WebSocketHeader::TEXT:
case WebSocketHeader::BINARY:{ case WebSocketHeader::BINARY:{
BufferString::Ptr buffer = std::make_shared<BufferString>(_remian_data); _session->onRecv(std::make_shared<BufferString>(_remian_data));
_session->onRecv(buffer);
} }
break; break;
default: default:
@ -145,11 +144,10 @@ protected:
/** /**
* websocket协议打包后回调 * websocket协议打包后回调
* @param ptr * @param buffer
* @param len
*/ */
void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len) override{ void onWebSocketEncodeData(const Buffer::Ptr &buffer) override{
SocketHelper::send((char *)ptr,len); SocketHelper::send(buffer);
} }
private: private:
typedef function<int(const Buffer::Ptr &buf)> onBeforeSendCB; typedef function<int(const Buffer::Ptr &buf)> onBeforeSendCB;

View File

@ -164,9 +164,9 @@ void WebSocketSplitter::onPlayloadData(uint8_t *ptr, uint64_t len) {
onWebSocketDecodePlayload(*this, _mask_flag ? ptr - len : ptr, len, _playload_offset); onWebSocketDecodePlayload(*this, _mask_flag ? ptr - len : ptr, len, _playload_offset);
} }
void WebSocketSplitter::encode(const WebSocketHeader &header,uint8_t *data, const uint64_t len) { void WebSocketSplitter::encode(const WebSocketHeader &header,const Buffer::Ptr &buffer) {
string ret; string ret;
auto len = buffer ? buffer->size() : 0;
uint8_t byte = header._fin << 7 | ((header._reserved & 0x07) << 4) | (header._opcode & 0x0F) ; uint8_t byte = header._fin << 7 | ((header._reserved & 0x07) << 4) | (header._opcode & 0x0F) ;
ret.push_back(byte); ret.push_back(byte);
@ -195,16 +195,16 @@ void WebSocketSplitter::encode(const WebSocketHeader &header,uint8_t *data, cons
ret.append((char *)header._mask.data(),4); ret.append((char *)header._mask.data(),4);
} }
onWebSocketEncodeData((uint8_t*)ret.data(),ret.size()); onWebSocketEncodeData(std::make_shared<BufferString>(std::move(ret)));
if(len > 0){ if(len > 0){
if(mask_flag){ if(mask_flag){
uint8_t *ptr = data; uint8_t *ptr = (uint8_t*)buffer->data();
for(int i = 0; i < len ; ++i,++ptr){ for(int i = 0; i < len ; ++i,++ptr){
*(ptr) ^= header._mask[i % 4]; *(ptr) ^= header._mask[i % 4];
} }
} }
onWebSocketEncodeData(data,len); onWebSocketEncodeData(buffer);
} }
} }

View File

@ -31,8 +31,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory> #include <memory>
using namespace std; #include "Network/Buffer.h"
using namespace std;
using namespace toolkit;
namespace mediakit { namespace mediakit {
@ -85,12 +87,10 @@ public:
/** /**
* *
* 2onWebSocketEncodeData回调 * 2onWebSocketEncodeData回调
*
* @param header * @param header
* @param data * @param buffer
* @param len
*/ */
void encode(const WebSocketHeader &header,uint8_t *data,const uint64_t len); void encode(const WebSocketHeader &header,const Buffer::Ptr &buffer);
protected: protected:
/** /**
* webSocket数据包包头onWebSocketDecodePlayload回调 * webSocket数据包包头onWebSocketDecodePlayload回调
@ -119,7 +119,7 @@ protected:
* @param ptr * @param ptr
* @param len * @param len
*/ */
virtual void onWebSocketEncodeData(const uint8_t *ptr,uint64_t len){}; virtual void onWebSocketEncodeData(const Buffer::Ptr &buffer){};
private: private:
void onPlayloadData(uint8_t *data,uint64_t len); void onPlayloadData(uint8_t *data,uint64_t len);
private: private: