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
e8951c12
Commit
e8951c12
authored
Dec 27, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加线程相关api
parent
5b43b2c0
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
243 行增加
和
29 行删除
+243
-29
api/include/mk_thread.h
+102
-0
api/source/mk_tcp.cpp
+1
-29
api/source/mk_tcp_private.h
+62
-0
api/source/mk_thread.cpp
+77
-0
src/Http/WebSocketClient.h
+1
-0
没有找到文件。
api/include/mk_thread.h
0 → 100644
查看文件 @
e8951c12
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MK_THREAD_H
#define MK_THREAD_H
#include <assert.h>
#include "mk_common.h"
#include "mk_tcp.h"
#ifdef __cplusplus
extern
"C"
{
#endif
///////////////////////////////////////////事件线程/////////////////////////////////////////////
typedef
void
*
mk_thread
;
/**
* 获取tcp会话对象所在事件线程
* @param ctx tcp会话对象
* @return 对象所在事件线程
*/
API_EXPORT
mk_thread
API_CALL
mk_thread_from_tcp_session
(
mk_tcp_session
ctx
);
/**
* 获取tcp客户端对象所在事件线程
* @param ctx tcp客户端
* @return 对象所在事件线程
*/
API_EXPORT
mk_thread
API_CALL
mk_thread_from_tcp_client
(
mk_tcp_client
ctx
);
///////////////////////////////////////////线程切换/////////////////////////////////////////////
typedef
void
(
API_CALL
*
on_mk_async
)(
void
*
user_data
);
/**
* 切换到事件线程并异步执行
* @param ctx 事件线程
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT
void
API_CALL
mk_async_do
(
mk_thread
ctx
,
on_mk_async
cb
,
void
*
user_data
);
/**
* 切换到事件线程并同步执行
* @param ctx 事件线程
* @param cb 回调函数
* @param user_data 用户数据指针
*/
API_EXPORT
void
API_CALL
mk_sync_do
(
mk_thread
ctx
,
on_mk_async
cb
,
void
*
user_data
);
///////////////////////////////////////////定时器/////////////////////////////////////////////
typedef
void
*
mk_timer
;
/**
* 定时器触发事件
* @return 下一次触发延时(单位毫秒),返回0则不再重复
*/
typedef
uint64_t
(
API_CALL
*
on_mk_timer
)(
void
*
user_data
);
/**
* 创建定时器
* @param ctx 线程对象
* @param delay_ms 执行延时,单位毫秒
* @param cb 回调函数
* @param user_data 用户数据指针
* @return 定时器对象
*/
API_EXPORT
mk_timer
API_CALL
mk_timer_create
(
mk_thread
ctx
,
uint64_t
delay_ms
,
on_mk_timer
cb
,
void
*
user_data
);
/**
* 销毁和取消定时器
* @param ctx 定时器对象
*/
API_EXPORT
void
API_CALL
mk_timer_release
(
mk_timer
ctx
);
#ifdef __cplusplus
}
#endif
#endif //MK_THREAD_H
api/source/mk_tcp.cpp
查看文件 @
e8951c12
...
...
@@ -25,8 +25,7 @@
*/
#include "mk_tcp.h"
#include "Network/TcpSession.h"
#include "Network/TcpClient.h"
#include "mk_tcp_private.h"
#include "Http/WebSocketClient.h"
#include "Http/WebSocketSession.h"
using
namespace
mediakit
;
...
...
@@ -89,17 +88,6 @@ API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx,const
static
TcpServer
::
Ptr
s_tcp_server
[
4
];
static
mk_tcp_session_events
s_events_server
=
{
0
};
class
TcpSessionForC
:
public
TcpSession
{
public
:
TcpSessionForC
(
const
Socket
::
Ptr
&
pSock
)
;
~
TcpSessionForC
()
override
=
default
;
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
;
void
onError
(
const
SockException
&
err
)
override
;
void
onManager
()
override
;
void
*
_user_data
;
uint16_t
_local_port
;
};
TcpSessionForC
::
TcpSessionForC
(
const
Socket
::
Ptr
&
pSock
)
:
TcpSession
(
pSock
)
{
_local_port
=
get_local_port
();
if
(
s_events_server
.
on_mk_tcp_session_create
)
{
...
...
@@ -178,22 +166,6 @@ API_EXPORT uint16_t API_CALL mk_tcp_server_server_start(uint16_t port, mk_tcp_ty
}
///////////////////////////////////////////////////TcpClientForC/////////////////////////////////////////////////////////
class
TcpClientForC
:
public
TcpClient
{
public
:
typedef
std
::
shared_ptr
<
TcpClientForC
>
Ptr
;
TcpClientForC
(
mk_tcp_client_events
*
events
)
;
~
TcpClientForC
()
override
;
void
onRecv
(
const
Buffer
::
Ptr
&
pBuf
)
override
;
void
onErr
(
const
SockException
&
ex
)
override
;
void
onManager
()
override
;
void
onConnect
(
const
SockException
&
ex
)
override
;
void
setClient
(
mk_tcp_client
client
);
void
*
_user_data
;
private
:
mk_tcp_client_events
_events
;
mk_tcp_client
_client
;
};
TcpClientForC
::
TcpClientForC
(
mk_tcp_client_events
*
events
){
_events
=
*
events
;
}
...
...
api/source/mk_tcp_private.h
0 → 100644
查看文件 @
e8951c12
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MK_TCP_PRIVATE_H
#define MK_TCP_PRIVATE_H
#include "mk_tcp.h"
#include "Network/TcpClient.h"
#include "Network/TcpSession.h"
using
namespace
toolkit
;
class
TcpClientForC
:
public
TcpClient
{
public
:
typedef
std
::
shared_ptr
<
TcpClientForC
>
Ptr
;
TcpClientForC
(
mk_tcp_client_events
*
events
)
;
~
TcpClientForC
()
override
;
void
onRecv
(
const
Buffer
::
Ptr
&
pBuf
)
override
;
void
onErr
(
const
SockException
&
ex
)
override
;
void
onManager
()
override
;
void
onConnect
(
const
SockException
&
ex
)
override
;
void
setClient
(
mk_tcp_client
client
);
void
*
_user_data
;
private
:
mk_tcp_client_events
_events
;
mk_tcp_client
_client
;
};
class
TcpSessionForC
:
public
TcpSession
{
public
:
TcpSessionForC
(
const
Socket
::
Ptr
&
pSock
)
;
~
TcpSessionForC
()
override
=
default
;
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
;
void
onError
(
const
SockException
&
err
)
override
;
void
onManager
()
override
;
void
*
_user_data
;
uint16_t
_local_port
;
};
#endif //MK_TCP_PRIVATE_H
api/source/mk_thread.cpp
0 → 100644
查看文件 @
e8951c12
/*
* MIT License
*
* Copyright (c) 2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "mk_thread.h"
#include "mk_tcp_private.h"
#include "Util/logger.h"
#include "Poller/EventPoller.h"
using
namespace
std
;
using
namespace
toolkit
;
API_EXPORT
mk_thread
API_CALL
mk_thread_from_tcp_session
(
mk_tcp_session
ctx
){
assert
(
ctx
);
TcpSession
*
obj
=
(
TcpSession
*
)
ctx
;
return
obj
->
getPoller
().
get
();
}
API_EXPORT
mk_thread
API_CALL
mk_thread_from_tcp_client
(
mk_tcp_client
ctx
){
assert
(
ctx
);
TcpClient
::
Ptr
*
client
=
(
TcpClient
::
Ptr
*
)
ctx
;
return
(
*
client
)
->
getPoller
().
get
();
}
API_EXPORT
void
API_CALL
mk_async_do
(
mk_thread
ctx
,
on_mk_async
cb
,
void
*
user_data
){
assert
(
ctx
&&
cb
);
EventPoller
*
poller
=
(
EventPoller
*
)
ctx
;
poller
->
async
([
cb
,
user_data
](){
cb
(
user_data
);
});
}
API_EXPORT
void
API_CALL
mk_sync_do
(
mk_thread
ctx
,
on_mk_async
cb
,
void
*
user_data
){
assert
(
ctx
&&
cb
);
EventPoller
*
poller
=
(
EventPoller
*
)
ctx
;
poller
->
sync
([
cb
,
user_data
](){
cb
(
user_data
);
});
}
API_EXPORT
mk_timer
API_CALL
mk_timer_create
(
mk_thread
ctx
,
uint64_t
delay_ms
,
on_mk_timer
cb
,
void
*
user_data
){
assert
(
ctx
&&
cb
);
EventPoller
*
poller
=
(
EventPoller
*
)
ctx
;
auto
ret
=
poller
->
doDelayTask
(
delay_ms
,[
cb
,
user_data
](){
return
cb
(
user_data
);
});
return
new
DelayTask
::
Ptr
(
ret
);
}
API_EXPORT
void
API_CALL
mk_timer_release
(
mk_timer
ctx
){
assert
(
ctx
);
DelayTask
::
Ptr
*
obj
=
(
DelayTask
::
Ptr
*
)
ctx
;
(
*
obj
)
->
cancel
();
delete
obj
;
}
\ No newline at end of file
src/Http/WebSocketClient.h
查看文件 @
e8951c12
...
...
@@ -89,6 +89,7 @@ public:
HttpWsClient
(
ClientTypeImp
<
ClientType
,
DataType
>
&
delegate
)
:
_delegate
(
delegate
){
_Sec_WebSocket_Key
=
encodeBase64
(
SHA1
::
encode_bin
(
makeRandStr
(
16
,
false
)));
setPoller
(
delegate
.
getPoller
());
}
~
HttpWsClient
(){}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论