Skip to content
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

Examples for the AsyncTCP #165

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

martinmmi
Copy link

Hello, iam martin from germany and i like to develop a tcp interface for an esp32 application.

How can i start with your AsyncTCP lib 1.2.1 or the new fork for esphome AsyncTCP lib 2.0.0?

I have this tcp sequences from a tcp server:

OFF:
80 30 41 42 43 44 45 46 47 20 20 20 20 20 20 20 20 20

GREEN:
80 31 41 42 43 44 45 46 47 20 20 20 20 20 20 20 20 20

RED:
80 33 41 42 43 44 45 46 47 20 20 20 20 20 20 20 20 20

How can I evaluate this sequences in a tcp client? Are some examples availble?

Many thanks
Martin

OttoWinter and others added 6 commits October 14, 2019 12:46
The AsyncClient::send() methods sets a boolean to true after pushing
data over the TCP socket successfully using tcp_output(). It also sets
a timestamp to remember at what time the data was sent.

The AsyncClient::_sent() callback method reacts to ACKs coming from
the connected client. This method sets the boolean to false.

In the AsyncClient::_poll() method, a check is done to see if the
boolean is true ("I'm waiting for an ACK") and if the time at which
the data was sent is too long ago (5000 ms). If this is the case,
a connection issue with the connected client is assumed and the
connection is forcibly closed by the server.

The race condition is when these operations get mixed up, because
of multithreading behavior. The _sent() method can be called during
the execution of the send() method:

1. send() sends out data using tcp_output()
2. _sent() is called because an ACK is processed, sets boolean to false
3. send() continues and sets boolean to true + timestamp to "now"

After this, the data exchange with the client was successful. Data were
sent and the ACK was seen.
However, the boolean ended up as true, making the _poll() method think
that an ACK is still to be expected. As a result, 5000 ms later, the
connection is dropped.

This commit fixes the code by first registering that an ACK is
expected, before calling tcp_output(). This way, there is no race
condition when the ACK is processed right after that call.

Additionally, I changed the boolean to an integer counter value.
The server might send multiple messages to the client, resulting in
multiple expected ACKs. A boolean does not cover this situation.

Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants