OpenWrt ubus without blocking (4): use ustream for sockets, not a hand-written write state machine
The official examples/client.c hands a received fd to ustream_fd and parses lines in notify_read. ustream handles nonblocking I/O, edge-triggered reads, buffering and partial writes. Includes a complete TCP echo server and explains why you do not split every socket write into a state machine.