RUDP Written In C# For Diarkis
By: Diarkis Inc.
Tags: Rudp, Technology, Real Time Network, C Sharp Programming

Before We Start

RUDP stands for Reliable UDP (User Datagram Protocol) is used to transport UDP messages. UDP by design is an unreliable network protocol. It means that your messages may get “lost”.

RUDP adds an extra feature to UDP so that your messages will not get “lost” and reliably delivered to the intended destination. For more detailed information on RUDP, please read some technical articles on RUDP as we will be focusing on our implementation of RUDP for Diarks.

Diarkis is our real-time network engine and it implements its own version of RUDP along with standard network protocols such as UDP, TCP, and HTTP.

RUDP Implemented For Diarkis

Our version of RUDP implements the following: 1. Reliable transportation by having a retry mechanism. 2. Guaranteeing of packet order. 3. Automatic packet splitting and reconstruction on reception for large size packets.

Diarkis’ RUDP also allows you to mix with plain UDP. You may choose to send a message as either UDP or RUDP. Each message packet has a header that indicates the mode of transport (UDP or RUDP).

The header flag has the following:

1. Plain UDP message

2. RUDP initialization packet

3. RUDP data packet

4. RUDP acknowledge packet

5. RUDP retry data packet

6. RUDP retry acknowledge packet

Reliable Transportation With a Retry Mechanism

The sender of packets expects an acknowledgement packet back from the recipient for every packet received. Sent packets without acknowledgement packets from the recipient will be re-sent to the recipient after a set time. This set time will increase as you retry more in order to prevent “spamming” the recipient. This means retries will be slower as it retries more.

When multiple retries for the same packet fail, the sender considers the recipient to be “gone” and terminates the communication. Guaranteeing of Packet Order

UDP itself does not maintain the order of packet sent and received. Our RUDP adds a feature to maintain the order of packets being sent and received. This is behaviour very similar to TCP. If the previous packet is lost and resent, the next packet will not be processed until the first packet is received and handled. Automatic Packet Splitting and Reconstruction on Reception

When sending a large packet (over 1,400bytes), our RUDP splits the packet into multiple separate packets and send them. The recipient then reconstructs the split-packets into the original packet. This is useful in many circumstances. First, most cloud service(GCP, AWS, Azure etc.) limits the size of a UDP packet to less then 1,400bytes (Not exactly 1,400bytes though). Packets that exceed the limit size are forcefully dropped. Many commercial routers also split large size packets and this leads to a higher chance of packet loss because if one split packet is lost, the rest will be lost also.

Technical Details on Implementation

Our RUDP spawns 2 threads. One thread is responsible for sending packets, and other is responsible for receiving the packets. The main thread will send the packet to be sent into the send buffer for the sender thread to pick up and send. The recipient buffer will be stacking the received packets to be popped in the order of the packets sent. Reordering of packets and reconstruction of split packets happen in the recipient thread, but the actual processing of the received packets happen only in the main thread. Likewise, the main thread never actually sends packets, but only send them to the buffer for the sender thread.

Why Two Extra Threads?

When you are waiting for packets to come in, you must block and wait. If we wait in the main thread, your entire application halts. We do not want that to happen, so we have a dedicated thread just to wait for incoming packets and processing them. The reason for having another thread for sending packets is very similar to having the recipient thread. Because the recipient thread blocks and waits, we cannot have the sending mechanism in the same thread. Otherwise, we will have to wait until we have some new packets coming in to send our own packets. Sending packets is a blocking operation, so we do not want to have that in the main thread either and that leads to us having a dedicated thread for sending packets.

Why Not Have Previous Packets Combined With Your Current Packet To Avoid Having Retries?

We avoided this method of implementation because we wanted to be able to handle large size packets also. If we were to combine previously sent packets to each sent packet, the size of the packet will grow, but we may have the size limitation from our cloud service and such. What will happen if we tried to send a large packet with this system? we will likely be combining the split packets together, but these “combined” split packets may be lost. If we do not have the retry mechanism like our Diarkis, those lost split packets will be forever lost making this RUDP not so reliable.

RUDP Packet Diagram

Writing Packet Reception Using Socket.Poll in C#

C# Socket class offers Poll method that blocks and waits for incoming UDP messages. We will be using this method instead of writing a loop with Thread.Sleep for better performance (Less CPU consumption while “waiting”). If you are using UdpClient class instead Socket class directly, you can access Poll method like this: udpClientInstance.Client.Poll

private void _PollRcv(ref EndPoint endpoint)

{

while (_connected && _udpClient != null &&

_cli.Client.Poll(RCV_POLL_TIME, SelectMode.SelectRead))

{

_HandleRcv(ref endpoint);

}

} private void _HandleRcv(ref EndPoint endpoint)

{

while (_connected && _udpClient != null &&

_udpClient.Available > 0)

{

// this is a blocking operation

int readSize = _udpClient.Client.ReceiveFrom(

_rcvBytes, ref endpoint);

// _rcvBytes is now fill with read UDP message bytes

}

}

Conclusion

Although the definition of RUDP is well defined and standardized, the implementation of RUDP is not. Depends on your own needs you may choose to implement RUDP in a certain way. You may even omit certain features such as packet order management if you do not need it. It is important to design how you want to implement RUDP based on your application’s requirements and what you need to achieve.

Contact us here.