void TlsStream::OnEvent(int revents)
{
- int rc, err;
+ int rc;
size_t count;
boost::mutex::scoped_lock lock(m_Mutex);
}
}
+ bool success = false;
+
switch (m_CurrentAction) {
case TlsActionRead:
do {
if (rc > 0) {
m_RecvQ->Write(buffer, rc);
- m_CV.notify_all();
+ success = true;
}
- } while (SSL_pending(m_SSL.get()));
+ } while (rc > 0);
+
+ if (success)
+ m_CV.notify_all();
break;
case TlsActionWrite:
rc = SSL_write(m_SSL.get(), buffer, count);
- if (rc > 0)
+ if (rc > 0) {
m_SendQ->Read(NULL, rc, true);
+ success = true;
+ }
break;
case TlsActionHandshake:
rc = SSL_do_handshake(m_SSL.get());
if (rc > 0) {
+ success = true;
m_HandshakeOK = true;
m_CV.notify_all();
}
VERIFY(!"Invalid TlsAction");
}
- if (rc > 0) {
- m_CurrentAction = TlsActionNone;
+ if (rc < 0) {
+ int err = SSL_get_error(m_SSL.get(), rc);
- if (!m_Eof) {
- if (m_SendQ->GetAvailableBytes() > 0)
- ChangeEvents(POLLIN|POLLOUT);
- else
+ switch (err) {
+ case SSL_ERROR_WANT_READ:
+ m_Retry = true;
ChangeEvents(POLLIN);
- }
- lock.unlock();
+ break;
+ case SSL_ERROR_WANT_WRITE:
+ m_Retry = true;
+ ChangeEvents(POLLOUT);
- while (m_RecvQ->IsDataAvailable() && IsHandlingEvents())
- SignalDataAvailable();
-
- if (m_Shutdown && !m_SendQ->IsDataAvailable())
- Close();
+ break;
+ case SSL_ERROR_ZERO_RETURN:
+ lock.unlock();
- return;
- }
+ Close();
- err = SSL_get_error(m_SSL.get(), rc);
+ break;
+ default:
+ m_ErrorCode = ERR_peek_error();
+ m_ErrorOccurred = true;
- switch (err) {
- case SSL_ERROR_WANT_READ:
- m_Retry = true;
- ChangeEvents(POLLIN);
+ if (m_ErrorCode != 0) {
+ Log(LogWarning, "TlsStream")
+ << "OpenSSL error: " << ERR_error_string(m_ErrorCode, NULL);
+ } else {
+ Log(LogWarning, "TlsStream", "TLS stream was disconnected.");
+ }
- break;
- case SSL_ERROR_WANT_WRITE:
- m_Retry = true;
- ChangeEvents(POLLOUT);
+ lock.unlock();
- break;
- case SSL_ERROR_ZERO_RETURN:
- lock.unlock();
+ Close();
- if (IsHandlingEvents())
- SignalDataAvailable();
+ break;
+ }
+ }
- Close();
+ if (success) {
+ m_CurrentAction = TlsActionNone;
- break;
- default:
- m_ErrorCode = ERR_peek_error();
- m_ErrorOccurred = true;
-
- if (m_ErrorCode != 0) {
- Log(LogWarning, "TlsStream")
- << "OpenSSL error: " << ERR_error_string(m_ErrorCode, NULL);
- } else {
- Log(LogWarning, "TlsStream", "TLS stream was disconnected.");
- }
+ if (!m_Eof) {
+ if (m_SendQ->GetAvailableBytes() > 0)
+ ChangeEvents(POLLIN|POLLOUT);
+ else
+ ChangeEvents(POLLIN);
+ }
- lock.unlock();
+ lock.unlock();
- if (IsHandlingEvents())
- SignalDataAvailable();
+ while (m_RecvQ->IsDataAvailable() && IsHandlingEvents())
+ SignalDataAvailable();
+ if (m_Shutdown && !m_SendQ->IsDataAvailable())
Close();
-
- break;
}
}