Skip to content

Commit 54dcb55

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

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

libraries/Network/src/NetworkClient.cpp

Lines changed: 16 additions & 3 deletions
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() {
@@ -182,6 +189,9 @@ NetworkClient::~NetworkClient() {
182189
}
183190

184191
void NetworkClient::stop() {
192+
if (clientSocketHandle) {
193+
clientSocketHandle->close();
194+
}
185195
clientSocketHandle = NULL;
186196
_rxBuffer = NULL;
187197
_connected = false;
@@ -473,7 +483,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
473483

474484
int NetworkClient::peek() {
475485
int res = -1;
476-
if (_rxBuffer) {
486+
if (fd() >= 0 && _rxBuffer) {
477487
res = _rxBuffer->peek();
478488
if (_rxBuffer->failed()) {
479489
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
@@ -484,7 +494,7 @@ int NetworkClient::peek() {
484494
}
485495

486496
int NetworkClient::available() {
487-
if (!_rxBuffer) {
497+
if (fd() < 0 || !_rxBuffer) {
488498
return 0;
489499
}
490500
int res = _rxBuffer->available();
@@ -502,6 +512,9 @@ void NetworkClient::clear() {
502512
}
503513

504514
uint8_t NetworkClient::connected() {
515+
if (fd() == -1 && _connected) {
516+
stop();
517+
}
505518
if (_connected) {
506519
uint8_t dummy;
507520
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);

0 commit comments

Comments
 (0)