reliableUDP

package module
v0.0.0-...-091cd13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2023 License: MIT Imports: 7 Imported by: 6

README

reliableUDP

a simple implementation of reliable udp

example

package main

import (
	"fmt"
	"log"
	"net"
	"time"

	"github.com/Doraemonkeys/reliableUDP"
)

func main() {
	udpconn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 12346})
	if err != nil {
		log.Fatal(err)
	}
	rudp := reliableUDP.New(udpconn)
	defer rudp.Close()
	go func() {
		for {
			d, addr, err := rudp.ReceiveAll(0)
			if err != nil {
				log.Println(err)
			}
			fmt.Println("receive", string(d), addr.String())
		}
	}()
	i := 0
	rudp.SetGlobalReceive()
	for {
		msg := fmt.Sprintf("hello %d", i)
		err := rudp.Send(&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345}, []byte(msg), 0)
		if err != nil {
			log.Println(err)
		}
		time.Sleep(time.Second * 2)
		i++
		if i > 5 {
			break
		}
	}
}
package main

import (
	"fmt"
	"log"
	"net"
	"time"

	"github.com/Doraemonkeys/reliableUDP"
)

func main() {
	udpconn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 12345})
	if err != nil {
		log.Fatal(err)
	}
	rudp := reliableUDP.New(udpconn)
	defer rudp.Close()
	ch := make(chan *net.UDPAddr)
	go func() {
		raddr := <-ch
		i := 0
		for {
			msg := fmt.Sprintf("hello %d", i)
			err := rudp.Send(raddr, []byte(msg), 0)
			if err != nil {
				log.Println(err)
			}
			time.Sleep(1 * time.Second)
			i++
		}
	}()
	i := 0
	rudp.SetGlobalReceive()
	for {
		d, addr, err := rudp.ReceiveAll(0)
		if err != nil {
			log.Println(err)
		}
		fmt.Println("receive", string(d), addr.String())
		if i == 0 {
			go func() {
				ch <- addr
			}()
		}
		i++
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReliableUDP

type ReliableUDP struct {
	// contains filtered or unexported fields
}

func New

func New(conn *net.UDPConn) *ReliableUDP

应该确保conn是可用的,且之后不能再使用conn

func (*ReliableUDP) CancelGlobalReceive

func (r *ReliableUDP) CancelGlobalReceive()

取消全局接收

func (*ReliableUDP) Close

func (r *ReliableUDP) Close()

关闭连接,关闭前请确保没有调用中的Receive函数和Send函数

func (*ReliableUDP) LocalAddr

func (r *ReliableUDP) LocalAddr() net.Addr

func (*ReliableUDP) Receive

func (r *ReliableUDP) Receive(addr *net.UDPAddr, timeout time.Duration) ([]byte, error)

最多接收1024-8字节

func (*ReliableUDP) ReceiveAll

func (r *ReliableUDP) ReceiveAll(timeout time.Duration) ([]byte, *net.UDPAddr, error)

func (*ReliableUDP) Send

func (r *ReliableUDP) Send(addr *net.UDPAddr, data []byte, timeout time.Duration) error

最多发送1024-8字节,并发安全。 发送超时时间为timeout,如果timeout为0则默认为4秒

func (*ReliableUDP) SendUnreliable

func (r *ReliableUDP) SendUnreliable(data []byte, addr *net.UDPAddr) error

不可靠的udp发送,并发安全

func (*ReliableUDP) SetGlobalReceive

func (r *ReliableUDP) SetGlobalReceive()

设置全局接收,如果设置了全局接收,那么Receive函数将不再接收数据包,而是将数据包发送到全局接收通道

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL