Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
ee980837
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ee980837
编写于
1月 16, 2022
作者:
dengyihao
提交者:
GitHub
1月 16, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9827 from taosdata/feature/rpc
refactor code
上级
b59f241e
41c3160d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
70 addition
and
35 deletion
+70
-35
source/libs/transport/src/transport.c
source/libs/transport/src/transport.c
+70
-35
未找到文件。
source/libs/transport/src/transport.c
浏览文件 @
ee980837
...
...
@@ -135,12 +135,13 @@ typedef struct SRpcConn {
uv_async_t
*
pWorkerAsync
;
queue
queue
;
int
ref
;
int
persist
;
// persist connection or not
SConnBuffer
connBuf
;
int
persist
;
// persist connection or not
SConnBuffer
connBuf
;
// read buf,
SConnBuffer
writeBuf
;
// write buf
int
count
;
void
*
shandle
;
// rpc init
void
*
ahandle
;
void
*
ahandle
;
//
void
*
hostThread
;
// del later
char
secured
;
int
spi
;
...
...
@@ -151,6 +152,7 @@ typedef struct SRpcConn {
}
SRpcConn
;
// auth function
static
int
uvAuthMsg
(
SRpcConn
*
pConn
,
char
*
msg
,
int
msgLen
);
static
int
rpcAuthenticateMsg
(
void
*
pMsg
,
int
msgLen
,
void
*
pAuth
,
void
*
pKey
);
static
void
rpcBuildAuthHead
(
void
*
pMsg
,
int
msgLen
,
void
*
pAuth
,
void
*
pKey
);
static
int
rpcAddAuthPart
(
SRpcConn
*
pConn
,
char
*
msg
,
int
msgLen
);
...
...
@@ -259,7 +261,7 @@ static bool isReadAll(SConnBuffer* data) {
SRpcHead
rpcHead
;
int32_t
headLen
=
sizeof
(
rpcHead
);
if
(
data
->
len
>=
headLen
)
{
memcpy
((
char
*
)
&
rpcHead
,
data
->
buf
,
headLen
);
memcpy
((
char
*
)
&
rpcHead
,
data
->
buf
+
RPC_RESERVE_SIZE
,
headLen
);
int32_t
msgLen
=
(
int32_t
)
htonl
((
uint32_t
)
rpcHead
.
msgLen
);
if
(
msgLen
>
data
->
len
)
{
data
->
left
=
msgLen
-
data
->
len
;
...
...
@@ -283,7 +285,7 @@ static void uvDoProcess(SRecvInfo* pRecv) {
// do auth and check
}
static
int
uvAuth
Data
(
SRpcConn
*
pConn
,
char
*
msg
,
int
len
)
{
static
int
uvAuth
Msg
(
SRpcConn
*
pConn
,
char
*
msg
,
int
len
)
{
SRpcHead
*
pHead
=
(
SRpcHead
*
)
msg
;
int
code
=
0
;
...
...
@@ -334,16 +336,21 @@ static int uvAuthData(SRpcConn* pConn, char* msg, int len) {
return
code
;
}
static
void
uvProcessData
(
SRpcConn
*
ctx
)
{
// refers specifically to query or insert timeout
static
void
uvHandleActivityTimeout
(
uv_timer_t
*
handle
)
{
// impl later
SRpcConn
*
conn
=
handle
->
data
;
}
static
void
uvProcessData
(
SRpcConn
*
pConn
)
{
SRecvInfo
info
;
SRecvInfo
*
p
=
&
info
;
SConnBuffer
*
pBuf
=
&
ctx
->
connBuf
;
SConnBuffer
*
pBuf
=
&
pConn
->
connBuf
;
p
->
msg
=
pBuf
->
buf
+
RPC_RESERVE_SIZE
;
p
->
msgLen
=
pBuf
->
len
;
p
->
ip
=
0
;
p
->
port
=
0
;
p
->
shandle
=
ctx
->
shandle
;
//
p
->
thandle
=
ctx
;
p
->
shandle
=
pConn
->
shandle
;
//
p
->
thandle
=
pConn
;
p
->
chandle
=
NULL
;
//
...
...
@@ -351,9 +358,14 @@ static void uvProcessData(SRpcConn* ctx) {
assert
(
rpcIsReq
(
pHead
->
msgType
));
SRpcInfo
*
pRpc
=
(
SRpcInfo
*
)
p
->
shandle
;
SRpcConn
*
pConn
=
(
SRpcConn
*
)
p
->
thandle
;
pConn
->
ahandle
=
(
void
*
)
pHead
->
ahandle
;
// auth here
int8_t
code
=
uvAuthMsg
(
pConn
,
(
char
*
)
pHead
,
p
->
msgLen
);
if
(
code
!=
0
)
{
terrno
=
code
;
return
;
}
pHead
->
code
=
htonl
(
pHead
->
code
);
SRpcMsg
rpcMsg
;
...
...
@@ -365,7 +377,9 @@ static void uvProcessData(SRpcConn* ctx) {
rpcMsg
.
code
=
pHead
->
code
;
rpcMsg
.
ahandle
=
pConn
->
ahandle
;
rpcMsg
.
handle
=
pConn
;
(
*
(
pRpc
->
cfp
))(
pRpc
->
parent
,
&
rpcMsg
,
NULL
);
uv_timer_start
(
pConn
->
pTimer
,
uvHandleActivityTimeout
,
pRpc
->
idleTime
,
0
);
// auth
// validate msg type
}
...
...
@@ -383,6 +397,9 @@ void uvOnReadCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) {
}
return
;
}
if
(
terrno
!=
0
)
{
// handle err code
}
if
(
nread
!=
UV_EOF
)
{
tDebug
(
"Read error %s
\n
"
,
uv_err_name
(
nread
));
...
...
@@ -410,21 +427,23 @@ void uvOnWriteCb(uv_write_t* req, int status) {
}
void
uvWorkerAsyncCb
(
uv_async_t
*
handle
)
{
SThreadObj
*
p
Obj
=
container_of
(
handle
,
SThreadObj
,
workerAsync
);
SThreadObj
*
p
Thrd
=
container_of
(
handle
,
SThreadObj
,
workerAsync
);
SRpcConn
*
conn
=
NULL
;
// opt later
pthread_mutex_lock
(
&
p
Obj
->
connMtx
);
if
(
!
QUEUE_IS_EMPTY
(
&
p
Obj
->
conn
))
{
queue
*
head
=
QUEUE_HEAD
(
&
p
Obj
->
conn
);
pthread_mutex_lock
(
&
p
Thrd
->
connMtx
);
if
(
!
QUEUE_IS_EMPTY
(
&
p
Thrd
->
conn
))
{
queue
*
head
=
QUEUE_HEAD
(
&
p
Thrd
->
conn
);
conn
=
QUEUE_DATA
(
head
,
SRpcConn
,
queue
);
QUEUE_REMOVE
(
&
conn
->
queue
);
}
pthread_mutex_unlock
(
&
p
Obj
->
connMtx
);
pthread_mutex_unlock
(
&
p
Thrd
->
connMtx
);
if
(
conn
==
NULL
)
{
tError
(
"except occurred, do nothing"
);
return
;
}
uv_buf_t
wb
=
uv_buf_init
(
conn
->
writeBuf
.
buf
,
conn
->
writeBuf
.
len
);
uv_write
(
conn
->
pWriter
,
(
uv_stream_t
*
)
conn
->
pTcp
,
&
wb
,
1
,
uvOnWriteCb
);
}
void
uvOnAcceptCb
(
uv_stream_t
*
stream
,
int
status
)
{
...
...
@@ -463,7 +482,7 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
assert
(
buf
->
base
[
0
]
==
notify
[
0
]);
free
(
buf
->
base
);
SThreadObj
*
p
Obj
=
q
->
data
;
SThreadObj
*
p
Thrd
=
q
->
data
;
uv_pipe_t
*
pipe
=
(
uv_pipe_t
*
)
q
;
if
(
!
uv_pipe_pending_count
(
pipe
))
{
...
...
@@ -475,16 +494,18 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) {
assert
(
pending
==
UV_TCP
);
SRpcConn
*
pConn
=
connCreate
();
pConn
->
shandle
=
p
Obj
->
shandle
;
pConn
->
shandle
=
p
Thrd
->
shandle
;
/* init conn timer*/
pConn
->
pTimer
=
malloc
(
sizeof
(
uv_timer_t
));
uv_timer_init
(
pObj
->
loop
,
pConn
->
pTimer
);
uv_timer_init
(
pThrd
->
loop
,
pConn
->
pTimer
);
pConn
->
pTimer
->
data
=
pConn
;
pConn
->
pWorkerAsync
=
pObj
->
workerAsync
;
// thread safty
pConn
->
hostThread
=
pThrd
;
pConn
->
pWorkerAsync
=
pThrd
->
workerAsync
;
// thread safty
// init client handle
pConn
->
pTcp
=
(
uv_tcp_t
*
)
malloc
(
sizeof
(
uv_tcp_t
));
uv_tcp_init
(
p
Obj
->
loop
,
pConn
->
pTcp
);
uv_tcp_init
(
p
Thrd
->
loop
,
pConn
->
pTcp
);
pConn
->
pTcp
->
data
=
pConn
;
// init write request, just
...
...
@@ -518,23 +539,23 @@ void* acceptThread(void* arg) {
uv_run
(
srv
->
loop
,
UV_RUN_DEFAULT
);
}
void
*
workerThread
(
void
*
arg
)
{
SThreadObj
*
p
Obj
=
(
SThreadObj
*
)
arg
;
SThreadObj
*
p
Thrd
=
(
SThreadObj
*
)
arg
;
p
Obj
->
loop
=
(
uv_loop_t
*
)
malloc
(
sizeof
(
uv_loop_t
));
uv_loop_init
(
p
Obj
->
loop
);
p
Thrd
->
loop
=
(
uv_loop_t
*
)
malloc
(
sizeof
(
uv_loop_t
));
uv_loop_init
(
p
Thrd
->
loop
);
uv_pipe_init
(
p
Obj
->
loop
,
pObj
->
pipe
,
1
);
uv_pipe_open
(
p
Obj
->
pipe
,
pObj
->
fd
);
uv_pipe_init
(
p
Thrd
->
loop
,
pThrd
->
pipe
,
1
);
uv_pipe_open
(
p
Thrd
->
pipe
,
pThrd
->
fd
);
p
Obj
->
pipe
->
data
=
pObj
;
p
Thrd
->
pipe
->
data
=
pThrd
;
QUEUE_INIT
(
&
p
Obj
->
conn
);
QUEUE_INIT
(
&
p
Thrd
->
conn
);
p
Obj
->
workerAsync
=
malloc
(
sizeof
(
uv_async_t
));
uv_async_init
(
p
Obj
->
loop
,
pObj
->
workerAsync
,
uvWorkerAsyncCb
);
p
Thrd
->
workerAsync
=
malloc
(
sizeof
(
uv_async_t
));
uv_async_init
(
p
Thrd
->
loop
,
pThrd
->
workerAsync
,
uvWorkerAsyncCb
);
uv_read_start
((
uv_stream_t
*
)
p
Obj
->
pipe
,
uvAllocConnBufferCb
,
uvOnConnectionCb
);
uv_run
(
p
Obj
->
loop
,
UV_RUN_DEFAULT
);
uv_read_start
((
uv_stream_t
*
)
p
Thrd
->
pipe
,
uvAllocConnBufferCb
,
uvOnConnectionCb
);
uv_run
(
p
Thrd
->
loop
,
UV_RUN_DEFAULT
);
}
static
SRpcConn
*
connCreate
()
{
SRpcConn
*
pConn
=
(
SRpcConn
*
)
calloc
(
1
,
sizeof
(
SRpcConn
));
...
...
@@ -547,6 +568,7 @@ static void connDestroy(SRpcConn* conn) {
uv_timer_stop
(
conn
->
pTimer
);
free
(
conn
->
pTimer
);
uv_close
((
uv_handle_t
*
)
conn
->
pTcp
,
NULL
);
free
(
conn
->
connBuf
.
buf
);
free
(
conn
->
pTcp
);
free
(
conn
->
pWriter
);
free
(
conn
);
...
...
@@ -573,9 +595,22 @@ void* rpcMallocCont(int contLen) { return NULL; }
void
rpcFreeCont
(
void
*
cont
)
{
return
;
}
void
*
rpcReallocCont
(
void
*
ptr
,
int
contLen
)
{
return
NULL
;
}
void
rpcSendRequest
(
void
*
thandle
,
const
SEpSet
*
pEpSet
,
SRpcMsg
*
pMsg
,
int64_t
*
rid
)
{
return
;
}
void
rpcSendRequest
(
void
*
thandle
,
const
SEpSet
*
pEpSet
,
SRpcMsg
*
pMsg
,
int64_t
*
rid
)
{
// impl later
return
;
}
void
rpcSendResponse
(
const
SRpcMsg
*
pMsg
)
{}
void
rpcSendResponse
(
const
SRpcMsg
*
pMsg
)
{
SRpcConn
*
pConn
=
pMsg
->
handle
;
SThreadObj
*
pThrd
=
pConn
->
hostThread
;
// opt later
pthread_mutex_lock
(
&
pThrd
->
connMtx
);
QUEUE_PUSH
(
&
pThrd
->
conn
,
&
pConn
->
queue
);
pthread_mutex_unlock
(
&
pThrd
->
connMtx
);
uv_async_send
(
pConn
->
pWorkerAsync
);
}
void
rpcSendRedirectRsp
(
void
*
pConn
,
const
SEpSet
*
pEpSet
)
{}
int
rpcGetConnInfo
(
void
*
thandle
,
SRpcConnInfo
*
pInfo
)
{
return
-
1
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录