Skip to content

Commit 6c39679

Browse files
author
chengxin
committed
【FIX】连接异常或者正常断开的时候都应该立即终止重连的定时任务,优化socket对象的构造时机
1 parent a9bec68 commit 6c39679

File tree

3 files changed

+20
-37
lines changed

3 files changed

+20
-37
lines changed

include/tigerapi/push_socket/push_client_impl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ namespace TIGER_API
3838
private:
3939
std::function<void(const tigeropen::push::pb::AssetData&)> asset_changed_;
4040
private:
41-
ClientConfig client_config_;
4241
boost::asio::io_service io_service_;
4342
std::shared_ptr<TIGER_API::PushSocket> socket_;
4443
std::shared_ptr<std::thread> worker_thread_;

src/push_socket/push_client_impl.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ TIGER_API::PushClientImpl::~PushClientImpl()
2121
}
2222

2323
TIGER_API::PushClientImpl::PushClientImpl(const TIGER_API::ClientConfig& client_config)
24-
:client_config_(client_config)
2524
{
26-
25+
socket_ = PushSocket::create_push_socket(&io_service_, client_config);
2726
}
2827

2928
void TIGER_API::PushClientImpl::connect()
@@ -32,7 +31,7 @@ void TIGER_API::PushClientImpl::connect()
3231
//启动工作线程
3332
worker_thread_ = std::shared_ptr<std::thread>(new std::thread([this]
3433
{
35-
socket_ = PushSocket::create_push_socket(&io_service_, client_config_);
34+
3635
socket_->connect();
3736

3837
LOG(INFO) << "io_service run on work thread";
@@ -48,26 +47,17 @@ void TIGER_API::PushClientImpl::disconnect()
4847

4948
void TIGER_API::PushClientImpl::set_connected_callback(const std::function<void()>& cb)
5049
{
51-
if (socket_)
52-
{
53-
socket_->set_connected_callback(cb);
54-
}
50+
socket_->set_connected_callback(cb);
5551
}
5652

5753
void TIGER_API::PushClientImpl::set_disconnected_callback(const std::function<void()>& cb)
5854
{
59-
if (socket_)
60-
{
61-
socket_->set_disconnected_callback(cb);
62-
}
55+
socket_->set_disconnected_callback(cb);
6356
}
6457

6558
void TIGER_API::PushClientImpl::set_inner_error_callback(const std::function<void(std::string)>& cb)
6659
{
67-
if (socket_)
68-
{
69-
socket_->set_inner_error_callback(cb);
70-
}
60+
socket_->set_inner_error_callback(cb);
7161
}
7262

7363
void TIGER_API::PushClientImpl::set_asset_changed_callback(const std::function<void(const tigeropen::push::pb::AssetData&)>& cb)
@@ -77,7 +67,7 @@ void TIGER_API::PushClientImpl::set_asset_changed_callback(const std::function<v
7767

7868
bool TIGER_API::PushClientImpl::subscribe_asset(const std::string& account)
7969
{
80-
if (!socket_)
70+
if (account.empty())
8171
{
8272
return false;
8373
}
@@ -100,7 +90,7 @@ bool TIGER_API::PushClientImpl::subscribe_asset(const std::string& account)
10090

10191
bool TIGER_API::PushClientImpl::unsubscribe_asset(const std::string& account)
10292
{
103-
if (!socket_)
93+
if (account.empty())
10494
{
10595
return false;
10696
}
@@ -144,18 +134,12 @@ bool TIGER_API::PushClientImpl::send_frame(const tigeropen::push::pb::Request& r
144134

145135
void TIGER_API::PushClientImpl::do_write(const std::string& frame)
146136
{
147-
if (socket_)
148-
{
149-
socket_->send_message(frame);
150-
}
137+
socket_->send_message(frame);
151138
}
152139

153140
void TIGER_API::PushClientImpl::do_disconnect()
154141
{
155-
if (socket_)
156-
{
157-
socket_->disconnect();
158-
}
142+
socket_->disconnect();
159143
}
160144

161145
void TIGER_API::PushClientImpl::on_message(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object)

src/push_socket/push_socket.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ TIGER_API::PushSocket::PushSocket(boost::asio::io_service* io_service,
3131
client_config_ = client_config;
3232
send_interval_ = client_config_.send_interval;
3333
recv_interval_ = client_config_.receive_interval;
34-
35-
init_socket();
36-
socket_->set_verify_callback(boost::bind(&PushSocket::verify_certificate, this, _1, _2));
37-
34+
3835
//初始化内存池,防止接收数据频繁的内存分配和释放引起的性能问题和内存碎片问题
3936
recv_buff_pool_.reset(new boost::pool<>(MEMORY_POOL_PAGE_SIZE, MEMORY_POOL_BLOCK_NUM));
4037

@@ -78,6 +75,8 @@ void TIGER_API::PushSocket::connect()
7875
std::string str_target_server_ip = rit->endpoint().address().to_string();
7976
LOG(INFO) << "resolved ip: " << str_target_server_ip;
8077

78+
init_socket();
79+
8180
socket_->lowest_layer().async_connect(*rit,
8281
boost::bind(&PushSocket::handle_connect, this, boost::asio::placeholders::error, ++rit));
8382

@@ -95,12 +94,6 @@ void TIGER_API::PushSocket::disconnect()
9594
{
9695
keep_alive_timer_->cancel();
9796
}
98-
99-
if (reconnect_timer_)
100-
{
101-
reconnect_timer_->cancel();
102-
}
103-
10497
close_session();
10598
}
10699

@@ -148,6 +141,8 @@ void TIGER_API::PushSocket::init_socket()
148141
}
149142

150143
socket_.emplace(*io_service_, ssl_content);
144+
145+
socket_->set_verify_callback(boost::bind(&PushSocket::verify_certificate, this, _1, _2));
151146
}
152147

153148
bool TIGER_API::PushSocket::verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx)
@@ -175,6 +170,11 @@ uint32_t TIGER_API::PushSocket::get_next_id()
175170

176171
void TIGER_API::PushSocket::close_session()
177172
{
173+
if (reconnect_timer_)
174+
{
175+
reconnect_timer_->cancel();
176+
}
177+
178178
LOG(INFO) << "close socket";
179179
socket_state_ = SocketState::DISCONNECTED;
180180

@@ -259,7 +259,7 @@ void TIGER_API::PushSocket::send_heart_beat()
259259

260260
void TIGER_API::PushSocket::auto_reconnect()
261261
{
262-
LOG(INFO) << "try reconnecting after" << reconnect_interval_ / 1000 << "seconds...";
262+
LOG(INFO) << "try reconnecting after " << reconnect_interval_ / 1000 << " seconds...";
263263
socket_state_ = SocketState::CONNECTING;
264264
reconnect_timer_->expires_from_now(boost::posix_time::milliseconds(reconnect_interval_));
265265
reconnect_timer_->async_wait([this](const boost::system::error_code& error)

0 commit comments

Comments
 (0)