Better connect() result test

This commit is contained in:
Sergey Lyubka committed 2022-06-29 19:19:19 +01:00
1 parent 89e2a15eb7
commit 5b8979a0f1
3 files changed
+29 -10

No files matched your search

+14 -5
View File
@@ -4166,6 +4166,19 @@ static void close_conn(struct mg_connection *c) {
mg_close_conn(c);
}
static void connect_conn(struct mg_connection *c) {
union usa usa;
socklen_t n = sizeof(usa);
// Use getpeername() to test whether we have connected
if (getpeername(FD(c), &usa.sa, &n) == 0) {
c->is_connecting = 0;
mg_call(c, MG_EV_CONNECT, NULL);
if (c->is_tls_hs) mg_tls_handshake(c);
} else {
mg_error(c, "socket error");
}
}
static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS || \
MG_ARCH == MG_ARCH_TIRTOS
@@ -4453,11 +4466,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
} else if (c->is_listening && c->is_udp == 0) {
if (c->is_readable) accept_conn(mgr, c);
} else if (c->is_connecting) {
if (c->is_readable || c->is_writable) {
c->is_connecting = 0;
mg_call(c, MG_EV_CONNECT, NULL);
if (c->is_tls_hs) mg_tls_handshake(c);
}
if (c->is_readable || c->is_writable) connect_conn(c);
} else if (c->is_tls_hs) {
if ((c->is_readable || c->is_writable)) mg_tls_handshake(c);
} else {
+14 -5
View File
@@ -313,6 +313,19 @@ static void close_conn(struct mg_connection *c) {
mg_close_conn(c);
}
static void connect_conn(struct mg_connection *c) {
union usa usa;
socklen_t n = sizeof(usa);
// Use getpeername() to test whether we have connected
if (getpeername(FD(c), &usa.sa, &n) == 0) {
c->is_connecting = 0;
mg_call(c, MG_EV_CONNECT, NULL);
if (c->is_tls_hs) mg_tls_handshake(c);
} else {
mg_error(c, "socket error");
}
}
static void setsockopts(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS || \
MG_ARCH == MG_ARCH_TIRTOS
@@ -600,11 +613,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
} else if (c->is_listening && c->is_udp == 0) {
if (c->is_readable) accept_conn(mgr, c);
} else if (c->is_connecting) {
if (c->is_readable || c->is_writable) {
c->is_connecting = 0;
mg_call(c, MG_EV_CONNECT, NULL);
if (c->is_tls_hs) mg_tls_handshake(c);
}
if (c->is_readable || c->is_writable) connect_conn(c);
} else if (c->is_tls_hs) {
if ((c->is_readable || c->is_writable)) mg_tls_handshake(c);
} else {
+1
View File
@@ -732,6 +732,7 @@ static void test_http_server(void) {
int i, errored = 0;
mg_connect(&mgr, "tcp://127.0.0.1:55117", eh9, &errored);
for (i = 0; i < 10 && errored == 0; i++) mg_mgr_poll(&mgr, 1);
MG_INFO(("errored: %d, expected: 7", errored));
ASSERT(errored == 7);
}