From ba6823e71d04c517b1f420d11e5ded4c169fb14c Mon Sep 17 00:00:00 2001 From: DragonAura <32223554+DragonAura@users.noreply.github.com> Date: Wed, 8 May 2024 10:22:24 +0800 Subject: [PATCH] fix: add cpp retry --- CAPI/cpp/API/src/Communication.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/CAPI/cpp/API/src/Communication.cpp b/CAPI/cpp/API/src/Communication.cpp index d1cb86b5..45100a8e 100755 --- a/CAPI/cpp/API/src/Communication.cpp +++ b/CAPI/cpp/API/src/Communication.cpp @@ -227,14 +227,18 @@ bool Communication::Recycle(int32_t playerID, int32_t teamID) bool Communication::TryConnection(int32_t playerID, int32_t teamID) { - protobuf::BoolRes reply; - ClientContext context; + constexpr int maxRetryNum = 10; auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID); - auto status = THUAI7Stub->TryConnection(&context, request, &reply); - if (status.ok()) - return true; - else - return false; + for (int retryNum = 0; retryNum < maxRetryNum; retryNum++) + { + protobuf::BoolRes reply; + ClientContext context; + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + auto status = THUAI7Stub->TryConnection(&context, request, &reply); + if (status.ok()) + return true; + } + return false; } void Communication::AddPlayer(int32_t playerID, int32_t teamID, THUAI7::ShipType ShipType) @@ -274,4 +278,4 @@ protobuf::MessageToClient Communication::GetMessage2Client() { return haveNewMessage; }); haveNewMessage = false; return message2Client; -} \ No newline at end of file +}