update espMqttClient

This commit is contained in:
technyon
2024-07-25 13:26:01 +02:00
parent f1f1612cd4
commit d64031d710
22 changed files with 589 additions and 812 deletions

View File

@@ -27,12 +27,12 @@ bool ClientPosix::connect(IPAddress ip, uint16_t port) {
_sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_sockfd < 0) {
emc_log_e("Error %d opening socket", errno);
emc_log_e("Error %d: \"%s\" opening socket", errno, strerror(errno));
}
int flag = 1;
if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) {
emc_log_e("Error %d disabling nagle", errno);
emc_log_e("Error %d: \"%s\" disabling nagle", errno, strerror(errno));
}
memset(&_host, 0, sizeof(_host));
@@ -47,15 +47,17 @@ bool ClientPosix::connect(IPAddress ip, uint16_t port) {
return false;
}
emc_log_i("Connected");
emc_log_i("Socket connected");
return true;
}
bool ClientPosix::connect(const char* host, uint16_t port) {
// tbi
(void) host;
(void) port;
return false;
bool ClientPosix::connect(const char* hostname, uint16_t port) {
IPAddress ipAddress = _hostToIP(hostname);
if (ipAddress == IPAddress(0)) {
emc_log_e("No such host '%s'", hostname);
return false;
}
return connect(ipAddress, port);
}
size_t ClientPosix::write(const uint8_t* buf, size_t size) {
@@ -87,6 +89,42 @@ bool ClientPosix::disconnected() {
return _sockfd < 0;
}
IPAddress ClientPosix::_hostToIP(const char* hostname) {
IPAddress returnIP(0);
struct addrinfo hints, *servinfo, *p;
struct sockaddr_in *h;
int rv;
// Set up request addrinfo struct
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
emc_log_i("Looking for '%s'", hostname);
// ask for host data
if ((rv = getaddrinfo(hostname, NULL, &hints, &servinfo)) != 0) {
emc_log_e("getaddrinfo: %s", gai_strerror(rv));
return returnIP;
}
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
h = (struct sockaddr_in *)p->ai_addr;
returnIP = ::htonl(h->sin_addr.s_addr);
if (returnIP != IPAddress(0)) break;
}
// Release allocated memory
freeaddrinfo(servinfo);
if (returnIP != IPAddress(0)) {
emc_log_i("Host '%s' = %u", hostname, (uint32_t)returnIP);
} else {
emc_log_e("No IP for '%s' found", hostname);
}
return returnIP;
}
} // namespace espMqttClientInternals
#endif

View File

@@ -19,6 +19,7 @@ the LICENSE file.
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "Transport.h" // includes IPAddress
#include "../Logging.h"
@@ -34,7 +35,7 @@ class ClientPosix : public Transport {
ClientPosix();
~ClientPosix();
bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override;
bool connect(const char* hostname, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override;
int read(uint8_t* buf, size_t size) override;
void stop() override;
@@ -44,6 +45,8 @@ class ClientPosix : public Transport {
protected:
int _sockfd;
sockaddr_in _host;
IPAddress _hostToIP(const char* hostname);
};
} // namespace espMqttClientInternals

View File

@@ -29,4 +29,12 @@ IPAddress::operator uint32_t() {
return _address;
}
bool IPAddress::operator==(IPAddress other) {
return _address == other._address;
}
bool IPAddress::operator!=(IPAddress other) {
return _address != other._address;
}
#endif

View File

@@ -18,8 +18,10 @@ class IPAddress {
public:
IPAddress();
IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3);
explicit IPAddress(uint32_t address);
IPAddress(uint32_t address); // NOLINT(runtime/explicit)
operator uint32_t();
bool operator==(IPAddress other);
bool operator!=(IPAddress other);
protected:
uint32_t _address;