Skip to content

Commit f1b6c7c

Browse files
author
chengxin
committed
【FIX】demo任务中监听系统的signal,补充日志
1 parent c4eb911 commit f1b6c7c

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,32 @@ void asset_changed_callback(const tigeropen::push::pb::AssetData& data) {
460460
ucout << "- cashbalance: " << data.cashbalance() << std::endl;
461461
}
462462

463+
std::atomic<bool> keep_running(true);
464+
void signal_handler(int signal)
465+
{
466+
if (signal == SIGINT || signal == SIGTERM)
467+
{
468+
keep_running = false;
469+
}
470+
}
471+
463472
int main()
464473
{
465474
/************************** set config **********************/
466-
ClientConfig config = ClientConfig();
467-
#if 1
468-
// config.private_key = U("");
469-
// config.tiger_id = U("");
470-
// config.account = U("");
471-
#else
472-
config.private_key = U("");
473-
config.tiger_id = U("");
474-
config.account = U("");
475-
#endif
476-
475+
bool sandbox_debug = false;
476+
ClientConfig config = ClientConfig(sandbox_debug);
477+
if (sandbox_debug)
478+
{
479+
config.private_key = U("");
480+
config.tiger_id = U("");
481+
config.account = U("");
482+
}
483+
else
484+
{
485+
config.private_key = U("");
486+
config.tiger_id = U("");
487+
config.account = U("");
488+
}
477489

478490
auto push_client = IPushClient::create_push_client(config);
479491
push_client->set_position_changed_callback(std::function<void(const tigeropen::push::pb::PositionData&)>(position_changed_callback));
@@ -485,20 +497,6 @@ int main()
485497
push_client->subscribe_asset("");
486498

487499
push_client->connect();
488-
std::string input;
489-
while (true)
490-
{
491-
std::cout << "Enter command (type 'exit' to quit): ";
492-
std::getline(std::cin, input);
493-
494-
if (input == "exit") {
495-
std::cout << "Exiting loop." << std::endl;
496-
// push_client->disconnect();
497-
break;
498-
}
499-
// Process other commands or input here
500-
std::cout << "You entered: " << input << std::endl;
501-
}
502500

503501
//config.lang = U("en_US");
504502

@@ -520,8 +518,15 @@ int main()
520518
*/
521519
// std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
522520
// TestTigerApi::test_grab_quote_permission(tigerapi);
521+
522+
std::signal(SIGINT, signal_handler); //Ctrl+C
523+
std::signal(SIGTERM, signal_handler); //kill
524+
while (keep_running)
525+
{
526+
std::this_thread::sleep_for(std::chrono::seconds(1));
527+
}
523528

524-
529+
push_client->disconnect();
525530

526531
return 0;
527532
}

include/tigerapi/push_client.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace TIGER_API
5454

5555
virtual void set_query_subscribed_symbols_changed_callback(const std::function<void(const tigeropen::push::pb::Response& query_subscribed_symbols_response)>& cb) = 0;
5656
virtual void query_subscribed_symbols() = 0;
57+
5758
virtual void set_quote_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBasicData&)>& cb) = 0;
5859
virtual bool subscribe_quote(const std::vector<std::string>& symbols) = 0;
5960
virtual bool subscribe_future_quote(const std::vector<std::string>& symbols) = 0;
@@ -90,10 +91,6 @@ namespace TIGER_API
9091
virtual void set_option_top_changed_callback(const std::function<void(const tigeropen::push::pb::OptionTopData&)>& cb) = 0;
9192
virtual bool subscribe_option_top(const std::string& market) = 0;
9293
virtual bool unsubscribe_option_top(const std::string& market) = 0;
93-
94-
95-
96-
//TODO:other
9794
};
9895
}
9996

src/push_socket/push_socket.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void TIGER_API::PushSocket::disconnect()
8484
{
8585
if (keep_alive_timer_)
8686
{
87+
LOG(INFO) << "stop keep alive scheduled task";
8788
boost::system::error_code ec;
8889
keep_alive_timer_->cancel(ec);
8990
if (ec)
@@ -173,6 +174,7 @@ void TIGER_API::PushSocket::close_session()
173174
bool open = socket_->lowest_layer().is_open();
174175
if (open)
175176
{
177+
LOG(INFO) << "socket shutdown both";
176178
boost::system::error_code ec;
177179
socket_->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
178180
if (ec)
@@ -181,20 +183,26 @@ void TIGER_API::PushSocket::close_session()
181183
dispatch_inner_error_callback(ec.message());
182184
}
183185

186+
LOG(INFO) << "socket execute close";
184187
socket_->lowest_layer().close(ec);
185188
if (ec)
186189
{
187190
LOG(ERROR) << ec;
188191
dispatch_inner_error_callback(ec.message());
189192
}
190193
}
194+
else
195+
{
196+
LOG(INFO) << "socket already closed";
197+
}
191198
}
192199
catch (const boost::system::system_error& e)
193200
{
194201
LOG(ERROR) << e.what();
195202
dispatch_inner_error_callback(e.what());
196203
}
197204

205+
LOG(INFO) << "socket close success";
198206
dispatch_disconnected_callback();
199207
}
200208

@@ -268,6 +276,7 @@ void TIGER_API::PushSocket::auto_reconnect()
268276

269277
void TIGER_API::PushSocket::cancel_reconnect_timer()
270278
{
279+
LOG(INFO) << "stop auto reconnect scheduled task";
271280
if (reconnect_timer_)
272281
{
273282
boost::system::error_code ec;

0 commit comments

Comments
 (0)