forked from xtaci/kcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_linux.go
91 lines (78 loc) · 1.7 KB
/
tx_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// build linux
package kcp
import (
"net"
"sync/atomic"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
func (s *UDPSession) txLoop() {
addr, _ := net.ResolveUDPAddr("udp", s.conn.LocalAddr().String())
var conn batchConn
if addr.IP.To4() != nil {
conn = ipv4.NewPacketConn(s.conn)
} else {
conn = ipv6.NewPacketConn(s.conn)
}
for {
select {
case txqueue := <-s.chTxQueue:
if len(txqueue) > 0 {
nbytes := 0
vec := txqueue
for len(vec) > 0 {
if n, err := conn.WriteBatch(vec, 0); err == nil {
vec = vec[n:]
} else {
s.socketError.Store(err)
s.Close()
return
}
}
for k := range txqueue {
nbytes = len(txqueue[k].Buffers[0])
xmitBuf.Put(txqueue[k].Buffers[0])
}
atomic.AddUint64(&DefaultSnmp.OutPkts, uint64(len(txqueue)))
atomic.AddUint64(&DefaultSnmp.OutBytes, uint64(nbytes))
}
case <-s.die:
return
}
}
}
func (l *Listener) txLoop() {
addr, _ := net.ResolveUDPAddr("udp", l.conn.LocalAddr().String())
var conn batchConn
if addr.IP.To4() != nil {
conn = ipv4.NewPacketConn(l.conn)
} else {
conn = ipv6.NewPacketConn(l.conn)
}
for {
select {
case txqueue := <-l.chTxQueue:
if len(txqueue) > 0 {
nbytes := 0
vec := txqueue
for len(vec) > 0 {
if n, err := conn.WriteBatch(vec, 0); err == nil {
vec = vec[n:]
} else {
l.socketError.Store(err)
l.Close()
return
}
}
for k := range txqueue {
nbytes = len(txqueue[k].Buffers[0])
xmitBuf.Put(txqueue[k].Buffers[0])
}
atomic.AddUint64(&DefaultSnmp.OutPkts, uint64(len(txqueue)))
atomic.AddUint64(&DefaultSnmp.OutBytes, uint64(nbytes))
}
case <-l.die:
return
}
}
}