Converting retry to a counter #4650

This commit is contained in:
Adam Potolsky 2015-05-20 12:08:25 -07:00
parent 1fd5883620
commit 994a9433fe
2 changed files with 23 additions and 12 deletions

View File

@ -111,7 +111,9 @@ SecureSocket::secureRead(void* buffer, UInt32 n)
LOG((CLOG_DEBUG2 "reading secure socket")); LOG((CLOG_DEBUG2 "reading secure socket"));
r = SSL_read(m_ssl->m_ssl, buffer, n); r = SSL_read(m_ssl->m_ssl, buffer, n);
bool fatal, retry; bool fatal;
static int retry;
checkResult(r, fatal, retry); checkResult(r, fatal, retry);
if (retry) { if (retry) {
@ -130,7 +132,9 @@ SecureSocket::secureWrite(const void* buffer, UInt32 n)
LOG((CLOG_DEBUG2 "writing secure socket")); LOG((CLOG_DEBUG2 "writing secure socket"));
r = SSL_write(m_ssl->m_ssl, buffer, n); r = SSL_write(m_ssl->m_ssl, buffer, n);
bool fatal, retry; bool fatal;
static int retry;
checkResult(r, fatal, retry); checkResult(r, fatal, retry);
if (retry) { if (retry) {
@ -253,7 +257,9 @@ SecureSocket::secureAccept(int socket)
LOG((CLOG_DEBUG2 "accepting secure socket")); LOG((CLOG_DEBUG2 "accepting secure socket"));
int r = SSL_accept(m_ssl->m_ssl); int r = SSL_accept(m_ssl->m_ssl);
bool fatal, retry; bool fatal;
static int retry;
checkResult(r, fatal, retry); checkResult(r, fatal, retry);
if (fatal) { if (fatal) {
@ -263,12 +269,13 @@ SecureSocket::secureAccept(int socket)
ARCH->sleep(1); ARCH->sleep(1);
} }
m_secureReady = !retry; m_secureReady = (0 == retry) ? true : false;
if (m_secureReady) { if (m_secureReady) {
LOG((CLOG_INFO "accepted secure socket")); LOG((CLOG_INFO "accepted secure socket"));
} }
return retry; return !m_secureReady;
} }
bool bool
@ -282,7 +289,9 @@ SecureSocket::secureConnect(int socket)
LOG((CLOG_DEBUG2 "connecting secure socket")); LOG((CLOG_DEBUG2 "connecting secure socket"));
int r = SSL_connect(m_ssl->m_ssl); int r = SSL_connect(m_ssl->m_ssl);
bool fatal, retry; bool fatal;
static int retry;
checkResult(r, fatal, retry); checkResult(r, fatal, retry);
if (fatal) { if (fatal) {
@ -291,7 +300,7 @@ SecureSocket::secureConnect(int socket)
return false; return false;
} }
m_secureReady = !retry; m_secureReady = (0 == retry) ? true : false;
if (m_secureReady) { if (m_secureReady) {
if (verifyCertFingerprint()) { if (verifyCertFingerprint()) {
@ -306,7 +315,7 @@ SecureSocket::secureConnect(int socket)
} }
} }
return retry; return !m_secureReady;
} }
bool bool
@ -332,22 +341,23 @@ SecureSocket::showCertificate()
} }
void void
SecureSocket::checkResult(int n, bool& fatal, bool& retry) SecureSocket::checkResult(int n, bool& fatal, int& retry)
{ {
// ssl errors are a little quirky. the "want" errors are normal and // ssl errors are a little quirky. the "want" errors are normal and
// should result in a retry. // should result in a retry.
fatal = false; fatal = false;
retry = false;
int errorCode = SSL_get_error(m_ssl->m_ssl, n); int errorCode = SSL_get_error(m_ssl->m_ssl, n);
switch (errorCode) { switch (errorCode) {
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
retry = 0;
// operation completed // operation completed
break; break;
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
// connection closed // connection closed
retry = 0;
LOG((CLOG_DEBUG2 "SSL connection has been closed")); LOG((CLOG_DEBUG2 "SSL connection has been closed"));
break; break;
@ -356,7 +366,7 @@ SecureSocket::checkResult(int n, bool& fatal, bool& retry)
case SSL_ERROR_WANT_CONNECT: case SSL_ERROR_WANT_CONNECT:
case SSL_ERROR_WANT_ACCEPT: case SSL_ERROR_WANT_ACCEPT:
LOG((CLOG_DEBUG2 "need to retry the same SSL function")); LOG((CLOG_DEBUG2 "need to retry the same SSL function"));
retry = true; retry += 1;
break; break;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
@ -391,6 +401,7 @@ SecureSocket::checkResult(int n, bool& fatal, bool& retry)
} }
if (fatal) { if (fatal) {
retry = 0;
showError(); showError();
disconnect(); disconnect();
} }

View File

@ -58,7 +58,7 @@ private:
bool secureAccept(int s); bool secureAccept(int s);
bool secureConnect(int s); bool secureConnect(int s);
bool showCertificate(); bool showCertificate();
void checkResult(int n, bool& fatal, bool& retry); void checkResult(int n, bool& fatal, int& retry);
void showError(const char* reason = NULL); void showError(const char* reason = NULL);
String getError(); String getError();
void disconnect(); void disconnect();