Building a tokio udp async stack

I am building a protocol (trying to emulate something, so I have to stick to the specs) that is UDP based but on top of UDP, they somehow emulate something like TCP with sequence numbers and the like.

For now everything is tokio and async await based (although I am open to suggestions here). I am trying to find an idiomatic pattern to have the listening loop working in the "background" (actually multiple listening loops on top of each other regarding the layers of the protocol).

The first level that I came up with is this: UDPConnection.rs · GitHub

Is this idiomatic? Is there any better way? I don't like how it forces me to lock the connection and timeout every now and then so others can have the chance to lock it and see if there is any packet available.

Don't try to share stuff between tasks. Spawn some actors and throw some channels at it. You can find an example here.

Locks are not your friend. If you don't try to share things that need to be mutated, your life becomes much easier.

1 Like

That is a much better implementation without locks. I was struggling with them. Thanks a lot!

I need to rework not only this layer but another one on top. Refactor inc :slight_smile:

I am in the process of splitting this and the layer which orchestrates the UDP connection would need to have the ability to split UDPConnection layer into two as well, send and receive to keep things separate. Wow, this is becoming on one side more complicated because I need more classes, but also a lot better because there is no need for locks. But I am going to need locks because once the session is established, send and receive need to share some data.

You could use channels between the send and receive tasks too, no?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.