0
votes

I have a coroutine that listens asynchronously on an asio UDP socket. When it receives a message it co_spawns a new co-routine to handle the message and then goes back to listening on the port. This new coroutine may need to do additional communication on the same UDP socket. What is a good way to make sure the replies to the requests that the second coroutine makes comes back to it?

I was thinking of making some kind of future to store the reply that the new coroutine can co_await but it doesn't seem to be avaible in asio and it doesn't look to be easy to make. I could store a function object that get's called when a reply comes but then what's the point of coroutines in the first place? I could have the new coroutine listen to the same socket but which coroutine will then get the reply? Either? Both?

To summarize: I want to be able to suspend the new coroutine until I get a reply and when I do get a reply in the original coroutine I want it to resume the new coroutine. Basically I want something like cppcoro::single_consumer_event

1
it just sounds like you want to handle the received message synchronously rather than to spawn a child-coroutine to handle it - David Haim
I want to be able to handle requests concurrently so I do want spawn another coroutine. I guess it is possible to do without spawning a new coroutine but it justs seems really messy... - christianO

1 Answers

0
votes

I figured out a decent solution but I would love to find a better one. I can create a future class that contains an asio::steady_timer. Then I can handle timeouts in a neat way and I can just call cancel the timer when the original coroutine gets a reply. Seems pretty overkill and steady_timer is huge (112 bytes?!) but if it works then it works...