@@ -524,27 +524,21 @@ void write(client_context_t *context, byte *data, int data_size) {
524524 CLIENT_ERROR (context, " The socket is null! (or is closed)" );
525525 return ;
526526 }
527+ if (context->disconnect ) {
528+ context->error_write = true ;
529+ return ;
530+ }
527531 if (context->error_write ) {
528532 CLIENT_ERROR (context, " Abort write data since error_write." );
529533 return ;
530534 }
531535 int write_size = context->socket ->write (data, data_size);
532536 CLIENT_DEBUG (context, " Sending data of size %d" , data_size);
533537 if (write_size != data_size) {
534- CLIENT_ERROR (context, " socket.write, data_size=%d, write_size=%d" , data_size, write_size);
535538 context->error_write = true ;
536- // Error write when :
537- // 1. remote client is disconnected
538- // 2. data_size is larger than the tcp internal send buffer
539- // But We has limited the data_size to 538, and TCP_SND_BUF = 1072. (See the comments on HOMEKIT_JSONBUFFER_SIZE)
540- // So we believe here is disconnected.
541- context->disconnect = true ;
542- homekit_server_close_client (context->server , context);
543- // We consider the socket is 'closed' when error in writing (eg. the remote client is disconnected, NO tcp ack receive).
544- // Closing the socket causes memory-leak if some data has not been sent (the write_buffer did not free)
545- // To fix this memory-leak, add tcp_abandon(_pcb, 0); in ClientContext.h of ESP8266WiFi-library.
539+ context->socket ->keepAlive (1 , 1 , 1 ); // fast disconnected internally in 1 second.
540+ CLIENT_ERROR (context, " socket.write, data_size=%d, write_size=%d" , data_size, write_size);
546541 }
547-
548542}
549543
550544int client_send_encrypted_ (client_context_t *context,
0 commit comments