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
68948288
Commit
68948288
authored
Nov 19, 2022
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TcpSession/UdpSession统一为Session类
parent
47530ce8
隐藏空白字符变更
内嵌
并排
正在显示
24 个修改的文件
包含
93 行增加
和
93 行删除
+93
-93
3rdpart/ZLToolKit
+1
-1
api/source/mk_common.cpp
+3
-3
api/source/mk_tcp.cpp
+21
-21
api/source/mk_tcp_private.h
+4
-4
api/source/mk_thread.cpp
+1
-1
postman/ZLMediaKit.postman_collection.json
+1
-1
server/WebApi.cpp
+1
-1
server/WebHook.cpp
+1
-1
src/Common/MediaSource.cpp
+1
-1
src/Http/HttpFileManager.cpp
+6
-6
src/Http/HttpFileManager.h
+2
-2
src/Http/HttpSession.cpp
+3
-3
src/Http/HttpSession.h
+3
-3
src/Http/WebSocketSession.h
+17
-17
src/Rtmp/RtmpSession.cpp
+1
-1
src/Rtmp/RtmpSession.h
+3
-3
src/Rtp/RtpSession.h
+1
-1
src/Rtsp/RtspSession.cpp
+2
-2
src/Rtsp/RtspSession.h
+5
-5
src/Shell/ShellSession.cpp
+1
-1
src/Shell/ShellSession.h
+2
-2
srt/SrtSession.cpp
+1
-1
srt/SrtSession.hpp
+1
-1
tests/test_wsServer.cpp
+11
-11
没有找到文件。
ZLToolKit
@
617b6b1d
Subproject commit
894be81929f227583081755288ab0927c077e411
Subproject commit
617b6b1db23f13e2592b29204b84b1b9dbbf81c0
api/source/mk_common.cpp
查看文件 @
68948288
...
...
@@ -174,7 +174,7 @@ API_EXPORT uint16_t API_CALL mk_http_server_start(uint16_t port, int ssl) {
try
{
http_server
[
ssl
]
=
std
::
make_shared
<
TcpServer
>
();
if
(
ssl
){
http_server
[
ssl
]
->
start
<
Tcp
SessionWithSSL
<
HttpSession
>
>
(
port
);
http_server
[
ssl
]
->
start
<
SessionWithSSL
<
HttpSession
>
>
(
port
);
}
else
{
http_server
[
ssl
]
->
start
<
HttpSession
>
(
port
);
}
...
...
@@ -191,7 +191,7 @@ API_EXPORT uint16_t API_CALL mk_rtsp_server_start(uint16_t port, int ssl) {
try
{
rtsp_server
[
ssl
]
=
std
::
make_shared
<
TcpServer
>
();
if
(
ssl
){
rtsp_server
[
ssl
]
->
start
<
Tcp
SessionWithSSL
<
RtspSession
>
>
(
port
);
rtsp_server
[
ssl
]
->
start
<
SessionWithSSL
<
RtspSession
>
>
(
port
);
}
else
{
rtsp_server
[
ssl
]
->
start
<
RtspSession
>
(
port
);
}
...
...
@@ -208,7 +208,7 @@ API_EXPORT uint16_t API_CALL mk_rtmp_server_start(uint16_t port, int ssl) {
try
{
rtmp_server
[
ssl
]
=
std
::
make_shared
<
TcpServer
>
();
if
(
ssl
){
rtmp_server
[
ssl
]
->
start
<
Tcp
SessionWithSSL
<
RtmpSession
>
>
(
port
);
rtmp_server
[
ssl
]
->
start
<
SessionWithSSL
<
RtmpSession
>
>
(
port
);
}
else
{
rtmp_server
[
ssl
]
->
start
<
RtmpSession
>
(
port
);
}
...
...
api/source/mk_tcp.cpp
查看文件 @
68948288
...
...
@@ -109,19 +109,19 @@ API_EXPORT uint16_t API_CALL mk_sock_info_local_port(const mk_sock_info ctx){
////////////////////////////////////////////////////////////////////////////////////////
API_EXPORT
mk_sock_info
API_CALL
mk_tcp_session_get_sock_info
(
const
mk_tcp_session
ctx
){
assert
(
ctx
);
TcpSessionForC
*
session
=
(
Tcp
SessionForC
*
)
ctx
;
SessionForC
*
session
=
(
SessionForC
*
)
ctx
;
return
(
SockInfo
*
)
session
;
}
API_EXPORT
void
API_CALL
mk_tcp_session_shutdown
(
const
mk_tcp_session
ctx
,
int
err
,
const
char
*
err_msg
){
assert
(
ctx
);
TcpSessionForC
*
session
=
(
Tcp
SessionForC
*
)
ctx
;
SessionForC
*
session
=
(
SessionForC
*
)
ctx
;
session
->
safeShutdown
(
SockException
((
ErrCode
)
err
,
err_msg
));
}
API_EXPORT
void
API_CALL
mk_tcp_session_send_buffer
(
const
mk_tcp_session
ctx
,
mk_buffer
buffer
)
{
assert
(
ctx
&&
buffer
);
TcpSessionForC
*
session
=
(
Tcp
SessionForC
*
)
ctx
;
SessionForC
*
session
=
(
SessionForC
*
)
ctx
;
session
->
send
(
*
((
Buffer
::
Ptr
*
)
buffer
));
}
...
...
@@ -134,9 +134,9 @@ API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx, const cha
API_EXPORT
void
API_CALL
mk_tcp_session_send_buffer_safe
(
const
mk_tcp_session
ctx
,
mk_buffer
buffer
)
{
assert
(
ctx
&&
buffer
);
try
{
std
::
weak_ptr
<
TcpSession
>
weak_session
=
((
Tcp
SessionForC
*
)
ctx
)
->
shared_from_this
();
std
::
weak_ptr
<
Session
>
weak_session
=
((
SessionForC
*
)
ctx
)
->
shared_from_this
();
auto
ref
=
mk_buffer_ref
(
buffer
);
((
Tcp
SessionForC
*
)
ctx
)
->
async
([
weak_session
,
ref
]()
{
((
SessionForC
*
)
ctx
)
->
async
([
weak_session
,
ref
]()
{
auto
session_session
=
weak_session
.
lock
();
if
(
session_session
)
{
session_session
->
send
(
*
((
Buffer
::
Ptr
*
)
ref
));
...
...
@@ -149,16 +149,16 @@ API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ct
}
API_EXPORT
mk_tcp_session_ref
API_CALL
mk_tcp_session_ref_from
(
const
mk_tcp_session
ctx
)
{
auto
ref
=
((
Tcp
SessionForC
*
)
ctx
)
->
shared_from_this
();
return
new
std
::
shared_ptr
<
TcpSessionForC
>
(
std
::
dynamic_pointer_cast
<
Tcp
SessionForC
>
(
ref
));
auto
ref
=
((
SessionForC
*
)
ctx
)
->
shared_from_this
();
return
new
std
::
shared_ptr
<
SessionForC
>
(
std
::
dynamic_pointer_cast
<
SessionForC
>
(
ref
));
}
API_EXPORT
void
mk_tcp_session_ref_release
(
const
mk_tcp_session_ref
ref
)
{
delete
(
std
::
shared_ptr
<
Tcp
SessionForC
>
*
)
ref
;
delete
(
std
::
shared_ptr
<
SessionForC
>
*
)
ref
;
}
API_EXPORT
mk_tcp_session
mk_tcp_session_from_ref
(
const
mk_tcp_session_ref
ref
)
{
return
((
std
::
shared_ptr
<
Tcp
SessionForC
>
*
)
ref
)
->
get
();
return
((
std
::
shared_ptr
<
SessionForC
>
*
)
ref
)
->
get
();
}
API_EXPORT
void
API_CALL
mk_tcp_session_send_safe
(
const
mk_tcp_session
ctx
,
const
char
*
data
,
size_t
len
)
{
...
...
@@ -167,30 +167,30 @@ API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx, cons
mk_buffer_unref
(
buffer
);
}
////////////////////////////////////////
Tcp
SessionForC////////////////////////////////////////////////
////////////////////////////////////////SessionForC////////////////////////////////////////////////
static
TcpServer
::
Ptr
s_tcp_server
[
4
];
static
mk_tcp_session_events
s_events_server
=
{
0
};
TcpSessionForC
::
TcpSessionForC
(
const
Socket
::
Ptr
&
pSock
)
:
Tcp
Session
(
pSock
)
{
SessionForC
::
SessionForC
(
const
Socket
::
Ptr
&
pSock
)
:
Session
(
pSock
)
{
_local_port
=
get_local_port
();
if
(
s_events_server
.
on_mk_tcp_session_create
)
{
s_events_server
.
on_mk_tcp_session_create
(
_local_port
,
this
);
}
}
void
Tcp
SessionForC
::
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
{
void
SessionForC
::
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
{
if
(
s_events_server
.
on_mk_tcp_session_data
)
{
s_events_server
.
on_mk_tcp_session_data
(
_local_port
,
this
,
(
mk_buffer
)
&
buffer
);
}
}
void
Tcp
SessionForC
::
onError
(
const
SockException
&
err
)
{
void
SessionForC
::
onError
(
const
SockException
&
err
)
{
if
(
s_events_server
.
on_mk_tcp_session_disconnect
)
{
s_events_server
.
on_mk_tcp_session_disconnect
(
_local_port
,
this
,
err
.
getErrCode
(),
err
.
what
());
}
}
void
Tcp
SessionForC
::
onManager
()
{
void
SessionForC
::
onManager
()
{
if
(
s_events_server
.
on_mk_tcp_session_manager
)
{
s_events_server
.
on_mk_tcp_session_manager
(
_local_port
,
this
);
}
...
...
@@ -202,13 +202,13 @@ void stopAllTcpServer(){
API_EXPORT
void
API_CALL
mk_tcp_session_set_user_data
(
mk_tcp_session
session
,
void
*
user_data
){
assert
(
session
);
TcpSessionForC
*
obj
=
(
Tcp
SessionForC
*
)
session
;
SessionForC
*
obj
=
(
SessionForC
*
)
session
;
obj
->
_user_data
=
user_data
;
}
API_EXPORT
void
*
API_CALL
mk_tcp_session_get_user_data
(
mk_tcp_session
session
){
assert
(
session
);
TcpSessionForC
*
obj
=
(
Tcp
SessionForC
*
)
session
;
SessionForC
*
obj
=
(
SessionForC
*
)
session
;
return
obj
->
_user_data
;
}
...
...
@@ -226,18 +226,18 @@ API_EXPORT uint16_t API_CALL mk_tcp_server_start(uint16_t port, mk_tcp_type type
s_tcp_server
[
type
]
=
std
::
make_shared
<
TcpServer
>
();
switch
(
type
)
{
case
mk_type_tcp
:
s_tcp_server
[
type
]
->
start
<
Tcp
SessionForC
>
(
port
);
s_tcp_server
[
type
]
->
start
<
SessionForC
>
(
port
);
break
;
case
mk_type_ssl
:
s_tcp_server
[
type
]
->
start
<
TcpSessionWithSSL
<
Tcp
SessionForC
>
>
(
port
);
s_tcp_server
[
type
]
->
start
<
SessionWithSSL
<
SessionForC
>
>
(
port
);
break
;
case
mk_type_ws
:
//此处你也可以修改WebSocketHeader::BINARY
s_tcp_server
[
type
]
->
start
<
WebSocketSession
<
Tcp
SessionForC
,
HttpSession
,
WebSocketHeader
::
TEXT
>
>
(
port
);
s_tcp_server
[
type
]
->
start
<
WebSocketSession
<
SessionForC
,
HttpSession
,
WebSocketHeader
::
TEXT
>
>
(
port
);
break
;
case
mk_type_wss
:
//此处你也可以修改WebSocketHeader::BINARY
s_tcp_server
[
type
]
->
start
<
WebSocketSession
<
Tcp
SessionForC
,
HttpsSession
,
WebSocketHeader
::
TEXT
>
>
(
port
);
s_tcp_server
[
type
]
->
start
<
WebSocketSession
<
SessionForC
,
HttpsSession
,
WebSocketHeader
::
TEXT
>
>
(
port
);
break
;
default
:
return
0
;
...
...
@@ -295,7 +295,7 @@ TcpClientForC::Ptr *mk_tcp_client_create_l(mk_tcp_client_events *events, mk_tcp_
case
mk_type_tcp
:
return
new
TcpClientForC
::
Ptr
(
new
TcpClientForC
(
events
));
case
mk_type_ssl
:
return
(
TcpClientForC
::
Ptr
*
)
new
std
::
shared_ptr
<
TcpSessionWithSSL
<
TcpClientForC
>
>
(
new
Tcp
SessionWithSSL
<
TcpClientForC
>
(
events
));
return
(
TcpClientForC
::
Ptr
*
)
new
std
::
shared_ptr
<
SessionWithSSL
<
TcpClientForC
>
>
(
new
SessionWithSSL
<
TcpClientForC
>
(
events
));
case
mk_type_ws
:
//此处你也可以修改WebSocketHeader::BINARY
return
(
TcpClientForC
::
Ptr
*
)
new
std
::
shared_ptr
<
WebSocketClient
<
TcpClientForC
,
WebSocketHeader
::
TEXT
,
false
>
>
(
new
WebSocketClient
<
TcpClientForC
,
WebSocketHeader
::
TEXT
,
false
>
(
events
));
...
...
api/source/mk_tcp_private.h
查看文件 @
68948288
...
...
@@ -13,7 +13,7 @@
#include "mk_tcp.h"
#include "Network/TcpClient.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
class
TcpClientForC
:
public
toolkit
::
TcpClient
{
public
:
...
...
@@ -31,10 +31,10 @@ private:
mk_tcp_client
_client
;
};
class
TcpSessionForC
:
public
toolkit
::
Tcp
Session
{
class
SessionForC
:
public
toolkit
::
Session
{
public
:
Tcp
SessionForC
(
const
toolkit
::
Socket
::
Ptr
&
pSock
)
;
~
Tcp
SessionForC
()
override
=
default
;
SessionForC
(
const
toolkit
::
Socket
::
Ptr
&
pSock
)
;
~
SessionForC
()
override
=
default
;
void
onRecv
(
const
toolkit
::
Buffer
::
Ptr
&
buffer
)
override
;
void
onError
(
const
toolkit
::
SockException
&
err
)
override
;
void
onManager
()
override
;
...
...
api/source/mk_thread.cpp
查看文件 @
68948288
...
...
@@ -18,7 +18,7 @@ using namespace toolkit;
API_EXPORT
mk_thread
API_CALL
mk_thread_from_tcp_session
(
mk_tcp_session
ctx
){
assert
(
ctx
);
TcpSessionForC
*
obj
=
(
Tcp
SessionForC
*
)
ctx
;
SessionForC
*
obj
=
(
SessionForC
*
)
ctx
;
return
obj
->
getPoller
().
get
();
}
...
...
postman/ZLMediaKit.postman_collection.json
查看文件 @
68948288
...
...
@@ -348,7 +348,7 @@
"response"
:
[]
},
{
"name"
:
"获取
Tcp
Session列表(getAllSession)"
,
"name"
:
"获取Session列表(getAllSession)"
,
"request"
:
{
"method"
:
"GET"
,
"header"
:
[],
...
...
server/WebApi.cpp
查看文件 @
68948288
...
...
@@ -854,7 +854,7 @@ void installWebApi() {
val
[
"count_closed"
]
=
count_closed
;
});
//获取所有
Tcp
Session列表信息
//获取所有Session列表信息
//可以根据本地端口和远端ip来筛选
//测试url(筛选某端口下的tcp会话) http://127.0.0.1/index/api/getAllSession?local_port=1935
api_regist
(
"/index/api/getAllSession"
,[](
API_ARGS_MAP
){
...
...
server/WebHook.cpp
查看文件 @
68948288
...
...
@@ -15,7 +15,7 @@
#include "Common/config.h"
#include "Common/MediaSource.h"
#include "Http/HttpRequester.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "Rtsp/RtspSession.h"
#include "Http/HttpSession.h"
#include "WebHook.h"
...
...
src/Common/MediaSource.cpp
查看文件 @
68948288
...
...
@@ -11,7 +11,7 @@
#include "Util/util.h"
#include "Util/NoticeCenter.h"
#include "Network/sockutil.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "MediaSource.h"
#include "Common/config.h"
#include "Record/MP4Reader.h"
...
...
src/Http/HttpFileManager.cpp
查看文件 @
68948288
...
...
@@ -181,7 +181,7 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
}
//拦截hls的播放请求
static
bool
emitHlsPlayed
(
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
const
HttpSession
::
HttpAccessPathInvoker
&
invoker
,
Tcp
Session
&
sender
){
static
bool
emitHlsPlayed
(
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
const
HttpSession
::
HttpAccessPathInvoker
&
invoker
,
Session
&
sender
){
//访问的hls.m3u8结尾,我们转换成kBroadcastMediaPlayed事件
Broadcast
::
AuthInvoker
auth_invoker
=
[
invoker
](
const
string
&
err
)
{
//cookie有效期为kHlsCookieSecond
...
...
@@ -236,7 +236,7 @@ public:
* 4、cookie中记录的url参数是否跟本次url参数一致,如果一致直接返回客户端错误码
* 5、触发kBroadcastHttpAccess事件
*/
static
void
canAccessPath
(
Tcp
Session
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
bool
is_dir
,
static
void
canAccessPath
(
Session
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
bool
is_dir
,
const
function
<
void
(
const
string
&
err_msg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
>
&
callback
)
{
//获取用户唯一id
auto
uid
=
parser
.
Params
();
...
...
@@ -353,7 +353,7 @@ static string pathCat(const string &a, const string &b){
* @param file_path 文件绝对路径
* @param cb 回调对象
*/
static
void
accessFile
(
Tcp
Session
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
const
string
&
file_path
,
const
HttpFileManager
::
invoker
&
cb
)
{
static
void
accessFile
(
Session
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
const
string
&
file_path
,
const
HttpFileManager
::
invoker
&
cb
)
{
bool
is_hls
=
end_with
(
file_path
,
kHlsSuffix
);
if
(
!
is_hls
&&
!
File
::
fileExist
(
file_path
.
data
()))
{
//文件不存在且不是hls,那么直接返回404
...
...
@@ -366,7 +366,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
replace
(
const_cast
<
string
&>
(
media_info
.
_streamid
),
kHlsSuffix
,
""
);
}
weak_ptr
<
Tcp
Session
>
weakSession
=
sender
.
shared_from_this
();
weak_ptr
<
Session
>
weakSession
=
sender
.
shared_from_this
();
//判断是否有权限访问该文件
canAccessPath
(
sender
,
parser
,
media_info
,
false
,
[
cb
,
file_path
,
parser
,
is_hls
,
media_info
,
weakSession
](
const
string
&
err_msg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
{
auto
strongSession
=
weakSession
.
lock
();
...
...
@@ -456,7 +456,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
});
}
static
string
getFilePath
(
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
Tcp
Session
&
sender
){
static
string
getFilePath
(
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
Session
&
sender
){
GET_CONFIG
(
bool
,
enableVhost
,
General
::
kEnableVhost
);
GET_CONFIG
(
string
,
rootPath
,
Http
::
kRootPath
);
GET_CONFIG_FUNC
(
StrCaseMap
,
virtualPathMap
,
Http
::
kVirtualPath
,
[](
const
string
&
str
)
{
...
...
@@ -491,7 +491,7 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, TcpS
* @param parser http请求
* @param cb 回调对象
*/
void
HttpFileManager
::
onAccessPath
(
Tcp
Session
&
sender
,
Parser
&
parser
,
const
HttpFileManager
::
invoker
&
cb
)
{
void
HttpFileManager
::
onAccessPath
(
Session
&
sender
,
Parser
&
parser
,
const
HttpFileManager
::
invoker
&
cb
)
{
auto
fullUrl
=
string
(
HTTP_SCHEMA
)
+
"://"
+
parser
[
"Host"
]
+
parser
.
FullUrl
();
MediaInfo
media_info
(
fullUrl
);
auto
file_path
=
getFilePath
(
parser
,
media_info
,
sender
);
...
...
src/Http/HttpFileManager.h
查看文件 @
68948288
...
...
@@ -14,7 +14,7 @@
#include "HttpBody.h"
#include "HttpCookie.h"
#include "Common/Parser.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "Util/function_traits.h"
namespace
mediakit
{
...
...
@@ -54,7 +54,7 @@ public:
* @param parser http请求
* @param cb 回调对象
*/
static
void
onAccessPath
(
toolkit
::
Tcp
Session
&
sender
,
Parser
&
parser
,
const
invoker
&
cb
);
static
void
onAccessPath
(
toolkit
::
Session
&
sender
,
Parser
&
parser
,
const
invoker
&
cb
);
/**
* 获取mime值
...
...
src/Http/HttpSession.cpp
查看文件 @
68948288
...
...
@@ -23,7 +23,7 @@ using namespace toolkit;
namespace
mediakit
{
HttpSession
::
HttpSession
(
const
Socket
::
Ptr
&
pSock
)
:
Tcp
Session
(
pSock
)
{
HttpSession
::
HttpSession
(
const
Socket
::
Ptr
&
pSock
)
:
Session
(
pSock
)
{
TraceP
(
this
);
GET_CONFIG
(
uint32_t
,
keep_alive_sec
,
Http
::
kKeepAliveSecond
);
pSock
->
setSendTimeOutSecond
(
keep_alive_sec
);
...
...
@@ -440,7 +440,7 @@ class AsyncSenderData {
public
:
friend
class
AsyncSender
;
typedef
std
::
shared_ptr
<
AsyncSenderData
>
Ptr
;
AsyncSenderData
(
const
Tcp
Session
::
Ptr
&
session
,
const
HttpBody
::
Ptr
&
body
,
bool
close_when_complete
)
{
AsyncSenderData
(
const
Session
::
Ptr
&
session
,
const
HttpBody
::
Ptr
&
body
,
bool
close_when_complete
)
{
_session
=
dynamic_pointer_cast
<
HttpSession
>
(
session
);
_body
=
body
;
_close_when_complete
=
close_when_complete
;
...
...
@@ -675,7 +675,7 @@ std::string HttpSession::get_peer_ip() {
if
(
!
forwarded_ip_header
.
empty
()
&&
!
_parser
.
getHeader
()[
forwarded_ip_header
].
empty
()){
return
_parser
.
getHeader
()[
forwarded_ip_header
];
}
return
Tcp
Session
::
get_peer_ip
();
return
Session
::
get_peer_ip
();
}
void
HttpSession
::
Handle_Req_POST
(
ssize_t
&
content_len
)
{
...
...
src/Http/HttpSession.h
查看文件 @
68948288
...
...
@@ -12,7 +12,7 @@
#define SRC_HTTP_HTTPSESSION_H_
#include <functional>
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "Rtmp/RtmpMediaSource.h"
#include "Rtmp/FlvMuxer.h"
#include "HttpRequestSplitter.h"
...
...
@@ -24,7 +24,7 @@
namespace
mediakit
{
class
HttpSession
:
public
toolkit
::
Tcp
Session
,
class
HttpSession
:
public
toolkit
::
Session
,
public
FlvMuxer
,
public
HttpRequestSplitter
,
public
WebSocketSplitter
{
...
...
@@ -139,7 +139,7 @@ private:
std
::
function
<
bool
(
const
char
*
data
,
size_t
len
)
>
_contentCallBack
;
};
using
HttpsSession
=
toolkit
::
Tcp
SessionWithSSL
<
HttpSession
>
;
using
HttpsSession
=
toolkit
::
SessionWithSSL
<
HttpSession
>
;
}
/* namespace mediakit */
...
...
src/Http/WebSocketSession.h
查看文件 @
68948288
...
...
@@ -27,18 +27,18 @@ public:
};
/**
* 该类实现了
Tcp
Session派生类发送数据的截取
* 该类实现了Session派生类发送数据的截取
* 目的是发送业务数据前进行websocket协议的打包
*/
template
<
typename
Tcp
SessionType
>
class
TcpSessionTypeImp
:
public
Tcp
SessionType
,
public
SendInterceptor
{
template
<
typename
SessionType
>
class
SessionTypeImp
:
public
SessionType
,
public
SendInterceptor
{
public
:
typedef
std
::
shared_ptr
<
TcpSessionTypeImp
>
Ptr
;
using
Ptr
=
std
::
shared_ptr
<
SessionTypeImp
>
;
Tcp
SessionTypeImp
(
const
mediakit
::
Parser
&
header
,
const
mediakit
::
HttpSession
&
parent
,
const
toolkit
::
Socket
::
Ptr
&
pSock
)
:
Tcp
SessionType
(
pSock
),
_identifier
(
parent
.
getIdentifier
())
{}
SessionTypeImp
(
const
mediakit
::
Parser
&
header
,
const
mediakit
::
HttpSession
&
parent
,
const
toolkit
::
Socket
::
Ptr
&
pSock
)
:
SessionType
(
pSock
),
_identifier
(
parent
.
getIdentifier
())
{}
~
TcpSessionTypeImp
()
{}
~
SessionTypeImp
()
=
default
;
/**
* 设置发送数据截取回调函数
...
...
@@ -58,7 +58,7 @@ protected:
if
(
_beforeSendCB
)
{
return
_beforeSendCB
(
buf
);
}
return
Tcp
SessionType
::
send
(
std
::
move
(
buf
));
return
SessionType
::
send
(
std
::
move
(
buf
));
}
std
::
string
getIdentifier
()
const
override
{
...
...
@@ -70,12 +70,12 @@ private:
onBeforeSendCB
_beforeSendCB
;
};
template
<
typename
Tcp
SessionType
>
class
Tcp
SessionCreator
{
template
<
typename
SessionType
>
class
SessionCreator
{
public
:
//返回的
Tcp
Session必须派生于SendInterceptor,可以返回null
toolkit
::
Tcp
Session
::
Ptr
operator
()(
const
mediakit
::
Parser
&
header
,
const
mediakit
::
HttpSession
&
parent
,
const
toolkit
::
Socket
::
Ptr
&
pSock
){
return
std
::
make_shared
<
TcpSessionTypeImp
<
Tcp
SessionType
>
>
(
header
,
parent
,
pSock
);
//返回的Session必须派生于SendInterceptor,可以返回null
toolkit
::
Session
::
Ptr
operator
()(
const
mediakit
::
Parser
&
header
,
const
mediakit
::
HttpSession
&
parent
,
const
toolkit
::
Socket
::
Ptr
&
pSock
){
return
std
::
make_shared
<
SessionTypeImp
<
SessionType
>
>
(
header
,
parent
,
pSock
);
}
};
...
...
@@ -228,15 +228,15 @@ private:
std
::
string
_payload_cache
;
std
::
string
_payload_section
;
std
::
weak_ptr
<
toolkit
::
Server
>
_weak_server
;
toolkit
::
Tcp
Session
::
Ptr
_session
;
toolkit
::
Session
::
Ptr
_session
;
Creator
_creator
;
};
template
<
typename
Tcp
SessionType
,
typename
HttpSessionType
=
mediakit
::
HttpSession
,
mediakit
::
WebSocketHeader
::
Type
DataType
=
mediakit
::
WebSocketHeader
::
TEXT
>
class
WebSocketSession
:
public
WebSocketSessionBase
<
TcpSessionCreator
<
Tcp
SessionType
>
,
HttpSessionType
,
DataType
>
{
template
<
typename
SessionType
,
typename
HttpSessionType
=
mediakit
::
HttpSession
,
mediakit
::
WebSocketHeader
::
Type
DataType
=
mediakit
::
WebSocketHeader
::
TEXT
>
class
WebSocketSession
:
public
WebSocketSessionBase
<
SessionCreator
<
SessionType
>
,
HttpSessionType
,
DataType
>
{
public
:
WebSocketSession
(
const
toolkit
::
Socket
::
Ptr
&
pSock
)
:
WebSocketSessionBase
<
TcpSessionCreator
<
Tcp
SessionType
>
,
HttpSessionType
,
DataType
>
(
pSock
){}
WebSocketSession
(
const
toolkit
::
Socket
::
Ptr
&
pSock
)
:
WebSocketSessionBase
<
SessionCreator
<
SessionType
>
,
HttpSessionType
,
DataType
>
(
pSock
){}
virtual
~
WebSocketSession
(){}
};
...
...
src/Rtmp/RtmpSession.cpp
查看文件 @
68948288
...
...
@@ -17,7 +17,7 @@ using namespace toolkit;
namespace
mediakit
{
RtmpSession
::
RtmpSession
(
const
Socket
::
Ptr
&
sock
)
:
Tcp
Session
(
sock
)
{
RtmpSession
::
RtmpSession
(
const
Socket
::
Ptr
&
sock
)
:
Session
(
sock
)
{
DebugP
(
this
);
GET_CONFIG
(
uint32_t
,
keep_alive_sec
,
Rtmp
::
kKeepAliveSecond
);
sock
->
setSendTimeOutSecond
(
keep_alive_sec
);
...
...
src/Rtmp/RtmpSession.h
查看文件 @
68948288
...
...
@@ -20,12 +20,12 @@
#include "RtmpMediaSourceImp.h"
#include "Util/util.h"
#include "Util/TimeTicker.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "Common/Stamp.h"
namespace
mediakit
{
class
RtmpSession
:
public
toolkit
::
Tcp
Session
,
public
RtmpProtocol
,
public
MediaSourceEvent
{
class
RtmpSession
:
public
toolkit
::
Session
,
public
RtmpProtocol
,
public
MediaSourceEvent
{
public
:
using
Ptr
=
std
::
shared_ptr
<
RtmpSession
>
;
...
...
@@ -109,7 +109,7 @@ private:
/**
* 支持ssl加密的rtmp服务器
*/
using
RtmpSessionWithSSL
=
toolkit
::
Tcp
SessionWithSSL
<
RtmpSession
>
;
using
RtmpSessionWithSSL
=
toolkit
::
SessionWithSSL
<
RtmpSession
>
;
}
/* namespace mediakit */
#endif
/* SRC_RTMP_RTMPSESSION_H_ */
src/Rtp/RtpSession.h
查看文件 @
68948288
...
...
@@ -13,7 +13,7 @@
#if defined(ENABLE_RTPPROXY)
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "RtpSplitter.h"
#include "RtpProcess.h"
#include "Util/TimeTicker.h"
...
...
src/Rtsp/RtspSession.cpp
查看文件 @
68948288
...
...
@@ -49,7 +49,7 @@ static unordered_map<string, weak_ptr<RtspSession> > g_mapGetter;
//对g_mapGetter上锁保护
static
recursive_mutex
g_mtxGetter
;
RtspSession
::
RtspSession
(
const
Socket
::
Ptr
&
sock
)
:
Tcp
Session
(
sock
)
{
RtspSession
::
RtspSession
(
const
Socket
::
Ptr
&
sock
)
:
Session
(
sock
)
{
DebugP
(
this
);
GET_CONFIG
(
uint32_t
,
keep_alive_sec
,
Rtsp
::
kKeepAliveSecond
);
sock
->
setSendTimeOutSecond
(
keep_alive_sec
);
...
...
@@ -1078,7 +1078,7 @@ ssize_t RtspSession::send(Buffer::Ptr pkt){
// DebugP(this) << pkt->data();
// }
_bytes_usage
+=
pkt
->
size
();
return
Tcp
Session
::
send
(
std
::
move
(
pkt
));
return
Session
::
send
(
std
::
move
(
pkt
));
}
bool
RtspSession
::
sendRtspResponse
(
const
string
&
res_code
,
const
std
::
initializer_list
<
string
>
&
header
,
const
string
&
sdp
,
const
char
*
protocol
)
{
...
...
src/Rtsp/RtspSession.h
查看文件 @
68948288
...
...
@@ -18,7 +18,7 @@
#include "Util/util.h"
#include "Util/logger.h"
#include "Common/config.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
#include "Player/PlayerBase.h"
#include "RtpMultiCaster.h"
#include "RtspMediaSource.h"
...
...
@@ -52,7 +52,7 @@ private:
Buffer
::
Ptr
_rtp
;
};
class
RtspSession
:
public
toolkit
::
Tcp
Session
,
public
RtspSplitter
,
public
RtpReceiver
,
public
MediaSourceEvent
{
class
RtspSession
:
public
toolkit
::
Session
,
public
RtspSplitter
,
public
RtpReceiver
,
public
MediaSourceEvent
{
public
:
using
Ptr
=
std
::
shared_ptr
<
RtspSession
>
;
using
onGetRealm
=
std
::
function
<
void
(
const
std
::
string
&
realm
)
>
;
...
...
@@ -62,7 +62,7 @@ public:
RtspSession
(
const
toolkit
::
Socket
::
Ptr
&
sock
);
virtual
~
RtspSession
();
////
Tcp
Session override////
////Session override////
void
onRecv
(
const
toolkit
::
Buffer
::
Ptr
&
buf
)
override
;
void
onError
(
const
toolkit
::
SockException
&
err
)
override
;
void
onManager
()
override
;
...
...
@@ -94,7 +94,7 @@ protected:
// 由于支持断连续推,存在OwnerPoller变更的可能
toolkit
::
EventPoller
::
Ptr
getOwnerPoller
(
MediaSource
&
sender
)
override
;
/////
Tcp
Session override////
/////Session override////
ssize_t
send
(
toolkit
::
Buffer
::
Ptr
pkt
)
override
;
//收到RTCP包回调
virtual
void
onRtcpPacket
(
int
track_idx
,
SdpTrack
::
Ptr
&
track
,
const
char
*
data
,
size_t
len
);
...
...
@@ -225,7 +225,7 @@ private:
/**
* 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
*/
using
RtspSessionWithSSL
=
toolkit
::
Tcp
SessionWithSSL
<
RtspSession
>
;
using
RtspSessionWithSSL
=
toolkit
::
SessionWithSSL
<
RtspSession
>
;
}
/* namespace mediakit */
...
...
src/Shell/ShellSession.cpp
查看文件 @
68948288
...
...
@@ -24,7 +24,7 @@ static onceToken s_token([]() {
REGIST_CMD
(
media
);
},
nullptr
);
ShellSession
::
ShellSession
(
const
Socket
::
Ptr
&
_sock
)
:
Tcp
Session
(
_sock
)
{
ShellSession
::
ShellSession
(
const
Socket
::
Ptr
&
_sock
)
:
Session
(
_sock
)
{
DebugP
(
this
);
pleaseInputUser
();
}
...
...
src/Shell/ShellSession.h
查看文件 @
68948288
...
...
@@ -14,11 +14,11 @@
#include <functional>
#include "Common/config.h"
#include "Util/TimeTicker.h"
#include "Network/
Tcp
Session.h"
#include "Network/Session.h"
namespace
mediakit
{
class
ShellSession
:
public
toolkit
::
Tcp
Session
{
class
ShellSession
:
public
toolkit
::
Session
{
public
:
ShellSession
(
const
toolkit
::
Socket
::
Ptr
&
_sock
);
virtual
~
ShellSession
();
...
...
srt/SrtSession.cpp
查看文件 @
68948288
...
...
@@ -8,7 +8,7 @@ namespace SRT {
using
namespace
mediakit
;
SrtSession
::
SrtSession
(
const
Socket
::
Ptr
&
sock
)
:
Udp
Session
(
sock
)
{
:
Session
(
sock
)
{
socklen_t
addr_len
=
sizeof
(
_peer_addr
);
memset
(
&
_peer_addr
,
0
,
addr_len
);
// TraceL<<"before addr len "<<addr_len;
...
...
srt/SrtSession.hpp
查看文件 @
68948288
...
...
@@ -8,7 +8,7 @@ namespace SRT {
using
namespace
toolkit
;
class
SrtSession
:
public
Udp
Session
{
class
SrtSession
:
public
Session
{
public
:
SrtSession
(
const
Socket
::
Ptr
&
sock
);
~
SrtSession
()
override
;
...
...
tests/test_wsServer.cpp
查看文件 @
68948288
...
...
@@ -22,9 +22,9 @@ using namespace mediakit;
/**
* 回显会话
*/
class
EchoSession
:
public
Tcp
Session
{
class
EchoSession
:
public
Session
{
public
:
EchoSession
(
const
Socket
::
Ptr
&
pSock
)
:
Tcp
Session
(
pSock
){
EchoSession
(
const
Socket
::
Ptr
&
pSock
)
:
Session
(
pSock
){
DebugL
;
}
virtual
~
EchoSession
(){
...
...
@@ -32,7 +32,7 @@ public:
}
void
attachServer
(
const
Server
&
server
)
override
{
DebugL
<<
getIdentifier
()
<<
" "
<<
Tcp
Session
::
getIdentifier
();
DebugL
<<
getIdentifier
()
<<
" "
<<
Session
::
getIdentifier
();
}
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
{
//回显数据
...
...
@@ -49,9 +49,9 @@ public:
};
class
EchoSessionWithUrl
:
public
Tcp
Session
{
class
EchoSessionWithUrl
:
public
Session
{
public
:
EchoSessionWithUrl
(
const
Socket
::
Ptr
&
pSock
)
:
Tcp
Session
(
pSock
){
EchoSessionWithUrl
(
const
Socket
::
Ptr
&
pSock
)
:
Session
(
pSock
){
DebugL
;
}
virtual
~
EchoSessionWithUrl
(){
...
...
@@ -59,7 +59,7 @@ public:
}
void
attachServer
(
const
Server
&
server
)
override
{
DebugL
<<
getIdentifier
()
<<
" "
<<
Tcp
Session
::
getIdentifier
();
DebugL
<<
getIdentifier
()
<<
" "
<<
Session
::
getIdentifier
();
}
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
{
//回显数据
...
...
@@ -80,13 +80,13 @@ public:
* 此对象可以根据websocket 客户端访问的url选择创建不同的对象
*/
struct
EchoSessionCreator
{
//返回的
Tcp
Session必须派生于SendInterceptor,可以返回null(拒绝连接)
Tcp
Session
::
Ptr
operator
()(
const
Parser
&
header
,
const
HttpSession
&
parent
,
const
Socket
::
Ptr
&
pSock
)
{
//返回的Session必须派生于SendInterceptor,可以返回null(拒绝连接)
Session
::
Ptr
operator
()(
const
Parser
&
header
,
const
HttpSession
&
parent
,
const
Socket
::
Ptr
&
pSock
)
{
// return nullptr;
if
(
header
.
Url
()
==
"/"
)
{
return
std
::
make_shared
<
Tcp
SessionTypeImp
<
EchoSession
>
>
(
header
,
parent
,
pSock
);
return
std
::
make_shared
<
SessionTypeImp
<
EchoSession
>
>
(
header
,
parent
,
pSock
);
}
return
std
::
make_shared
<
Tcp
SessionTypeImp
<
EchoSessionWithUrl
>
>
(
header
,
parent
,
pSock
);
return
std
::
make_shared
<
SessionTypeImp
<
EchoSessionWithUrl
>
>
(
header
,
parent
,
pSock
);
}
};
...
...
@@ -107,7 +107,7 @@ int main(int argc, char *argv[]) {
httpsSrv
->
start
<
WebSocketSessionBase
<
EchoSessionCreator
,
HttpsSession
>
>
(
443
);
//默认443
TcpServer
::
Ptr
httpSrvOld
(
new
TcpServer
());
//兼容之前的代码(但是不支持根据url选择生成
Tcp
Session类型)
//兼容之前的代码(但是不支持根据url选择生成Session类型)
httpSrvOld
->
start
<
WebSocketSession
<
EchoSession
,
HttpSession
>
>
(
8080
);
DebugL
<<
"请打开网页:http://www.websocket-test.com/,进行测试"
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论