-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
61 lines (56 loc) · 1.63 KB
/
utils.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
package ddio
import (
"errors"
"net"
"strconv"
"strings"
"sync/atomic"
"unsafe"
)
func noescape(pointer unsafe.Pointer) unsafe.Pointer {
x := uintptr(pointer)
return unsafe.Pointer(x ^ 0)
}
// 方便的双倍扩容函数
func doubleGrow(memPool *MemoryPool, oldBuf []byte) (newBuf []byte, bl bool) {
bl = memPool.Grow(&oldBuf, (cap(oldBuf)/memPool.BlockSize())*2)
if bl {
newBuf = oldBuf
}
return
}
// Sub-Reactor用于检查连接关闭标志
func checkConnClosed(conn *TCPConn) bool {
return atomic.LoadUint32(&conn.closed) == 1
}
func parseAddress(addr string) (config NetPollConfig, argMap map[string]string, err error) {
argSlice := strings.Split(strings.SplitN(addr, "?", 2)[1], "&")
argMap = make(map[string]string, len(argSlice)/2)
for _, v := range argSlice {
kAndV := strings.Split(v, "=")
argMap[kAndV[0]] = kAndV[1]
}
connProtocol := strings.Split(strings.SplitN(addr, "?", 2)[0], "//")
switch {
case strings.EqualFold(connProtocol[0], "tcp:"):
ipSplit := strings.Split(connProtocol[1], ":")
switch {
case len(ipSplit) == 2:
config.Protocol = TCP_V4
case len(ipSplit) > 2:
config.Protocol = TCP_V6
default:
err = errors.New("ip format not supported")
return
}
// IPV6地址有简便表示多个零的写法,要对这种方法做特殊处理
// 比如这个地址:fe80::1029:f994:b74a:7bef,::处缺少了3组零位
// net.ParseIP中排除Port
ip := net.ParseIP(connProtocol[1][:len(connProtocol[1])-len(ipSplit[len(ipSplit)-1])-1])
config.IP = ip
config.Port, err = strconv.Atoi(ipSplit[len(ipSplit)-1])
default:
return config, nil, errors.New("not supported protocol")
}
return
}