better resource management for SecureSocket buffer. fixes #16
This commit is contained in:
parent
b994c94a90
commit
916b085474
|
@ -30,6 +30,7 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
//
|
//
|
||||||
// SecureSocket
|
// SecureSocket
|
||||||
|
@ -206,45 +207,43 @@ SecureSocket::doWrite()
|
||||||
{
|
{
|
||||||
static bool s_retry = false;
|
static bool s_retry = false;
|
||||||
static int s_retrySize = 0;
|
static int s_retrySize = 0;
|
||||||
static void* s_staticBuffer = NULL;
|
static std::unique_ptr<char[]> s_staticBuffer;
|
||||||
|
static std::size_t s_staticBufferSize = 0;
|
||||||
|
|
||||||
// write data
|
// write data
|
||||||
int bufferSize = 0;
|
int bufferSize = 0;
|
||||||
int bytesWrote = 0;
|
int bytesWrote = 0;
|
||||||
int status = 0;
|
int status = 0;
|
||||||
|
|
||||||
|
if (!isSecureReady())
|
||||||
|
return kRetry;
|
||||||
|
|
||||||
if (s_retry) {
|
if (s_retry) {
|
||||||
bufferSize = s_retrySize;
|
bufferSize = s_retrySize;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
bufferSize = m_outputBuffer.getSize();
|
bufferSize = m_outputBuffer.getSize();
|
||||||
s_staticBuffer = malloc(bufferSize);
|
if (bufferSize > s_staticBufferSize) {
|
||||||
memcpy(s_staticBuffer, m_outputBuffer.peek(bufferSize), bufferSize);
|
s_staticBuffer.reset(new char[bufferSize]);
|
||||||
|
s_staticBufferSize = bufferSize;
|
||||||
|
}
|
||||||
|
if (bufferSize > 0) {
|
||||||
|
memcpy(s_staticBuffer.get(), m_outputBuffer.peek(bufferSize), bufferSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bufferSize == 0) {
|
if (bufferSize == 0) {
|
||||||
return kRetry;
|
return kRetry;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSecureReady()) {
|
status = secureWrite(s_staticBuffer.get(), bufferSize, bytesWrote);
|
||||||
status = secureWrite(s_staticBuffer, bufferSize, bytesWrote);
|
if (status > 0) {
|
||||||
if (status > 0) {
|
s_retry = false;
|
||||||
s_retry = false;
|
} else if (status < 0) {
|
||||||
bufferSize = 0;
|
return kBreak;
|
||||||
free(s_staticBuffer);
|
} else if (status == 0) {
|
||||||
s_staticBuffer = NULL;
|
s_retry = true;
|
||||||
}
|
s_retrySize = bufferSize;
|
||||||
else if (status < 0) {
|
return kNew;
|
||||||
return kBreak;
|
|
||||||
}
|
|
||||||
else if (status == 0) {
|
|
||||||
s_retry = true;
|
|
||||||
s_retrySize = bufferSize;
|
|
||||||
return kNew;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return kRetry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bytesWrote > 0) {
|
if (bytesWrote > 0) {
|
||||||
|
|
Loading…
Reference in New Issue