diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index 89e682f..b888b18 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -139,6 +139,7 @@ void HTTPConnection::closeConnection() { if (_wsHandler != nullptr) { HTTPS_LOGD("Free WS Handler"); + _wsHandler->onClose(); delete _wsHandler; _wsHandler = NULL; } @@ -206,7 +207,7 @@ int HTTPConnection::updateBuffer() { } else { // An error occured _connectionState = STATE_ERROR; - HTTPS_LOGE("An receive error occured, FID=%d", _socket); + HTTPS_LOGE("An receive error occurred, FID=%d, code=%d", _socket, readReturnCode); closeConnection(); return -1; } @@ -254,7 +255,7 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) { size_t HTTPConnection::pendingBufferSize() { updateBuffer(); - + if (isClosed()) return 0; return _bufferUnusedIdx - _bufferProcessed + pendingByteCount(); } @@ -586,11 +587,15 @@ void HTTPConnection::loop() { } // If the handler has terminated the connection, clean up and close the socket too - if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) { - HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket); - delete _wsHandler; - _wsHandler = nullptr; - _connectionState = STATE_CLOSING; + if (_wsHandler != nullptr){ + if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) { + HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket); + delete _wsHandler; + _wsHandler = nullptr; + _connectionState = STATE_CLOSING; + } + } else { + HTTPS_LOGI("WS closed due to SSL level issue and cleanded up"); } break; default:; diff --git a/src/HTTPSConnection.cpp b/src/HTTPSConnection.cpp index e0e3dd0..e500ff9 100644 --- a/src/HTTPSConnection.cpp +++ b/src/HTTPSConnection.cpp @@ -109,7 +109,11 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) { } size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) { - return SSL_read(_ssl, buffer, length); + int ret = SSL_read(_ssl, buffer, length); + if (ret < 0) { + HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret)); + } + return ret; } size_t HTTPSConnection::pendingByteCount() {