Skip to content

Commit aaf6cae

Browse files
committed
fix: NetworkClient - close the connection in stop() method
for all copies referring it
1 parent cf44890 commit aaf6cae

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Diff for: libraries/Network/src/NetworkClient.cpp

+16-4
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() {
@@ -178,10 +185,12 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE
178185
}
179186

180187
NetworkClient::~NetworkClient() {
181-
stop();
182188
}
183189

184190
void NetworkClient::stop() {
191+
if (clientSocketHandle) {
192+
clientSocketHandle->close();
193+
}
185194
clientSocketHandle = NULL;
186195
_rxBuffer = NULL;
187196
_connected = false;
@@ -473,7 +482,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
473482

474483
int NetworkClient::peek() {
475484
int res = -1;
476-
if (_rxBuffer) {
485+
if (fd() >= 0 && _rxBuffer) {
477486
res = _rxBuffer->peek();
478487
if (_rxBuffer->failed()) {
479488
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
@@ -484,7 +493,7 @@ int NetworkClient::peek() {
484493
}
485494

486495
int NetworkClient::available() {
487-
if (!_rxBuffer) {
496+
if (fd() < 0 || !_rxBuffer) {
488497
return 0;
489498
}
490499
int res = _rxBuffer->available();
@@ -502,6 +511,9 @@ void NetworkClient::clear() {
502511
}
503512

504513
uint8_t NetworkClient::connected() {
514+
if (fd() == -1 && _connected) {
515+
stop();
516+
}
505517
if (_connected) {
506518
uint8_t dummy;
507519
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);

0 commit comments

Comments
 (0)