diff --git a/src/arduino_homekit_server.cpp b/src/arduino_homekit_server.cpp index 7121914..6625ee2 100644 --- a/src/arduino_homekit_server.cpp +++ b/src/arduino_homekit_server.cpp @@ -14,7 +14,7 @@ #include //wc_sha512 #include "constants.h" -#include "base64.h" +#include "homekit_base64.h" #include "pairing.h" #include "storage.h" #include "query_params.h" @@ -524,6 +524,10 @@ void write(client_context_t *context, byte *data, int data_size) { CLIENT_ERROR(context, "The socket is null! (or is closed)"); return; } + if (context->disconnect) { + context->error_write = true; + return; + } if (context->error_write) { CLIENT_ERROR(context, "Abort write data since error_write."); return; @@ -531,20 +535,10 @@ void write(client_context_t *context, byte *data, int data_size) { int write_size = context->socket->write(data, data_size); CLIENT_DEBUG(context, "Sending data of size %d", data_size); if (write_size != data_size) { - CLIENT_ERROR(context, "socket.write, data_size=%d, write_size=%d", data_size, write_size); context->error_write = true; - // Error write when : - // 1. remote client is disconnected - // 2. data_size is larger than the tcp internal send buffer - // But We has limited the data_size to 538, and TCP_SND_BUF = 1072. (See the comments on HOMEKIT_JSONBUFFER_SIZE) - // So we believe here is disconnected. - context->disconnect = true; - homekit_server_close_client(context->server, context); - // We consider the socket is 'closed' when error in writing (eg. the remote client is disconnected, NO tcp ack receive). - // Closing the socket causes memory-leak if some data has not been sent (the write_buffer did not free) - // To fix this memory-leak, add tcp_abandon(_pcb, 0); in ClientContext.h of ESP8266WiFi-library. + context->socket->keepAlive(1, 1, 1); // fast disconnected internally in 1 second. + CLIENT_ERROR(context, "socket.write, data_size=%d, write_size=%d", data_size, write_size); } - } int client_send_encrypted_(client_context_t *context, @@ -3162,7 +3156,7 @@ void homekit_mdns_init(homekit_server_t *server) { } if (homekit_mdns_started) { - MDNS.close(); + // MDNS.close(); MDNS.begin(name->value.string_value, staIP); INFO("MDNS restart: %s, IP: %s", name->value.string_value, staIP.toString().c_str()); MDNS.announce();