Skip to content

Commit

Permalink
NetworkClient - close the connection in stop() method (#9542)
Browse files Browse the repository at this point in the history
* 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 <me-no-dev@users.noreply.github.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 10, 2024
1 parent 78bb452 commit e8e251a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions libraries/Network/src/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ class NetworkClientSocketHandle {
NetworkClientSocketHandle(int fd) : sockfd(fd) {}

~NetworkClientSocketHandle() {
close(sockfd);
close();
}

void close() {
if (sockfd >= 0) {
::close(sockfd);
sockfd = -1;
}
}

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

NetworkClient::~NetworkClient() {
stop();
}
NetworkClient::~NetworkClient() {}

void NetworkClient::stop() {
if (clientSocketHandle) {
clientSocketHandle->close();
}
clientSocketHandle = NULL;
_rxBuffer = NULL;
_connected = false;
Expand Down Expand Up @@ -473,7 +481,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) {

int NetworkClient::peek() {
int res = -1;
if (_rxBuffer) {
if (fd() >= 0 && _rxBuffer) {
res = _rxBuffer->peek();
if (_rxBuffer->failed()) {
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
Expand All @@ -484,7 +492,7 @@ int NetworkClient::peek() {
}

int NetworkClient::available() {
if (!_rxBuffer) {
if (fd() < 0 || !_rxBuffer) {
return 0;
}
int res = _rxBuffer->available();
Expand All @@ -502,6 +510,9 @@ void NetworkClient::clear() {
}

uint8_t NetworkClient::connected() {
if (fd() == -1 && _connected) {
stop();
}
if (_connected) {
uint8_t dummy;
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
Expand Down

0 comments on commit e8e251a

Please sign in to comment.