|
| 1 | +From 4369443525c662ddb2ecb15dd9cdba0098e1f01d Mon Sep 17 00:00:00 2001 |
| 2 | + |
| 3 | +Date: Mon, 28 Oct 2024 09:56:31 +0100 |
| 4 | +Subject: [PATCH] TLSSocketWrapper: add read/write event flags |
| 5 | + |
| 6 | + This allows to properly handle timeouts during read write operations |
| 7 | +--- |
| 8 | + .../netsocket/include/netsocket/TLSSocketWrapper.h | 4 ++++ |
| 9 | + connectivity/netsocket/source/TLSSocketWrapper.cpp | 8 ++++---- |
| 10 | + 2 files changed, 8 insertions(+), 4 deletions(-) |
| 11 | + |
| 12 | +diff --git a/connectivity/netsocket/include/netsocket/TLSSocketWrapper.h b/connectivity/netsocket/include/netsocket/TLSSocketWrapper.h |
| 13 | +index 2dc3b4b000..79fe5c564d 100644 |
| 14 | +--- a/connectivity/netsocket/include/netsocket/TLSSocketWrapper.h |
| 15 | ++++ b/connectivity/netsocket/include/netsocket/TLSSocketWrapper.h |
| 16 | +@@ -379,6 +379,10 @@ private: |
| 17 | + Socket *_transport; |
| 18 | + int _timeout = -1; |
| 19 | + |
| 20 | ++ // Event flags |
| 21 | ++ static const int READ_FLAG = 0x1u; |
| 22 | ++ static const int WRITE_FLAG = 0x2u; |
| 23 | ++ |
| 24 | + #ifdef MBEDTLS_X509_CRT_PARSE_C |
| 25 | + mbedtls_x509_crt *_cacert = nullptr; |
| 26 | + mbedtls_x509_crt *_clicert = nullptr; |
| 27 | +diff --git a/connectivity/netsocket/source/TLSSocketWrapper.cpp b/connectivity/netsocket/source/TLSSocketWrapper.cpp |
| 28 | +index c020cd9f59..3a66be2e5e 100644 |
| 29 | +--- a/connectivity/netsocket/source/TLSSocketWrapper.cpp |
| 30 | ++++ b/connectivity/netsocket/source/TLSSocketWrapper.cpp |
| 31 | +@@ -381,7 +381,7 @@ nsapi_error_t TLSSocketWrapper::continue_handshake() |
| 32 | + ret = mbedtls_ssl_handshake(&_ssl); |
| 33 | + if (_timeout && (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)) { |
| 34 | + uint32_t flag; |
| 35 | +- flag = _event_flag.wait_any(1, _timeout); |
| 36 | ++ flag = _event_flag.wait_any(WRITE_FLAG | READ_FLAG, _timeout); |
| 37 | + if (flag & osFlagsError) { |
| 38 | + break; |
| 39 | + } |
| 40 | +@@ -461,7 +461,7 @@ nsapi_error_t TLSSocketWrapper::send(const void *data, nsapi_size_t size) |
| 41 | + break; |
| 42 | + } else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ) { |
| 43 | + uint32_t flag; |
| 44 | +- flag = _event_flag.wait_any(1, _timeout); |
| 45 | ++ flag = _event_flag.wait_any(WRITE_FLAG, _timeout); |
| 46 | + if (flag & osFlagsError) { |
| 47 | + // Timeout break |
| 48 | + break; |
| 49 | +@@ -522,7 +522,7 @@ nsapi_size_or_error_t TLSSocketWrapper::recv(void *data, nsapi_size_t size) |
| 50 | + break; |
| 51 | + } else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ) { |
| 52 | + uint32_t flag; |
| 53 | +- flag = _event_flag.wait_any(1, _timeout); |
| 54 | ++ flag = _event_flag.wait_any(READ_FLAG, _timeout); |
| 55 | + if (flag & osFlagsError) { |
| 56 | + // Timeout break |
| 57 | + break; |
| 58 | +@@ -855,7 +855,7 @@ nsapi_error_t TLSSocketWrapper::listen(int) |
| 59 | + |
| 60 | + void TLSSocketWrapper::event() |
| 61 | + { |
| 62 | +- _event_flag.set(1); |
| 63 | ++ _event_flag.set(READ_FLAG | WRITE_FLAG); |
| 64 | + if (_sigio) { |
| 65 | + _sigio(); |
| 66 | + } |
| 67 | +-- |
| 68 | +2.45.2 |
| 69 | + |
0 commit comments