Clean up on exit

This commit is contained in:
Changhua 2022-10-16 16:08:13 +08:00
parent 9a21dbd082
commit f13cea79fd

View File

@ -51,7 +51,7 @@ public:
{ {
cout << "~WcfClient()" << endl; cout << "~WcfClient()" << endl;
this->DisableRecvMsg(); this->DisableRecvMsg();
// WxDestroySDK(); WxDestroySDK();
} }
int IsLogin() int IsLogin()
@ -135,7 +135,7 @@ public:
{ {
unique_lock<mutex> l(mu_); unique_lock<mutex> l(mu_);
status_ = s; status_ = s;
done_ = true; done_ = true;
cv_.notify_one(); cv_.notify_one();
} }
@ -167,8 +167,6 @@ public:
} }
} }
// void EnableRecvMsg(function<void(WxMsg &)> msg_handle_cb) { GetMessage(msg_handle_cb); }
int DisableRecvMsg() int DisableRecvMsg()
{ {
Empty empty; Empty empty;
@ -437,24 +435,23 @@ int OnMsg(WxMsg msg)
<< ", " << msg.sender() << ", " << msg.roomid() << ", " << msg.content() << endl; << ", " << msg.sender() << ", " << msg.roomid() << ", " << msg.content() << endl;
return 0; return 0;
} }
// volatile bool gKeepRunning = false;
// void handler(int s) volatile sig_atomic_t gStop;
// { void handler(int s)
// printf("Caught signal %d\n", s); {
// // exit(1); cout << "Ctrl + C" << endl;
// gKeepRunning = false; gStop = 1;
// } }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret; int ret;
// signal(SIGINT, handler);
signal(SIGINT, handler);
WcfClient &client = WcfClient::Instance("localhost:10086"); WcfClient &client = WcfClient::Instance("localhost:10086");
ret = client.IsLogin(); cout << "IsLogin: " << client.IsLogin() << endl;
cout << "IsLogin: " << ret << endl;
cout << "Self Wxid: " << client.GetSelfWxid() << endl; cout << "Self Wxid: " << client.GetSelfWxid() << endl;
ret = client.SendTextMsg("来自CPP的消息", "filehelper", ""); ret = client.SendTextMsg("来自CPP的消息", "filehelper", "");
@ -474,8 +471,14 @@ int main(int argc, char **argv)
cout << "GetContacts: " << cnts.contacts().size() << endl; cout << "GetContacts: " << cnts.contacts().size() << endl;
vector<Contact> vcnts(cnts.contacts().begin(), cnts.contacts().end()); vector<Contact> vcnts(cnts.contacts().begin(), cnts.contacts().end());
for (auto &c : vcnts) { for (auto &c : vcnts) {
string gender = "";
if (c.gender() == 1) {
gender = "";
} else if (c.gender() == 2) {
gender = "";
}
cout << c.wxid() << "\t" << c.code() << "\t" << c.name() << "\t" << c.country() << "\t" << c.province() << "\t" cout << c.wxid() << "\t" << c.code() << "\t" << c.name() << "\t" << c.country() << "\t" << c.province() << "\t"
<< c.city() << "\t" << c.gender() << endl; << c.city() << "\t" << gender << endl;
} }
DbNames db = client.GetDbNames(); DbNames db = client.GetDbNames();
@ -506,16 +509,17 @@ int main(int argc, char **argv)
// cout << "AcceptNewFriend: " << ret << endl; // cout << "AcceptNewFriend: " << ret << endl;
function<void(WxMsg &)> cb = OnMsg; function<void(WxMsg &)> cb = OnMsg;
thread t1 = thread([&]() { client.EnableRecvMsg(cb); }); thread t1 = thread([&]() { client.EnableRecvMsg(cb); });
// client.EnableRecvMsg(cb);
cout << "Block?..." << endl; while (!gStop) {
// gKeepRunning = true;
while (true) {
// cout << gKeepRunning << endl;
Sleep(1000); Sleep(1000);
} }
Sleep(1000); cout << "Cleanup" << endl;
cout << "Exit..." << endl; client.DisableRecvMsg();
system("pause");
client.~WcfClient();
return 0;
} }