libwebsockets
Lightweight C library for HTML5 websockets
|
UDP is supported in lws... the quickest way is to use the api lws_create_adopt_udp()
which returns a wsi bound to the provided vhost, protocol, lws_retry
struct, dns address and port.
The wsi can be treated normally and lws_write()
used to write on it.
Retries are important in udp but there's no standardized ack method unlike tcp. Lws allows you to bind an lws_retry
struct describing the policy to the udp wsi, but since one UDP socket may have many transactions in flight, the lws_sul
and uint16_t
to count the retries must live in the user's transaction object like this
in the LWS_CALLBACK_RAW_WRITEABLE
callback, before doing the write, set up the retry like this
This manages the retry counter in the transaction object, guards against it wrapping, selects the timeout using the policy bound to the wsi, and sets the lws_sul
in the transaction object to call the given callback if the sul time expires.
In the callback, it should simply call lws_callback_on_writable()
for the udp wsi.
You can simulate udp packetloss at tx and rx by using the Fault Injection apis with the well-known fault names "udp_tx_loss" and "udp_rx_loss", typically with the probabilistic setting, in commandline format something like --fault-injection "wsi/udp_tx_loss(10%)"