Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
453994f2
Commit
453994f2
authored
Aug 13, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
减少内存拷贝,提高websocket服务器性能
parent
0931d82a
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
24 行增加
和
27 行删除
+24
-27
src/Http/HttpSession.cpp
+4
-4
src/Http/HttpSession.h
+2
-3
src/Http/WebSocketSession.h
+7
-9
src/Http/WebSocketSplitter.cpp
+5
-5
src/Http/WebSocketSplitter.h
+6
-6
没有找到文件。
src/Http/HttpSession.cpp
查看文件 @
453994f2
...
@@ -954,12 +954,12 @@ void HttpSession::onWrite(const Buffer::Ptr &buffer) {
...
@@ -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
()
{
...
...
src/Http/HttpSession.h
查看文件 @
453994f2
...
@@ -104,10 +104,9 @@ protected:
...
@@ -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
);
...
...
src/Http/WebSocketSession.h
查看文件 @
453994f2
...
@@ -89,7 +89,7 @@ protected:
...
@@ -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:
...
@@ -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:
...
@@ -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:
...
@@ -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
;
...
...
src/Http/WebSocketSplitter.cpp
查看文件 @
453994f2
...
@@ -164,9 +164,9 @@ void WebSocketSplitter::onPlayloadData(uint8_t *ptr, uint64_t len) {
...
@@ -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
...
@@ -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
);
}
}
}
}
...
...
src/Http/WebSocketSplitter.h
查看文件 @
453994f2
...
@@ -31,8 +31,10 @@
...
@@ -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:
...
@@ -85,12 +87,10 @@ public:
/**
/**
* 编码一个数据包
* 编码一个数据包
* 将触发2次onWebSocketEncodeData回调
* 将触发2次onWebSocketEncodeData回调
* 第一次是数据头,第二次是负载数据
* @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:
...
@@ -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
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论