1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/minimal-examples/raw/minimal-raw-adopt-udp
Andy Green f7149e90c2 cc0: align dedication to CC0 FAQ recommended format
https://libwebsockets.org/pipermail/libwebsockets/2019-April/007937.html

thanks to Bruce Perens for noting it.

This doesn't change the intention or status of the CC0 files, they were
pure CC0 before (ie, public domain) and they are pure CC0 now.  It just
gets rid of the (C) part at the top of the dedication which may be read
to be a bit contradictory since the purpose is to make it public domain.
2019-05-02 09:29:01 +01:00
..
CMakeLists.txt minimal: raw udp 2018-04-06 10:38:03 +08:00
minimal-raw-adopt-udp.c cc0: align dedication to CC0 FAQ recommended format 2019-05-02 09:29:01 +01:00
README.md minimal: raw udp 2018-04-06 10:38:03 +08:00

lws minimal ws server raw adopt udp

This example demonstrates echoing packets on a UDP socket in lws.

A "foreign" UDP socket is created, bound (so it can "listen"), and adopted into lws event loop. It acts like a tcp RAW mode connection in lws and uses the same callbacks.

Writing is a bit different for UDP. By default, the system has no idea about the receiver state and so asking for a callback_on_writable() always believes that the socket is writeable... the callback will happen next time around the event loop if there are no pending partials.

With UDP, there is no "connection". You need to write with sendto() and direct the packets to a specific destination. You can learn the source of the last packet that arrived at the LWS_CALLBACK_RAW_RX callback by getting a struct lws_udp * from lws_get_udp(wsi). To be able to send back to that guy, you should take a copy of the struct lws_udp * and use the .sa and .salen members in your sendto().

However the kernel may not accept to buffer / write everything you wanted to send. So you are responsible to watch the result of sendto() and resend the unsent part next time.

build

 $ cmake . && make

usage

 $ ./lws-minimal-raw-adopt-udp
$ ./lws-minimal-raw-adopt-udp 
[2018/03/24 08:12:37:8869] USER: LWS minimal raw adopt udp | nc -u 127.0.0.1 7681
[2018/03/24 08:12:37:8870] NOTICE: Creating Vhost 'default' (no listener), 1 protocols, IPv6 off
[2018/03/24 08:12:37:8878] USER: LWS_CALLBACK_RAW_ADOPT
[2018/03/24 08:12:41:5656] USER: LWS_CALLBACK_RAW_RX (6)
[2018/03/24 08:12:41:5656] NOTICE: 
[2018/03/24 08:12:41:5656] NOTICE: 0000: 68 65 6C 6C 6F 0A                                  hello.          
[2018/03/24 08:12:41:5656] NOTICE: 
 $ nc -u 127.0.0.1 7681
hello
hello