Network programming and coroutines in C++
In Chapter 7, you learned about the TCP and UDP communication protocols. We discussed their usefulness in network data transfer, but coroutines make them even more powerful. As mentioned, the code will behave in an asynchronous way, but we have control over the scheduling. Coroutines will be more efficient in context switching as they are executed on the user level. We’ll continue with the Generator
definition to match the type of the coroutine, as discussed earlier. Traditionally, that object is made move-only – this allows us to restrict the usage of the coroutine wrapper, but in general cases, coroutine objects are non-copyable and non-moveable, because the coroutine frame is a part of them, and some local variables can be references or pointers to other local variables. Thus, let’s extend the structure accordingly:
Important note
This, again, is a very generic pattern that is used in almost every coroutine example...