Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
pulsar
提交
d8f61553
pulsar
项目概览
apache
/
pulsar
通知
129
Star
40
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
pulsar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
提交
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d8f61553
编写于
7月 20, 2019
作者:
M
massakam
提交者:
Matteo Merli
7月 19, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce strand to C++ client for exclusive control (#4750)
上级
849b6c57
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
68 addition
and
11 deletion
+68
-11
pulsar-client-cpp/lib/ClientConnection.cc
pulsar-client-cpp/lib/ClientConnection.cc
+49
-9
pulsar-client-cpp/lib/ClientConnection.h
pulsar-client-cpp/lib/ClientConnection.h
+19
-2
未找到文件。
pulsar-client-cpp/lib/ClientConnection.cc
浏览文件 @
d8f61553
...
...
@@ -138,6 +138,13 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
executor_
(
executor
),
resolver_
(
executor
->
createTcpResolver
()),
socket_
(
executor
->
createSocket
()),
#if BOOST_VERSION >= 107000
strand_
(
boost
::
asio
::
make_strand
(
executor_
->
io_service_
.
get_executor
())),
#elif BOOST_VERSION >= 106600
strand_
(
executor_
->
io_service_
.
get_executor
()),
#else
strand_
(
executor_
->
io_service_
),
#endif
logicalAddress_
(
logicalAddress
),
physicalAddress_
(
physicalAddress
),
cnxString_
(
"[<none> -> "
+
physicalAddress
+
"] "
),
...
...
@@ -344,9 +351,16 @@ void ClientConnection::handleTcpConnected(const boost::system::error_code& err,
return
;
}
}
#if BOOST_VERSION >= 106600
tlsSocket_
->
async_handshake
(
boost
::
asio
::
ssl
::
stream
<
tcp
::
socket
>::
client
,
std
::
bind
(
&
ClientConnection
::
handleHandshake
,
shared_from_this
(),
std
::
placeholders
::
_1
));
boost
::
asio
::
bind_executor
(
strand_
,
std
::
bind
(
&
ClientConnection
::
handleHandshake
,
shared_from_this
(),
std
::
placeholders
::
_1
)));
#else
tlsSocket_
->
async_handshake
(
boost
::
asio
::
ssl
::
stream
<
tcp
::
socket
>::
client
,
strand_
.
wrap
(
std
::
bind
(
&
ClientConnection
::
handleHandshake
,
shared_from_this
(),
std
::
placeholders
::
_1
)));
#endif
}
else
{
handleHandshake
(
boost
::
system
::
errc
::
make_error_code
(
boost
::
system
::
errc
::
success
));
}
...
...
@@ -1146,31 +1160,57 @@ void ClientConnection::sendCommand(const SharedBuffer& cmd) {
if
(
pendingWriteOperations_
++
==
0
)
{
// Write immediately to socket
asyncWrite
(
cmd
.
const_asio_buffer
(),
customAllocWriteHandler
(
std
::
bind
(
&
ClientConnection
::
handleSend
,
shared_from_this
(),
std
::
placeholders
::
_1
,
cmd
)));
if
(
tlsSocket_
)
{
#if BOOST_VERSION >= 106600
boost
::
asio
::
post
(
strand_
,
std
::
bind
(
&
ClientConnection
::
sendCommandInternal
,
shared_from_this
(),
cmd
));
#else
strand_
.
post
(
std
::
bind
(
&
ClientConnection
::
sendCommandInternal
,
shared_from_this
(),
cmd
));
#endif
}
else
{
sendCommandInternal
(
cmd
);
}
}
else
{
// Queue to send later
pendingWriteBuffers_
.
push_back
(
cmd
);
}
}
void
ClientConnection
::
sendCommandInternal
(
const
SharedBuffer
&
cmd
)
{
asyncWrite
(
cmd
.
const_asio_buffer
(),
customAllocWriteHandler
(
std
::
bind
(
&
ClientConnection
::
handleSend
,
shared_from_this
(),
std
::
placeholders
::
_1
,
cmd
)));
}
void
ClientConnection
::
sendMessage
(
const
OpSendMsg
&
opSend
)
{
Lock
lock
(
mutex_
);
if
(
pendingWriteOperations_
++
==
0
)
{
PairSharedBuffer
buffer
=
Commands
::
newSend
(
outgoingBuffer_
,
outgoingCmd_
,
opSend
.
producerId_
,
opSend
.
sequenceId_
,
getChecksumType
(),
opSend
.
msg_
);
// Write immediately to socket
asyncWrite
(
buffer
,
customAllocWriteHandler
(
std
::
bind
(
&
ClientConnection
::
handleSendPair
,
shared_from_this
(),
std
::
placeholders
::
_1
)));
if
(
tlsSocket_
)
{
#if BOOST_VERSION >= 106600
boost
::
asio
::
post
(
strand_
,
std
::
bind
(
&
ClientConnection
::
sendMessageInternal
,
shared_from_this
(),
opSend
));
#else
strand_
.
post
(
std
::
bind
(
&
ClientConnection
::
sendMessageInternal
,
shared_from_this
(),
opSend
));
#endif
}
else
{
sendMessageInternal
(
opSend
);
}
}
else
{
// Queue to send later
pendingWriteBuffers_
.
push_back
(
opSend
);
}
}
void
ClientConnection
::
sendMessageInternal
(
const
OpSendMsg
&
opSend
)
{
PairSharedBuffer
buffer
=
Commands
::
newSend
(
outgoingBuffer_
,
outgoingCmd_
,
opSend
.
producerId_
,
opSend
.
sequenceId_
,
getChecksumType
(),
opSend
.
msg_
);
asyncWrite
(
buffer
,
customAllocWriteHandler
(
std
::
bind
(
&
ClientConnection
::
handleSendPair
,
shared_from_this
(),
std
::
placeholders
::
_1
)));
}
void
ClientConnection
::
handleSend
(
const
boost
::
system
::
error_code
&
err
,
const
SharedBuffer
&
)
{
if
(
err
)
{
LOG_WARN
(
cnxString_
<<
"Could not send message on connection: "
<<
err
<<
" "
<<
err
.
message
());
...
...
pulsar-client-cpp/lib/ClientConnection.h
浏览文件 @
d8f61553
...
...
@@ -24,6 +24,7 @@
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/strand.hpp>
#include <boost/any.hpp>
#include <mutex>
#include <functional>
...
...
@@ -125,7 +126,9 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
LookupDataResultPromisePtr
promise
);
void
sendCommand
(
const
SharedBuffer
&
cmd
);
void
sendCommandInternal
(
const
SharedBuffer
&
cmd
);
void
sendMessage
(
const
OpSendMsg
&
opSend
);
void
sendMessageInternal
(
const
OpSendMsg
&
opSend
);
void
registerProducer
(
int
producerId
,
ProducerImplPtr
producer
);
void
registerConsumer
(
int
consumerId
,
ConsumerImplPtr
consumer
);
...
...
@@ -220,7 +223,11 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
template
<
typename
ConstBufferSequence
,
typename
WriteHandler
>
inline
void
asyncWrite
(
const
ConstBufferSequence
&
buffers
,
WriteHandler
handler
)
{
if
(
tlsSocket_
)
{
boost
::
asio
::
async_write
(
*
tlsSocket_
,
buffers
,
handler
);
#if BOOST_VERSION >= 106600
boost
::
asio
::
async_write
(
*
tlsSocket_
,
buffers
,
boost
::
asio
::
bind_executor
(
strand_
,
handler
));
#else
boost
::
asio
::
async_write
(
*
tlsSocket_
,
buffers
,
strand_
.
wrap
(
handler
));
#endif
}
else
{
boost
::
asio
::
async_write
(
*
socket_
,
buffers
,
handler
);
}
...
...
@@ -229,7 +236,11 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
template
<
typename
MutableBufferSequence
,
typename
ReadHandler
>
inline
void
asyncReceive
(
const
MutableBufferSequence
&
buffers
,
ReadHandler
handler
)
{
if
(
tlsSocket_
)
{
tlsSocket_
->
async_read_some
(
buffers
,
handler
);
#if BOOST_VERSION >= 106600
tlsSocket_
->
async_read_some
(
buffers
,
boost
::
asio
::
bind_executor
(
strand_
,
handler
));
#else
tlsSocket_
->
async_read_some
(
buffers
,
strand_
.
wrap
(
handler
));
#endif
}
else
{
socket_
->
async_receive
(
buffers
,
handler
);
}
...
...
@@ -319,6 +330,12 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
friend
class
PulsarFriend
;
bool
isTlsAllowInsecureConnection_
;
#if BOOST_VERSION >= 106600
boost
::
asio
::
strand
<
boost
::
asio
::
io_service
::
executor_type
>
strand_
;
#else
boost
::
asio
::
io_service
::
strand
strand_
;
#endif
};
}
// namespace pulsar
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录