This project implements an advanced UDP (User Datagram Protocol) framework in Go, featuring automated packet reassembly, customizable retry and timeout mechanisms, and packet prioritization with Quality of Service (QoS) support.
- Automated Packet Reassembly: Ensures data integrity and order in a connectionless protocol.
- Customizable Retry and Timeout Mechanisms: Improves reliability in high packet loss environments.
- Packet Prioritization and QoS: Allows critical data to be transmitted first.
To install the UDP framework, use the following command:
go get github.com/mrinalxdev/udpframework
Here's a basic example of how to use the UDP framework:
package main
import (
"log"
"net"
"github.com/mrinalxdev/udpframework"
)
func main() {
retryConfig := udpframework.RetryConfig{
MaxRetries: 3,
BaseTimeout: time.Second,
BackoffRate: 1.5,
}
qosConfig := udpframework.QoSConfig{
PriorityLevels: 3,
PriorityQueues: make([][]udpframework.Packet, 3),
}
framework, err := udpframework.UdGo(":8080", retryConfig, qosConfig)
if err != nil {
log.Fatal(err)
}
defer framework.Close()
// Send a packet
packet := udpframework.Packet{
SequenceNumber: 1,
Priority: 2,
Data: []byte("Hello, UDP!"),
}
destAddr, _ := net.ResolveUDPAddr("udp", "localhost:9090")
err = framework.SendPacket(packet, destAddr)
if err != nil {
log.Printf("Error sending packet: %v", err)
}
// Receive packets
for {
data, addr, err := framework.ReceivePacket()
if err != nil {
log.Printf("Error receiving packet: %v", err)
continue
}
log.Printf("Received from %v: %s", addr, string(data))
}
}
type UDPFramework struct {
// Unexported fields
}
The main struct for the UDP framework.
type Packet struct {
SequenceNumber uint32
Priority int
Data []byte
RetryCount int
}
Represents a UDP packet with additional metadata.
type RetryConfig struct {
MaxRetries int
BaseTimeout time.Duration
BackoffRate float64
}
Holds the configuration for retry and timeout mechanisms.
type QoSConfig struct {
PriorityLevels int
PriorityQueues [][]Packet
}
Holds the configuration for Quality of Service.
func UdGo(addr string, retryConfig RetryConfig, qosConfig QoSConfig) (*UDPFramework, error)
Creates a new instance of UDPFramework.
func (uf *UDPFramework) SendPacket(packet Packet, destAddr *net.UDPAddr) error
Sends a packet with automatic retries and prioritization.
func (uf *UDPFramework) ReceivePacket() ([]byte, *net.UDPAddr, error)
Receives and reassembles packets.
func (uf *UDPFramework) Close() error
Closes the UDP connection.
MaxRetries
: Maximum number of retry attempts.BaseTimeout
: Initial timeout duration.BackoffRate
: Rate at which the timeout increases after each retry.
PriorityLevels
: Number of priority levels.PriorityQueues
: Slice of queues for each priority level.
packet := udpframework.Packet{
SequenceNumber: 1,
Priority: 2, // High priority
Data: []byte("Important message"),
}
destAddr, _ := net.ResolveUDPAddr("udp", "localhost:9090")
err := framework.SendPacket(packet, destAddr)
for {
data, addr, err := framework.ReceivePacket()
if err != nil {
log.Printf("Error receiving packet: %v", err)
continue
}
log.Printf("Reassembled message from %v: %s", addr, string(data))
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.