Browse Source

update readme

master
zjt 1 year ago
parent
commit
6f3f0ec9c5
4 changed files with 106 additions and 3 deletions
  1. 1
    0
      .gitignore
  2. 102
    2
      README.md
  3. 2
    0
      conf.go
  4. 1
    1
      rudp.go

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.swp

+ 102
- 2
README.md View File

@@ -1,7 +1,107 @@
1 1
 # rudp
2
-rudp 是在UDP上实现的可靠传输
2
+rudp采用请求回应机制,实现了UDP的可靠传输,即接收方检查是否丢失数据,然后向发送方请求丢失的数据,因此发送方必须保留已经发送过的数据一定时间来回应数据丢失。为了减小发送方数据保留量,在每收到n个包时通知发送方n之前的包已经收到可以清除了,另外超过设定的包超时时间后也会清除。
3
+
4
+# 使用
5
+1 创建rudp对象
6
+
7
+```
8
+rudp := rudp.New()
9
+```
10
+
11
+2 发送消息,n 发送的的消息长度,err 是否出错
12
+
13
+```
14
+n ,err := rudp.Send(bts []byte)
15
+```
16
+
17
+3 接受消息,n 返回接受到的的消息长度,err 是否出错
18
+
19
+```
20
+n , err := rudp.Recv(data []byte)
21
+```
22
+
23
+4 更新时间获取要发送的消息,如果设置的sendDelay大于更新tick,update返回nil,下次调用时间到时会返回所有的消息链表
24
+
25
+```
26
+var package *Package = rudp.Update(tick int)
27
+```
28
+5 相关设置
29
+
30
+```
31
+rudp.SetCorruptTick(n int)    //设置超过n个tick连接丢失
32
+rudp.SetExpiredTick(n int)    //设置发送的消息最大保留n个tick
33
+rudp.SetSendDelayTick(n int)  //设置n个tick发送一次消息包
34
+rudp.SetMissingTime(n int)    //设置n纳秒没有收到消息包就认为消息丢失,请求重发
35
+```
36
+
37
+#兼容tcp
38
+另外rudp也实现了tcp的相关接口,很容易改造现有的tcp项目为rudp
39
+###服务端
40
+1 监听udp端口
41
+
42
+```
43
+	addr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 9981}
44
+	conn, err := net.ListenUDP("udp", addr)
45
+	if err != nil {
46
+		fmt.Println(err)
47
+		return
48
+	}
49
+```
50
+2 接受连接
51
+
52
+```
53
+	listener := rudp.NewListener(conn)
54
+	rconn, err := listener.AcceptRudp()
55
+	if err != nil {
56
+		fmt.Printf("accept err %v\n", err)
57
+		return
58
+	}
59
+```
60
+3 读取消息
61
+
62
+```
63
+	data := make([]byte, rudp.MAX_PACKAGE)
64
+	n, err := rconn.Read(data)
65
+	if err != nil {
66
+		fmt.Printf("read err %s\n", err)
67
+		return
68
+	}
69
+```
70
+4 发送消息
71
+
72
+```
73
+	n , err := rconn.Write([]byte("hello rudp"))
74
+```
75
+
76
+###客户端
77
+1 拨号
78
+
79
+```
80
+    raddr := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9981}
81
+    //raddr := net.UDPAddr{IP: net.ParseIP("47.89.180.105"), Port: 9981}
82
+    laddr := net.UDPAddr{IP: net.IPv4zero, Port: 0}
83
+    conn, err := net.DialUDP("udp", &laddr, &raddr)
84
+    if err != nil {
85
+    	fmt.Println(err)
86
+    	return
87
+    }
88
+```
89
+2 创建conn
90
+
91
+```
92
+	rconn := rudp.NewConn(conn, rudp.New())
93
+```
94
+3 发送消息,同服务端
95
+4 接受消息,同服务端
96
+
97
+###相关设置
98
+
99
+```
100
+rudp.SetAtuoSend(bool) 设置rudp是否自动发送消息
101
+rudp.SetSendTick() 设置发送的间隔(为0时自动发送消息不启用)
102
+rudp.SetMaxSendNumPerTick() 设置每个tick可以最大发送的消息数量
103
+``` 
3 104
 
4
-rudp is a reliable transmission on UDP
5 105
 # Links
6 106
 1. https://github.com/cloudwu/rudp --rudp in c
7 107
 2. https://blog.codingnow.com/2016/03/reliable_udp.html --blog of rudp

+ 2
- 0
conf.go View File

@@ -6,10 +6,12 @@ import "time"
6 6
 var corruptTick int = 5
7 7
 var expiredTick int = 1e2 * 60 * 5 //5 minute on sendTick 1e7
8 8
 var sendDelayTick int = 1
9
+var missingTime int = 1e7
9 10
 
10 11
 func SetCorruptTick(tick int)   { corruptTick = tick }
11 12
 func SetExpiredTick(tick int)   { expiredTick = tick }
12 13
 func SetSendDelayTick(tick int) { sendDelayTick = tick }
14
+func SetMissingTime(miss int)   { missingTime = miss }
13 15
 
14 16
 //rudp conn
15 17
 var debug bool = false

+ 1
- 1
rudp.go View File

@@ -306,7 +306,7 @@ func (this *Rudp) checkMissing(direct bool) {
306 306
 		if !direct && last == 0 {
307 307
 			this.recvSkip[this.recvIDMin] = nano
308 308
 			dbg("miss start %v-%v,max %v", this.recvIDMin, head.id-1, this.recvIDMax)
309
-		} else if direct || last+1e8 < nano {
309
+		} else if direct || last+missingTime < nano {
310 310
 			delete(this.recvSkip, this.recvIDMin)
311 311
 			this.reqSendAgain <- [2]int{this.recvIDMin, head.id - 1}
312 312
 			dbg("req miss %v-%v,direct %v,wait num %v",

Loading…
Cancel
Save