-
Notifications
You must be signed in to change notification settings - Fork 13.3k
TCP-server keepalive #3886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@d-a-v what do you think of this? At first glance, it looks sound to me. |
My only comment would be: what happens when all three are specified as 0? Also, maybe split the keepalive(),ethod into 2: one to set the values, and also enable keepalive, and a different method, or maybe an overload, to disable keepalive. |
|
I personaly don't use this so I don't know. But it surely can be added so it can be tested, in the form of WiFiClient::keepAlive(idle,interval_count) that would manipulate the pcb by setting it inside ClientContext.h so that it can be enabled for any started tcp client (the ones by hand, or the ones got from a tcp server). |
Keepalives, once enabled, should be on for every TCP connection regardless of what traffic is being sent over that socket. Therefore I'd argue against the suggested implementation, it should be done lower down the stack. Keepalives operate below the TCP layer and do not interfere with the message flow in TCP so the only reason not to use keepalive is to reduce data transmissions. According to this good reference "Keepalive is non-invasive, and in most cases, if you're in doubt, you can turn it on without the risk of doing something wrong." I added more to this response in the last two paragraphs of this comment. |
@kentaylor I'm not sure lwIP API propose automatic keepalive for every new tcp connection. It needs to be checked but anyway It can be done by hand in ClientContext.h since it is the esp8266/arduino entry point for every tcp connection.
|
My reading of the code is that keepalive is turned on globally by this #define rather than via an API. Once turned on it will be used on every TCP connection. Is that wrong? It is the behaviour I would have expected. |
Hello. The keepalive packets were not sent before I wrote the functions from my first message. I watched this with tcpdump by connecting to the raspberry pi. I use this so that the node knows that it has disconnected from the server. |
Thanks @EnricoElCartmanez, my reading of the code was wrong. There is also a test for a socket option here that is set for each socket. |
Looking again at @EnricoElCartmanez initial suggestion in light of my new understanding that keepalives are not enabled by default. Can I suggest that the @EnricoElCartmanez suggestion be modified to enable keepalives by default with the default keep_idle, keep_interval and keep_count values also left as default? According to this reference "Keepalive is non-invasive, and in most cases, if you're in doubt, you can turn it on without the risk of doing something wrong." The only downside is a little extra data traffic. A TCP connection is created and destroyed in accordance with this state diagram Sometimes though, packet loss means the Close - Responder Sequence in the bottom right of the diagram is not followed. This is normally rare but it is then that the keepalives provide the functionality that "that the node knows that it has disconnected from the server". Additionally keep alives will maintain mapping through a NAT which is required when a TCP connection is maintained for a long time and most IOT devices are behind NATs. As the cost is low and the benefit high of avoiding an occasional incorrect assumption that a TCP socket exists when it doesn't, keepalives should be enabled for all TCP connections by default. Methods should exist to turn it off but it should be on by default. I doubt the options to change from default values for default keep_idle, keep_interval and keep_count values are even very useful. |
I am a bit confused about how to use your code. For he keepalive function to work do you have to add anything like to your sketch's setup or is there no change to the affected sketch necessary? |
Hello Bill. If you need to activate keepalive on your client, you should call keepalive() function in your sketch before calling connect() or do not call if you not need keepalive. Like this: WiFiClient client;
void setup (void) {
client.keepalive(true, 10000, 10000, 2);
client.connect(ip, port);
}
void loop (void) {
...
} I'm not a professional programmer. I just needed this function and it worked. I will be pleased if someone is useful. |
@d-a-v is there a RAM difference when keepalive is enabled in lwip? I expect something like 12 bytes per pcb or along those lines. |
Since keepalive code is already enabled by #define, there is no impact using it on lwip side (neither ram or flash). |
Already implemented in #3937 . |
Hardware
Hardware: ESP-12
Core Version: 2.4.0-rc2
Hello. It may be useful for someone to be able to enable keepalive packets for a TCP server. For example, like this:
In file WifiClient.cpp:
In file WifiClient.h:
Can it be worth adding this feature to the project? Thank you. Sorry for my english.
The text was updated successfully, but these errors were encountered: