refactor: make SecureSocket write-retry state per connection

doWrite()'s retry/buffer state was held in function-local static
variables, shared across every SecureSocket instance in the process
rather than per connection. Move it to instance members, and simplify
secureRead()/secureWrite()'s retry counters (also static, but not
load-bearing across calls) to plain locals.
This commit is contained in:
ElCruncharino 2026-07-19 00:59:20 -04:00 committed by Chris Rizzitello
parent 59d41b906e
commit 35d877e128
2 changed files with 18 additions and 16 deletions

View file

@ -66,6 +66,7 @@ SecureSocket::SecureSocket(
SecureSocket::~SecureSocket() SecureSocket::~SecureSocket()
{ {
freeSSL(); freeSSL();
free(m_writeStaticBuffer);
} }
void SecureSocket::close() void SecureSocket::close()
@ -174,26 +175,22 @@ TCPSocket::JobResult SecureSocket::doRead()
TCPSocket::JobResult SecureSocket::doWrite() TCPSocket::JobResult SecureSocket::doWrite()
{ {
using enum JobResult; using enum JobResult;
static bool s_retry = false;
static int s_retrySize = 0;
static int s_staticBufferSize = 0;
static void *s_staticBuffer = nullptr;
// write data // write data
int bufferSize = 0; int bufferSize = 0;
int bytesWrote = 0; int bytesWrote = 0;
int status = 0; int status = 0;
if (s_retry) { if (m_writeRetry) {
bufferSize = s_retrySize; bufferSize = m_writeRetrySize;
} else { } else {
bufferSize = m_outputBuffer.getSize(); bufferSize = m_outputBuffer.getSize();
if (bufferSize != 0) { if (bufferSize != 0) {
if (bufferSize > s_staticBufferSize) { if (bufferSize > m_writeStaticBufferSize) {
s_staticBuffer = realloc(s_staticBuffer, bufferSize); m_writeStaticBuffer = realloc(m_writeStaticBuffer, bufferSize);
s_staticBufferSize = bufferSize; m_writeStaticBufferSize = bufferSize;
} }
memcpy(s_staticBuffer, m_outputBuffer.peek(bufferSize), bufferSize); memcpy(m_writeStaticBuffer, m_outputBuffer.peek(bufferSize), bufferSize);
} }
} }
@ -202,14 +199,14 @@ TCPSocket::JobResult SecureSocket::doWrite()
} }
if (isSecureReady()) { if (isSecureReady()) {
status = secureWrite(s_staticBuffer, bufferSize, bytesWrote); status = secureWrite(m_writeStaticBuffer, bufferSize, bytesWrote);
if (status > 0) { if (status > 0) {
s_retry = false; m_writeRetry = false;
} else if (status < 0) { } else if (status < 0) {
return Break; return Break;
} else if (status == 0) { } else if (status == 0) {
s_retry = true; m_writeRetry = true;
s_retrySize = bufferSize; m_writeRetrySize = bufferSize;
return New; return New;
} }
} else { } else {
@ -232,7 +229,7 @@ int SecureSocket::secureRead(void *buffer, int size, int &read)
LOG_VERBOSE("reading secure socket"); LOG_VERBOSE("reading secure socket");
read = SSL_read(m_ssl->m_ssl, buffer, size); read = SSL_read(m_ssl->m_ssl, buffer, size);
static int retry; int retry = 0;
// Check result will cleanup the connection in the case of a fatal // Check result will cleanup the connection in the case of a fatal
checkResult(read, retry); checkResult(read, retry);
@ -260,7 +257,7 @@ int SecureSocket::secureWrite(const void *buffer, int size, int &wrote)
wrote = SSL_write(m_ssl->m_ssl, buffer, size); wrote = SSL_write(m_ssl->m_ssl, buffer, size);
static int retry; int retry = 0;
// Check result will cleanup the connection in the case of a fatal // Check result will cleanup the connection in the case of a fatal
checkResult(wrote, retry); checkResult(wrote, retry);

View file

@ -96,4 +96,9 @@ private:
bool m_secureReady = false; bool m_secureReady = false;
bool m_fatal = false; bool m_fatal = false;
SecurityLevel m_securityLevel = SecurityLevel::Encrypted; SecurityLevel m_securityLevel = SecurityLevel::Encrypted;
bool m_writeRetry = false;
int m_writeRetrySize = 0;
int m_writeStaticBufferSize = 0;
void *m_writeStaticBuffer = nullptr;
}; };