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
f53879cf
Commit
f53879cf
authored
Sep 26, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
收到确切的WebSocket数据时才创建业务逻辑对象
parent
44914681
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
25 行增加
和
29 行删除
+25
-29
src/Http/HttpSession.h
+25
-29
没有找到文件。
src/Http/HttpSession.h
查看文件 @
f53879cf
...
...
@@ -135,43 +135,22 @@ private:
template
<
typename
SessionType
>
class
WebSocketSession
:
public
HttpSession
{
public
:
WebSocketSession
(
const
std
::
shared_ptr
<
ThreadPool
>
&
pTh
,
const
Socket
::
Ptr
&
pSock
)
:
HttpSession
(
pTh
,
pSock
){
_session
=
std
::
make_shared
<
SessionImp
>
(
pTh
,
pSock
);
}
virtual
~
WebSocketSession
(){};
WebSocketSession
(
const
std
::
shared_ptr
<
ThreadPool
>
&
pTh
,
const
Socket
::
Ptr
&
pSock
)
:
HttpSession
(
pTh
,
pSock
){}
virtual
~
WebSocketSession
(){}
//收到eof或其他导致脱离TcpServer事件的回调
void
onError
(
const
SockException
&
err
)
override
{
HttpSession
::
onError
(
err
);
_session
->
onError
(
err
);
if
(
_session
){
_session
->
onError
(
err
);
}
}
//每隔一段时间触发,用来做超时管理
void
onManager
()
override
{
HttpSession
::
onManager
();
_session
->
onManager
();
}
//在创建TcpSession后,TcpServer会把自身的配置参数通过该函数传递给TcpSession
void
attachServer
(
const
TcpServer
&
server
)
override
{
HttpSession
::
attachServer
(
server
);
_session
->
attachServer
(
server
);
//此处截取数据并进行websocket协议打包
weak_ptr
<
WebSocketSession
>
weakSelf
=
dynamic_pointer_cast
<
WebSocketSession
>
(
shared_from_this
());
_session
->
setOnBeforeSendCB
([
weakSelf
](
const
Buffer
::
Ptr
&
buf
){
auto
strongSelf
=
weakSelf
.
lock
();
if
(
strongSelf
){
bool
mask_flag
=
strongSelf
->
_mask_flag
;
strongSelf
->
_mask_flag
=
false
;
strongSelf
->
WebSocketSplitter
::
encode
((
uint8_t
*
)
buf
->
data
(),
buf
->
size
());
strongSelf
->
_mask_flag
=
mask_flag
;
}
return
buf
->
size
();
});
}
//作为该TcpSession的唯一标识符
string
getIdentifier
()
const
override
{
return
_session
->
getIdentifier
();
if
(
_session
){
_session
->
onManager
();
}
}
protected
:
/**
...
...
@@ -181,6 +160,22 @@ protected:
void
onWebSocketDecodeHeader
(
const
WebSocketHeader
&
packet
)
override
{
//新包,原来的包残余数据清空掉
_remian_data
.
clear
();
if
(
_firstPacket
){
_firstPacket
=
false
;
_session
=
std
::
make_shared
<
SessionImp
>
(
nullptr
,
_sock
);
//此处截取数据并进行websocket协议打包
weak_ptr
<
WebSocketSession
>
weakSelf
=
dynamic_pointer_cast
<
WebSocketSession
>
(
shared_from_this
());
_session
->
setOnBeforeSendCB
([
weakSelf
](
const
Buffer
::
Ptr
&
buf
){
auto
strongSelf
=
weakSelf
.
lock
();
if
(
strongSelf
){
bool
mask_flag
=
strongSelf
->
_mask_flag
;
strongSelf
->
_mask_flag
=
false
;
strongSelf
->
WebSocketSplitter
::
encode
((
uint8_t
*
)
buf
->
data
(),
buf
->
size
());
strongSelf
->
_mask_flag
=
mask_flag
;
}
return
buf
->
size
();
});
}
}
/**
...
...
@@ -280,6 +275,7 @@ private:
private
:
std
::
shared_ptr
<
SessionImp
>
_session
;
string
_remian_data
;
bool
_firstPacket
=
true
;
};
/**
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论