Reuse sock.c logic in mbedtls integration

This commit is contained in:
Sergey Lyubka committed 2022-03-21 14:39:27 +00:00
1 parent 7ef82f5caf
commit 0fbc4b8ff4
3 files changed
+10 -14

No files matched your search

+5 -7
View File
@@ -3238,7 +3238,8 @@ static void tomgaddr(union usa *usa, struct mg_addr *a, bool is_ip6) {
#endif #endif
} }
static bool mg_sock_would_block(void) { bool mg_sock_would_block(void);
bool mg_sock_would_block(void) {
int err = MG_SOCK_ERRNO; int err = MG_SOCK_ERRNO;
return err == EINPROGRESS || err == EWOULDBLOCK return err == EINPROGRESS || err == EWOULDBLOCK
#ifndef WINCE #ifndef WINCE
@@ -4307,10 +4308,7 @@ void mg_tls_free(struct mg_connection *c) {
} }
} }
static bool mg_wouldblock(int n) { bool mg_sock_would_block(void);
return n < 0 &&
(errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK);
}
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
struct mg_connection *c = (struct mg_connection *) ctx; struct mg_connection *c = (struct mg_connection *) ctx;
@@ -4318,7 +4316,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
int n = (int) send(fd, buf, len, 0); int n = (int) send(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n > 0) return n;
if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED; return MBEDTLS_ERR_NET_SEND_FAILED;
} }
@@ -4328,7 +4326,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
n = (int) recv(fd, buf, len, 0); n = (int) recv(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n > 0) return n;
if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED; return MBEDTLS_ERR_NET_RECV_FAILED;
} }
+2 -1
View File
@@ -75,7 +75,8 @@ static void tomgaddr(union usa *usa, struct mg_addr *a, bool is_ip6) {
#endif #endif
} }
static bool mg_sock_would_block(void) { bool mg_sock_would_block(void);
bool mg_sock_would_block(void) {
int err = MG_SOCK_ERRNO; int err = MG_SOCK_ERRNO;
return err == EINPROGRESS || err == EWOULDBLOCK return err == EINPROGRESS || err == EWOULDBLOCK
#ifndef WINCE #ifndef WINCE
+3 -6
View File
@@ -24,10 +24,7 @@ void mg_tls_free(struct mg_connection *c) {
} }
} }
static bool mg_wouldblock(int n) { bool mg_sock_would_block(void);
return n < 0 &&
(errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK);
}
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
struct mg_connection *c = (struct mg_connection *) ctx; struct mg_connection *c = (struct mg_connection *) ctx;
@@ -35,7 +32,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
int n = (int) send(fd, buf, len, 0); int n = (int) send(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n > 0) return n;
if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED; return MBEDTLS_ERR_NET_SEND_FAILED;
} }
@@ -45,7 +42,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
n = (int) recv(fd, buf, len, 0); n = (int) recv(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n > 0) return n;
if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED; return MBEDTLS_ERR_NET_RECV_FAILED;
} }