Skip to content

Commit 6395381

Browse files
committed
Refactored close functionality
1 parent 63dc3fd commit 6395381

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

main/wifi.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void wifi_ctrl(void* _param) {
241241
wifi_init_sta(ssid, key);
242242
} else {
243243
wifi_init_softap(ssid, key);
244-
}
244+
}
245245
} else {
246246
ESP_LOGW(TAG, "No SSID set, cannot start wifi");
247247
}
@@ -251,6 +251,13 @@ static void wifi_ctrl(void* _param) {
251251
}
252252
}
253253

254+
static void close_client_socket()
255+
{
256+
close(clientConnection);
257+
clientConnection = NO_CONNECTION;
258+
xEventGroupSetBits(s_wifi_event_group, WIFI_SOCKET_DISCONNECTED);
259+
}
260+
254261
void wifi_bind_socket() {
255262
char addr_str[128];
256263
int addr_family;
@@ -285,7 +292,6 @@ void wifi_wait_for_socket_connected() {
285292
ESP_LOGI(TAG, "Waiting for connection");
286293
struct sockaddr sourceAddr;
287294
uint addrLen = sizeof(sourceAddr);
288-
ESP_LOGE(TAG, "Waiting for connection");
289295
clientConnection = accept(serverSock, (struct sockaddr *)&sourceAddr, &addrLen);
290296
if (clientConnection < 0) {
291297
ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
@@ -379,9 +385,7 @@ void wifi_send_packet(const char * buffer, size_t size) {
379385
int err = send(clientConnection, buffer, size, 0);
380386
if (err < 0) {
381387
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
382-
close(clientConnection);
383-
clientConnection = NO_CONNECTION;
384-
xEventGroupSetBits(s_wifi_event_group, WIFI_SOCKET_DISCONNECTED);
388+
close_client_socket();
385389

386390
}
387391
xEventGroupSetBits(s_wifi_event_group, WIFI_PACKET_WAIT_SEND);
@@ -424,11 +428,7 @@ static void wifi_receiving_task(void *pvParameters) {
424428
ESP_LOG_BUFFER_HEX_LEVEL(TAG, &rxp_wifi, 10, ESP_LOG_DEBUG);
425429
xQueueSend(wifiRxQueue, &rxp_wifi, portMAX_DELAY);
426430
} else if (len == 0) {
427-
//vTaskDelay(10);
428-
close(clientConnection); //Reading 0 bytes most often means the client has disconnected
429-
clientConnection = NO_CONNECTION;
430-
xEventGroupSetBits(s_wifi_event_group, WIFI_SOCKET_DISCONNECTED);
431-
//printf("No data!\n");
431+
close_client_socket(); //Reading 0 bytes most often means the client has disconnected.
432432
} else {
433433
vTaskDelay(10);
434434
}

0 commit comments

Comments
 (0)