mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
use original formatting
This commit is contained in:
@@ -13,81 +13,80 @@ the LICENSE file.
|
|||||||
namespace espMqttClientInternals {
|
namespace espMqttClientInternals {
|
||||||
|
|
||||||
ClientPosix::ClientPosix()
|
ClientPosix::ClientPosix()
|
||||||
: _sockfd(-1)
|
: _sockfd(-1)
|
||||||
, _host() {
|
, _host() {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientPosix::~ClientPosix() {
|
ClientPosix::~ClientPosix() {
|
||||||
ClientPosix::stop();
|
ClientPosix::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientPosix::connect(IPAddress ip, uint16_t port) {
|
bool ClientPosix::connect(IPAddress ip, uint16_t port) {
|
||||||
if (connected())
|
if (connected()) stop();
|
||||||
stop();
|
|
||||||
|
|
||||||
_sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
|
_sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (_sockfd < 0) {
|
if (_sockfd < 0) {
|
||||||
emc_log_e("Error %d opening socket", errno);
|
emc_log_e("Error %d opening socket", errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flag = 1;
|
int flag = 1;
|
||||||
if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) {
|
if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) {
|
||||||
emc_log_e("Error %d disabling nagle", errno);
|
emc_log_e("Error %d disabling nagle", errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&_host, 0, sizeof(_host));
|
memset(&_host, 0, sizeof(_host));
|
||||||
_host.sin_family = AF_INET;
|
_host.sin_family = AF_INET;
|
||||||
_host.sin_addr.s_addr = htonl(uint32_t(ip));
|
_host.sin_addr.s_addr = htonl(uint32_t(ip));
|
||||||
_host.sin_port = htons(port);
|
_host.sin_port = ::htons(port);
|
||||||
|
|
||||||
int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host));
|
int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host));
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno));
|
emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
emc_log_i("Connected");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ClientPosix::connect(const char * host, uint16_t port) {
|
|
||||||
// tbi
|
|
||||||
(void)host;
|
|
||||||
(void)port;
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
emc_log_i("Connected");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ClientPosix::write(const uint8_t * buf, size_t size) {
|
bool ClientPosix::connect(const char* host, uint16_t port) {
|
||||||
return ::send(_sockfd, buf, size, 0);
|
// tbi
|
||||||
|
(void) host;
|
||||||
|
(void) port;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientPosix::read(uint8_t * buf, size_t size) {
|
size_t ClientPosix::write(const uint8_t* buf, size_t size) {
|
||||||
int ret = ::recv(_sockfd, buf, size, MSG_DONTWAIT);
|
return ::send(_sockfd, buf, size, 0);
|
||||||
/*
|
}
|
||||||
|
|
||||||
|
int ClientPosix::read(uint8_t* buf, size_t size) {
|
||||||
|
int ret = ::recv(_sockfd, buf, size, MSG_DONTWAIT);
|
||||||
|
/*
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
emc_log_e("Error reading: %s", strerror(errno));
|
emc_log_e("Error reading: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientPosix::stop() {
|
void ClientPosix::stop() {
|
||||||
if (_sockfd >= 0) {
|
if (_sockfd >= 0) {
|
||||||
::close(_sockfd);
|
::close(_sockfd);
|
||||||
_sockfd = -1;
|
_sockfd = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientPosix::connected() {
|
bool ClientPosix::connected() {
|
||||||
return _sockfd >= 0;
|
return _sockfd >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientPosix::disconnected() {
|
bool ClientPosix::disconnected() {
|
||||||
return _sockfd < 0;
|
return _sockfd < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user