Giter VIP home page Giter VIP logo

rudp's Introduction

Build Status Coverage Status

rudp

rudp采用请求回应机制,实现了UDP的可靠传输,即接收方检查是否丢失数据,然后向发送方请求丢失的数据,因此发送方必须保留已经发送过的数据一定时间来回应数据丢失。为了减小发送方数据保留量,在每收到n个包时通知发送方n之前的包已经收到可以清除了,另外超过设定的包超时时间后也会清除。

使用

1 创建rudp对象

rudp := rudp.New()

2 发送消息,n 发送的的消息长度,err 是否出错

n ,err := rudp.Send(bts []byte)

3 接受消息,n 返回接受到的的消息长度,err 是否出错

n , err := rudp.Recv(data []byte)

4 更新时间获取要发送的消息,如果设置的sendDelay大于更新tick,update返回nil,下次调用时间到时会返回所有的消息链表

var package *Package = rudp.Update(tick int)

5 相关设置

rudp.SetCorruptTick(n int)    //设置超过n个tick连接丢失
rudp.SetExpiredTick(n int)    //设置发送的消息最大保留n个tick
rudp.SetSendDelayTick(n int)  //设置n个tick发送一次消息包
rudp.SetMissingTime(n int)    //设置n纳秒没有收到消息包就认为消息丢失,请求重发

兼容tcp

另外rudp也实现了tcp的相关接口,很容易改造现有的tcp项目为rudp

服务端

1 监听udp端口

addr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 9981}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
	fmt.Println(err)
	return
}

2 接受连接

listener := rudp.NewListener(conn)
rconn, err := listener.AcceptRudp()
if err != nil {
	fmt.Printf("accept err %v\n", err)
	return
}

3 读取消息

data := make([]byte, rudp.MAX_PACKAGE)
n, err := rconn.Read(data)
if err != nil {
	fmt.Printf("read err %s\n", err)
	return
}

4 发送消息

n , err := rconn.Write([]byte("hello rudp"))

客户端

1 拨号

raddr := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9981}
//raddr := net.UDPAddr{IP: net.ParseIP("47.89.180.105"), Port: 9981}
laddr := net.UDPAddr{IP: net.IPv4zero, Port: 0}
conn, err := net.DialUDP("udp", &laddr, &raddr)
if err != nil {
	fmt.Println(err)
	return
}

2 创建conn

rconn := rudp.NewConn(conn, rudp.New())

3 发送消息,同服务端 4 接受消息,同服务端

相关设置

rudp.SetAtuoSend(bool) 设置rudp是否自动发送消息
rudp.SetSendTick() 设置发送的间隔(为0时自动发送消息不启用)
rudp.SetMaxSendNumPerTick() 设置每个tick可以最大发送的消息数量

Links

  1. https://github.com/cloudwu/rudp --rudp in c
  2. https://blog.codingnow.com/2016/03/reliable_udp.html --blog of rudp

rudp's People

Contributors

u35s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rudp's Issues

Race condition with read/write to 'corrupt' field

I have forked this repository, as I can see the original author has not made any commits for a year to here: https://git.parallelcoin.io/dev/rudp - just want to remark firstly that it is mostly very nice readable code.

The corrupt field is written to in multiple goroutines so in my fork I have changed the value to use go.uber.org/atomic int32 to store the error value and now the tests run without flagging a race condition. I have also taken the liberty of correcting the naming of method receivers to Go idiom.

This commit: https://git.parallelcoin.io/dev/rudp/commit/efb6fb9d1faeedab0dd550ba9f977100b1b8222d has all of these changes directly from your code and will only differ in the changes I made to redirect the imports of the example code.

Thank you for writing this, I was searching for a Go implementation of RUDP and this saves me writing a simpler, less powerful but (maybe) faster version, and because it can drop in where TCP connections are used, it can be used with HTTP for RPC (or any web service), while allowing potentially easy connectivity to any other language via its RUDP implementation. This is a more mature, simpler protocol than QUIC, with a lot of code existing already that knows how to use it.

There are something wrongs when no sendQueue message

func (r *Rudp) sendMessage(tmp *packageBuffer) {
	m := r.sendQueue.head
	for m != nil {
		tmp.packMessage(m)
		m = m.next
	}

        // ***  this is not correct when some situation
	if r.sendQueue.head != nil {
		if r.sendHistory.tail == nil {
			r.sendHistory = r.sendQueue
		} else {
			r.sendHistory.tail.next = r.sendQueue.head
			r.sendHistory.tail = r.sendQueue.tail
		}
		r.sendQueue.head = nil
		r.sendQueue.tail = nil
	}
}

if sendQueue is empty , and the program will never send sendHistory's message
or sendHistory's message will be sent when sendQueue contain message.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.