mirror of
https://github.com/esphome/esphome.git
synced 2024-12-19 16:07:47 +01:00
Re-enable TCP nodelay for ESP32 (#2390)
This commit is contained in:
parent
756c6721e9
commit
97e76d64d6
@ -127,6 +127,14 @@ APIError APINoiseFrameHelper::init() {
|
|||||||
return APIError::TCP_NONBLOCKING_FAILED;
|
return APIError::TCP_NONBLOCKING_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int enable = 1;
|
||||||
|
err = socket_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nodelay failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NODELAY_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
// init prologue
|
// init prologue
|
||||||
prologue_.insert(prologue_.end(), PROLOGUE_INIT, PROLOGUE_INIT + strlen(PROLOGUE_INIT));
|
prologue_.insert(prologue_.end(), PROLOGUE_INIT, PROLOGUE_INIT + strlen(PROLOGUE_INIT));
|
||||||
|
|
||||||
@ -722,6 +730,13 @@ APIError APIPlaintextFrameHelper::init() {
|
|||||||
HELPER_LOG("Setting nonblocking failed with errno %d", errno);
|
HELPER_LOG("Setting nonblocking failed with errno %d", errno);
|
||||||
return APIError::TCP_NONBLOCKING_FAILED;
|
return APIError::TCP_NONBLOCKING_FAILED;
|
||||||
}
|
}
|
||||||
|
int enable = 1;
|
||||||
|
err = socket_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
|
||||||
|
if (err != 0) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Setting nodelay failed with errno %d", errno);
|
||||||
|
return APIError::TCP_NODELAY_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
state_ = State::DATA;
|
state_ = State::DATA;
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
|
@ -257,7 +257,7 @@ class LWIPRawImpl : public Socket {
|
|||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
*reinterpret_cast<int *>(optval) = tcp_nagle_disabled(pcb_);
|
*reinterpret_cast<int *>(optval) = nodelay_;
|
||||||
*optlen = 4;
|
*optlen = 4;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -286,11 +286,7 @@ class LWIPRawImpl : public Socket {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int val = *reinterpret_cast<const int *>(optval);
|
int val = *reinterpret_cast<const int *>(optval);
|
||||||
if (val != 0) {
|
nodelay_ = val;
|
||||||
tcp_nagle_disable(pcb_);
|
|
||||||
} else {
|
|
||||||
tcp_nagle_enable(pcb_);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -444,9 +440,11 @@ class LWIPRawImpl : public Socket {
|
|||||||
if (written == 0)
|
if (written == 0)
|
||||||
// no need to output if nothing written
|
// no need to output if nothing written
|
||||||
return 0;
|
return 0;
|
||||||
int err = internal_output();
|
if (nodelay_) {
|
||||||
if (err == -1)
|
int err = internal_output();
|
||||||
return -1;
|
if (err == -1)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
ssize_t writev(const struct iovec *iov, int iovcnt) override {
|
ssize_t writev(const struct iovec *iov, int iovcnt) override {
|
||||||
@ -466,9 +464,11 @@ class LWIPRawImpl : public Socket {
|
|||||||
if (written == 0)
|
if (written == 0)
|
||||||
// no need to output if nothing written
|
// no need to output if nothing written
|
||||||
return 0;
|
return 0;
|
||||||
int err = internal_output();
|
if (nodelay_) {
|
||||||
if (err == -1)
|
int err = internal_output();
|
||||||
return -1;
|
if (err == -1)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
int setblocking(bool blocking) override {
|
int setblocking(bool blocking) override {
|
||||||
@ -550,6 +550,9 @@ class LWIPRawImpl : public Socket {
|
|||||||
bool rx_closed_ = false;
|
bool rx_closed_ = false;
|
||||||
pbuf *rx_buf_ = nullptr;
|
pbuf *rx_buf_ = nullptr;
|
||||||
size_t rx_buf_offset_ = 0;
|
size_t rx_buf_offset_ = 0;
|
||||||
|
// don't use lwip nodelay flag, it sometimes causes reconnect
|
||||||
|
// instead use it for determining whether to call lwip_output
|
||||||
|
bool nodelay_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
|
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
|
||||||
|
Loading…
Reference in New Issue
Block a user