Skip to content

Commit 948ae25

Browse files
committed
[STR-3621] Save last successful address
1 parent 14b8a03 commit 948ae25

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

Socket/TCPClient.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -319,34 +319,37 @@ bool CTCPClient::Connect(const std::string& strServer, const std::string& strPor
319319
{
320320
// create socket
321321
m_ConnectSocket = socket(pResPtr->ai_family, pResPtr->ai_socktype, pResPtr->ai_protocol);
322-
if (m_ConnectSocket < 0) // or == -1
323-
continue;
324-
325-
// connexion to the server
326-
const auto ConnectionTimeBegin = std::chrono::steady_clock::now();
327-
int iConRet = connect(m_ConnectSocket, pResPtr->ai_addr, pResPtr->ai_addrlen);
328-
const auto ConnectionTimeEnd = std::chrono::steady_clock::now();
329-
if (iConRet >= 0) // or != -1
322+
if (m_ConnectSocket >= 0)
330323
{
331-
/* Success */
332-
m_eStatus = CONNECTED;
333-
334-
if (m_eSettingsFlags & ENABLE_LOG)
335-
{
336-
const auto TimeElapsed =
337-
std::chrono::duration_cast<std::chrono::milliseconds>(ConnectionTimeEnd - ConnectionTimeBegin).count();
338-
m_oLog(StringFormat(
339-
"[TCPClient][Info] Successfully connected to address %s at index %d. Connection took %d ms",
340-
SockAddrToString(pResPtr->ai_addr).data(), uTmpIndex, TimeElapsed));
341-
}
342-
343-
if (m_pResultAddrInfo != nullptr)
324+
// connexion to the server
325+
const auto ConnectionTimeBegin = std::chrono::steady_clock::now();
326+
int iConRet = connect(m_ConnectSocket, pResPtr->ai_addr, pResPtr->ai_addrlen);
327+
const auto ConnectionTimeEnd = std::chrono::steady_clock::now();
328+
if (iConRet >= 0) // or != -1
344329
{
345-
freeaddrinfo(m_pResultAddrInfo);
346-
m_pResultAddrInfo = nullptr;
330+
/* Success */
331+
m_eStatus = CONNECTED;
332+
333+
m_strLastAddress = SockAddrToString(pResPtr->ai_addr);
334+
if (m_eSettingsFlags & ENABLE_LOG)
335+
{
336+
const auto TimeElapsed =
337+
std::chrono::duration_cast<std::chrono::milliseconds>(ConnectionTimeEnd - ConnectionTimeBegin)
338+
.count();
339+
340+
m_oLog(StringFormat(
341+
"[TCPClient][Info] Successfully connected to address %s at index %d. Connection took %d ms",
342+
m_strLastAddress.data(), uTmpIndex, TimeElapsed));
343+
}
344+
345+
if (m_pResultAddrInfo != nullptr)
346+
{
347+
freeaddrinfo(m_pResultAddrInfo);
348+
m_pResultAddrInfo = nullptr;
349+
}
350+
351+
return true;
347352
}
348-
349-
return true;
350353
}
351354

352355
if (m_eSettingsFlags & ENABLE_LOG)

Socket/TCPClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class CTCPClient : public ASocket
7171

7272
Socket GetSocketDescriptor() const { return m_ConnectSocket; }
7373

74+
std::string GetLastAddress() const { return m_strLastAddress; }
75+
7476
protected:
7577
enum SocketStatus
7678
{
@@ -87,6 +89,7 @@ class CTCPClient : public ASocket
8789
struct addrinfo m_HintsAddrInfo;
8890
std::random_device m_RandDevice{};
8991
std::default_random_engine m_Rng;
92+
std::string m_strLastAddress;
9093
};
9194

9295
#endif

0 commit comments

Comments
 (0)