You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 20, 2025. It is now read-only.
@@ -10,4 +11,38 @@ This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-
10
11
This library is the base for [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)
11
12
12
13
## AsyncClient and AsyncServer
14
+
13
15
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