Unverified Commit e8e251ab authored by Juraj Andrássy's avatar Juraj Andrássy Committed by GitHub

NetworkClient - close the connection in stop() method (#9542)

* fix: NetworkClient - close the connection in stop() method

for all copies referring it

* ci(pre-commit): Apply automatic fixes

---------
Co-authored-by: default avatarMe No Dev <me-no-dev@users.noreply.github.com>
Co-authored-by: default avatarpre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
parent 78bb452f
...@@ -162,7 +162,14 @@ public: ...@@ -162,7 +162,14 @@ public:
NetworkClientSocketHandle(int fd) : sockfd(fd) {} NetworkClientSocketHandle(int fd) : sockfd(fd) {}
~NetworkClientSocketHandle() { ~NetworkClientSocketHandle() {
close(sockfd); close();
}
void close() {
if (sockfd >= 0) {
::close(sockfd);
sockfd = -1;
}
} }
int fd() { int fd() {
...@@ -177,11 +184,12 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE ...@@ -177,11 +184,12 @@ NetworkClient::NetworkClient(int fd) : _connected(true), _timeout(WIFI_CLIENT_DE
_rxBuffer.reset(new NetworkClientRxBuffer(fd)); _rxBuffer.reset(new NetworkClientRxBuffer(fd));
} }
NetworkClient::~NetworkClient() { NetworkClient::~NetworkClient() {}
stop();
}
void NetworkClient::stop() { void NetworkClient::stop() {
if (clientSocketHandle) {
clientSocketHandle->close();
}
clientSocketHandle = NULL; clientSocketHandle = NULL;
_rxBuffer = NULL; _rxBuffer = NULL;
_connected = false; _connected = false;
...@@ -473,7 +481,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) { ...@@ -473,7 +481,7 @@ int NetworkClient::read(uint8_t *buf, size_t size) {
int NetworkClient::peek() { int NetworkClient::peek() {
int res = -1; int res = -1;
if (_rxBuffer) { if (fd() >= 0 && _rxBuffer) {
res = _rxBuffer->peek(); res = _rxBuffer->peek();
if (_rxBuffer->failed()) { if (_rxBuffer->failed()) {
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno)); log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
...@@ -484,7 +492,7 @@ int NetworkClient::peek() { ...@@ -484,7 +492,7 @@ int NetworkClient::peek() {
} }
int NetworkClient::available() { int NetworkClient::available() {
if (!_rxBuffer) { if (fd() < 0 || !_rxBuffer) {
return 0; return 0;
} }
int res = _rxBuffer->available(); int res = _rxBuffer->available();
...@@ -502,6 +510,9 @@ void NetworkClient::clear() { ...@@ -502,6 +510,9 @@ void NetworkClient::clear() {
} }
uint8_t NetworkClient::connected() { uint8_t NetworkClient::connected() {
if (fd() == -1 && _connected) {
stop();
}
if (_connected) { if (_connected) {
uint8_t dummy; uint8_t dummy;
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT); int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment