1d4afb5ceSopenharmony_ci# lws minimal ws server raw adopt udp
2d4afb5ceSopenharmony_ci
3d4afb5ceSopenharmony_ciThis example demonstrates echoing packets on a UDP socket in lws.
4d4afb5ceSopenharmony_ci
5d4afb5ceSopenharmony_ciA "foreign" UDP socket is created, bound (so it can "listen"), and
6d4afb5ceSopenharmony_ciadopted into lws event loop.  It acts like a tcp RAW mode connection in
7d4afb5ceSopenharmony_cilws and uses the same callbacks.
8d4afb5ceSopenharmony_ci
9d4afb5ceSopenharmony_ciWriting is a bit different for UDP.  By default, the system has no
10d4afb5ceSopenharmony_ciidea about the receiver state and so asking for a callback_on_writable()
11d4afb5ceSopenharmony_cialways believes that the socket is writeable... the callback will
12d4afb5ceSopenharmony_cihappen next time around the event loop if there are no pending partials.
13d4afb5ceSopenharmony_ci
14d4afb5ceSopenharmony_ciWith UDP, there is no "connection".  You need to write with sendto() and
15d4afb5ceSopenharmony_cidirect the packets to a specific destination.  You can learn the source
16d4afb5ceSopenharmony_ciof the last packet that arrived at the LWS_CALLBACK_RAW_RX callback by
17d4afb5ceSopenharmony_cigetting a `struct lws_udp *` from `lws_get_udp(wsi)`.  To be able to
18d4afb5ceSopenharmony_cisend back to that guy, you should take a copy of the `struct lws_udp *` and
19d4afb5ceSopenharmony_ciuse the .sa and .salen members in your sendto().
20d4afb5ceSopenharmony_ci
21d4afb5ceSopenharmony_ciHowever the kernel may not accept to buffer / write everything you wanted to send.
22d4afb5ceSopenharmony_ciSo you are responsible to watch the result of sendto() and resend the
23d4afb5ceSopenharmony_ciunsent part next time.
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci## build
26d4afb5ceSopenharmony_ci
27d4afb5ceSopenharmony_ci```
28d4afb5ceSopenharmony_ci $ cmake . && make
29d4afb5ceSopenharmony_ci```
30d4afb5ceSopenharmony_ci
31d4afb5ceSopenharmony_ci## usage
32d4afb5ceSopenharmony_ci
33d4afb5ceSopenharmony_ci```
34d4afb5ceSopenharmony_ci $ ./lws-minimal-raw-adopt-udp
35d4afb5ceSopenharmony_ci$ ./lws-minimal-raw-adopt-udp 
36d4afb5ceSopenharmony_ci[2018/03/24 08:12:37:8869] USER: LWS minimal raw adopt udp | nc -u 127.0.0.1 7681
37d4afb5ceSopenharmony_ci[2018/03/24 08:12:37:8870] NOTICE: Creating Vhost 'default' (no listener), 1 protocols, IPv6 off
38d4afb5ceSopenharmony_ci[2018/03/24 08:12:37:8878] USER: LWS_CALLBACK_RAW_ADOPT
39d4afb5ceSopenharmony_ci[2018/03/24 08:12:41:5656] USER: LWS_CALLBACK_RAW_RX (6)
40d4afb5ceSopenharmony_ci[2018/03/24 08:12:41:5656] NOTICE: 
41d4afb5ceSopenharmony_ci[2018/03/24 08:12:41:5656] NOTICE: 0000: 68 65 6C 6C 6F 0A                                  hello.          
42d4afb5ceSopenharmony_ci[2018/03/24 08:12:41:5656] NOTICE: 
43d4afb5ceSopenharmony_ci```
44d4afb5ceSopenharmony_ci
45d4afb5ceSopenharmony_ci```
46d4afb5ceSopenharmony_ci $ nc -u 127.0.0.1 7681
47d4afb5ceSopenharmony_cihello
48d4afb5ceSopenharmony_cihello
49d4afb5ceSopenharmony_ci```
50