forked from Strvice/CNS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.go
More file actions
69 lines (62 loc) · 1.34 KB
/
tools.go
File metadata and controls
69 lines (62 loc) · 1.34 KB
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
package main
import (
"net"
"strings"
)
func toAddrs(addrs string) []string {
addrs = strings.ReplaceAll(addrs, " ", "")
return strings.Split(addrs, ",")
}
const EACH_SIZE = 2048
/* version 1 */
func readLine(conn *net.TCPConn) []byte {
var buff = make([]byte, EACH_SIZE)
l, err := conn.Read(buff)
if err != nil || l <= 0 {
return nil
}
if l < EACH_SIZE {
return buff[:l]
} else {
return append(buff, readLine(conn)...)
}
}
/* version 2 */
// func readLine(conn *net.TCPConn) []byte {
// var data = make([]byte, EACH_SIZE)
// var hasRead int = EACH_SIZE
// var result []byte = nil
// for hasRead == EACH_SIZE {
// hasRead, err := conn.Read(data)
// if err != nil || hasRead <= 0 {
// return result
// }
// result = append(data[0:hasRead])
// }
// return result
// }
func readLine2(conn *net.TCPConn) []byte {
var buff = make([]byte, EACH_SIZE*2)
l, err := conn.Read(buff)
if err != nil || l <= 0 {
return nil
}
if l < EACH_SIZE {
return buff[:l]
} else {
return append(buff, readLine(conn)...)
}
}
func readLineFromUdp(conn *net.UDPConn) ([]byte, *net.UDPAddr) {
var buff = make([]byte, EACH_SIZE*2)
l, addr, err := conn.ReadFromUDP(buff)
if err != nil || l <= 0 {
return nil, nil
}
if l < EACH_SIZE {
return buff[:l], addr
} else {
b, addr := readLineFromUdp(conn)
return append(buff, b...), addr
}
}