Skip to content

Commit

Permalink
Return error if tcp connection is failed
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Jul 10, 2024
1 parent 6d0db8e commit 00211ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/peer_signaling.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ static int peer_signaling_mqtt_connect(const char *hostname, int port) {
bool session_present;

if (ssl_transport_connect(&g_ps.net_ctx, hostname, port, NULL) < 0) {
LOGE("ssl transport connect failed");
return -1;
}

Expand Down Expand Up @@ -446,7 +447,11 @@ int peer_signaling_join_channel() {
return -1;
}

peer_signaling_mqtt_connect(g_ps.mqtt_host, g_ps.mqtt_port);
if (peer_signaling_mqtt_connect(g_ps.mqtt_host, g_ps.mqtt_port) < 0) {
LOGW("Connect MQTT server failed");
return -1;
}

peer_signaling_mqtt_subscribe(1);
return 0;
}
Expand Down
9 changes: 8 additions & 1 deletion src/ssl_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int ssl_transport_connect(NetworkContext_t *net_ctx,

if ((ret = mbedtls_ctr_drbg_seed(&net_ctx->ctr_drbg, mbedtls_entropy_func, &net_ctx->entropy,
(const unsigned char *) pers, strlen(pers))) != 0) {
return -1;
}

if ((ret = mbedtls_ssl_config_defaults(&net_ctx->conf,
Expand All @@ -45,6 +46,7 @@ int ssl_transport_connect(NetworkContext_t *net_ctx,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {

LOGE("ssl config error: -0x%x", (unsigned int) -ret);
return -1;
}


Expand All @@ -63,15 +65,20 @@ int ssl_transport_connect(NetworkContext_t *net_ctx,

if ((ret = mbedtls_ssl_setup(&net_ctx->ssl, &net_ctx->conf)) != 0) {
LOGE("ssl setup error: -0x%x", (unsigned int) -ret);
return -1;
}

if ((ret = mbedtls_ssl_set_hostname(&net_ctx->ssl, host)) != 0) {
LOGE("ssl set hostname error: -0x%x", (unsigned int) -ret);
return -1;
}

tcp_socket_open(&net_ctx->tcp_socket);
tcp_blocking_timeout(&net_ctx->tcp_socket, 1000);
ports_resolve_addr(host, &resolved_addr);
if (ports_resolve_addr(host, &resolved_addr) < 0) {
LOGE("Failed to connect tcp server");
return -1;
}

resolved_addr.port = port;
tcp_socket_connect(&net_ctx->tcp_socket, &resolved_addr);
Expand Down

0 comments on commit 00211ec

Please sign in to comment.