From 0054104017b6339e10e0bbc24d0fdf94d1061260 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 30 Dec 2015 13:00:02 +0100 Subject: Fixed problems in cBlockingSslClientSocket. --- src/PolarSSL++/BlockingSslClientSocket.cpp | 28 ++++++++++++++++++++-------- src/PolarSSL++/BlockingSslClientSocket.h | 3 ++- src/Protocol/MojangAPI.cpp | 4 ++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/PolarSSL++/BlockingSslClientSocket.cpp b/src/PolarSSL++/BlockingSslClientSocket.cpp index e789b5374..61aee211d 100644 --- a/src/PolarSSL++/BlockingSslClientSocket.cpp +++ b/src/PolarSSL++/BlockingSslClientSocket.cpp @@ -70,7 +70,9 @@ class cBlockingSslClientSocketLinkCallbacks: { m_Socket.OnDisconnected(); } + public: + cBlockingSslClientSocketLinkCallbacks(cBlockingSslClientSocket & a_Socket): m_Socket(a_Socket) { @@ -143,7 +145,6 @@ bool cBlockingSslClientSocket::Connect(const AString & a_ServerName, UInt16 a_Po return false; } - m_IsConnected = true; return true; } @@ -182,7 +183,11 @@ bool cBlockingSslClientSocket::SetTrustedRootCertsFromString(const AString & a_C bool cBlockingSslClientSocket::Send(const void * a_Data, size_t a_NumBytes) { - ASSERT(m_IsConnected); + if (!m_IsConnected) + { + m_LastErrorText = "Socket is closed"; + return false; + } // Keep sending the data until all of it is sent: const char * Data = reinterpret_cast(a_Data); @@ -216,8 +221,7 @@ bool cBlockingSslClientSocket::Send(const void * a_Data, size_t a_NumBytes) int cBlockingSslClientSocket::Receive(void * a_Data, size_t a_MaxBytes) { - ASSERT(m_IsConnected); - + // Even if m_IsConnected is false (socket disconnected), the SSL context may have more data in the queue int res = m_Ssl.ReadPlain(a_Data, a_MaxBytes); if (res < 0) { @@ -239,9 +243,16 @@ void cBlockingSslClientSocket::Disconnect(void) } m_Ssl.NotifyClose(); - m_Socket->Close(); - m_Socket.reset(); m_IsConnected = false; + + // Grab a copy of the socket so that we know it doesn't change under our hands: + auto socket = m_Socket; + if (socket != nullptr) + { + socket->Close(); + } + + m_Socket.reset(); } @@ -293,6 +304,7 @@ int cBlockingSslClientSocket::SendEncrypted(const unsigned char * a_Buffer, size + void cBlockingSslClientSocket::OnConnected(void) { m_IsConnected = true; @@ -305,7 +317,7 @@ void cBlockingSslClientSocket::OnConnected(void) void cBlockingSslClientSocket::OnConnectError(const AString & a_ErrorMsg) { - LOG("Cannot connect to %s: %s", m_ServerName.c_str(), a_ErrorMsg.c_str()); + LOG("Cannot connect to %s: \"%s\"", m_ServerName.c_str(), a_ErrorMsg.c_str()); m_Event.Set(); } @@ -337,8 +349,8 @@ void cBlockingSslClientSocket::SetLink(cTCPLinkPtr a_Link) void cBlockingSslClientSocket::OnDisconnected(void) { - m_Socket.reset(); m_IsConnected = false; + m_Socket.reset(); m_Event.Set(); } diff --git a/src/PolarSSL++/BlockingSslClientSocket.h b/src/PolarSSL++/BlockingSslClientSocket.h index 462ee95a7..3c61f7f89 100644 --- a/src/PolarSSL++/BlockingSslClientSocket.h +++ b/src/PolarSSL++/BlockingSslClientSocket.h @@ -9,6 +9,7 @@ #pragma once +#include #include "OSSupport/Network.h" #include "CallbackSslContext.h" @@ -81,7 +82,7 @@ protected: AString m_LastErrorText; /** Set to true if the connection established successfully. */ - bool m_IsConnected; + std::atomic m_IsConnected; /** Protects m_IncomingData against multithreaded access. */ cCriticalSection m_CSIncomingData; diff --git a/src/Protocol/MojangAPI.cpp b/src/Protocol/MojangAPI.cpp index dfbf9a568..0edb9f33e 100644 --- a/src/Protocol/MojangAPI.cpp +++ b/src/Protocol/MojangAPI.cpp @@ -748,7 +748,7 @@ void cMojangAPI::QueryNamesToUUIDs(AStringVector & a_NamesToQuery) Json::Reader reader; if (!reader.parse(Response, root, false) || !root.isArray()) { - LOGWARNING("%s failed: Cannot parse received data (NameToUUID) to JSON!", __FUNCTION__); + LOGWARNING("%s failed: Cannot parse received data (NameToUUID) to JSON: \"%s\"", __FUNCTION__, reader.getFormattedErrorMessages().c_str()); LOGD("Response body:\n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str()); continue; } @@ -861,7 +861,7 @@ void cMojangAPI::QueryUUIDToProfile(const AString & a_UUID) Json::Value root; if (!reader.parse(Response, root, false) || !root.isObject()) { - LOGWARNING("%s failed: Cannot parse received data (NameToUUID) to JSON!", __FUNCTION__); + LOGWARNING("%s failed: Cannot parse received data (NameToUUID) to JSON: \"%s\"", __FUNCTION__, reader.getFormattedErrorMessages().c_str()); LOGD("Response body:\n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str()); return; } -- cgit v1.2.3