Skip to content

Commit e8e251a

Browse files
JAndrassyme-no-devpre-commit-ci-lite[bot]
authored
NetworkClient - close the connection in stop() method (espressif#9542)
* fix: NetworkClient - close the connection in stop() method for all copies referring it * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Me No Dev <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 78bb452 commit e8e251a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

libraries/Network/src/NetworkClient.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ class NetworkClientSocketHandle {
162162
NetworkClientSocketHandle(int fd) : sockfd(fd) {}
163163

164164
~NetworkClientSocketHandle() {
165-
close(sockfd);
165+
close();
166+
}
167+
168+
void close() {
169+
if (sockfd >= 0) {
170+
::close(sockfd);
171+
sockfd = -1;
172+
}
166173
}
167174

168175
int fd() {
@@ -177,11 +184,12 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE
177184
_rxBuffer.reset(new NetworkClientRxBuffer(fd));
178185
}
179186

180-
NetworkClient::~NetworkClient() {
181-
stop();
182-
}
187+
NetworkClient::~NetworkClient() {}
183188

184189
void NetworkClient::stop() {
190+
if (clientSocketHandle) {
191+
clientSocketHandle->close();
192+
}
185193
clientSocketHandle = NULL;
186194
_rxBuffer = NULL;
187195
_connected = false;
@@ -473,7 +481,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
473481

474482
int NetworkClient::peek() {
475483
int res = -1;
476-
if (_rxBuffer) {
484+
if (fd() >= 0 && _rxBuffer) {
477485
res = _rxBuffer->peek();
478486
if (_rxBuffer->failed()) {
479487
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
@@ -484,7 +492,7 @@ int NetworkClient::peek() {
484492
}
485493

486494
int NetworkClient::available() {
487-
if (!_rxBuffer) {
495+
if (fd() < 0 || !_rxBuffer) {
488496
return 0;
489497
}
490498
int res = _rxBuffer->available();
@@ -502,6 +510,9 @@ void NetworkClient::clear() {
502510
}
503511

504512
uint8_t NetworkClient::connected() {
513+
if (fd() == -1 && _connected) {
514+
stop();
515+
}
505516
if (_connected) {
506517
uint8_t dummy;
507518
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);

0 commit comments

Comments
 (0)