Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 93df773

Browse files
committed
Extend Readme.md
1 parent 17039c3 commit 93df773

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# AsyncTCP
2-
[![Build Status](https://travis-ci.org/me-no-dev/AsyncTCP.svg?branch=master)](https://travis-ci.org/me-no-dev/AsyncTCP) ![](https://github.com/me-no-dev/AsyncTCP/workflows/Async%20TCP%20CI/badge.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2f7e4d1df8b446d192cbfec6dc174d2d)](https://www.codacy.com/manual/me-no-dev/AsyncTCP?utm_source=github.com&utm_medium=referral&utm_content=me-no-dev/AsyncTCP&utm_campaign=Badge_Grade)
1+
# AsyncTCP
2+
3+
[![Build Status](https://travis-ci.org/me-no-dev/AsyncTCP.svg?branch=master)](https://travis-ci.org/me-no-dev/AsyncTCP) ![](https://github.com/me-no-dev/AsyncTCP/workflows/Async%20TCP%20CI/badge.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2f7e4d1df8b446d192cbfec6dc174d2d)](https://www.codacy.com/manual/me-no-dev/AsyncTCP?utm_source=github.com&utm_medium=referral&utm_content=me-no-dev/AsyncTCP&utm_campaign=Badge_Grade)
34

45
### Async TCP Library for ESP32 Arduino
56

@@ -10,4 +11,38 @@ This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-
1011
This library is the base for [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)
1112

1213
## AsyncClient and AsyncServer
14+
1315
The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use.
16+
17+
### AsyncServer
18+
19+
To setup a server, create an `AsyncServer` instance, specifying either a port or a port and an IP address. Then register a callback using the `onClient()` method, which will be called whenever a new TCP client connects to the server.
20+
21+
By default, when sending the response in small chunks in rapid succession, the server will buffer the chunks until an ack for a previous packet has been received. By using ` setNoDelay(true);` you can disable this buffering and send the data as soon as possible. ([see Nagle's algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm]))
22+
23+
Now you can use `begin()` to start listening for incoming connections. The server can be stopped by calling `end()`.
24+
25+
```cpp
26+
AsyncServer server(80);
27+
server.onClient(...);
28+
server.begin();
29+
...
30+
server.end();
31+
```
32+
33+
Internally, after you call `begin()`, it is first ensured that an event queue ([RTOS queue](https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/02-Queues-mutexes-and-semaphores/01-Queues)) is created and a service Task ([RTOS task](https://www.freertos.org/Documentation/01-FreeRTOS-quick-start/01-Beginners-guide/01-RTOS-fundamentals)) is created. Both the queue and task are created only once, even if there are multiple servers.
34+
35+
Then [lwip](https://savannah.nongnu.org/projects/lwip/) is configured to accept tcp connections on the specified port and invoke the `_s_accept` function when a new connection is made, which forwards to the `AsyncServer._accept()` function. The `_accept()` function creates a new `AsyncClient` and pushes it to the event queue. The service task will then pick up the event and call the `AsyncServer._accepted()` which in turn calls the callback registered with `onClient()`. Thus the callback is ultimately called in the service task context.
36+
37+
### AsyncClient
38+
39+
A client can either be created by the server when a new connection is made or by the user.
40+
41+
In both cases, the connection registers various callbacks with lwip to push the events to the event queue and thus to the service task. There are the following callbacks:
42+
43+
- **recv:** invoke the `onPacket()` callback if registered, or the `onData()` callback followed by marking the data as received.
44+
- **sent:** invoke the `onAck()` callback
45+
- **error:** de-register the callbacks from lwip and call the user's `onError` and `onDisconnect` callbacks.
46+
- **poll:** used for ack and rx timeouts and to call the user's `onPoll` callback.
47+
48+
To send data, either use `write()` to send data immediately, or a combination of `add()` and `send()` to queue data and send it all together.

0 commit comments

Comments
 (0)