Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
84 lines (69 loc) · 1.46 KB
/
Copy pathutil.go
File metadata and controls
84 lines (69 loc) · 1.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 Tim Oster. All rights reserved.
// Use of this source code is governed by the MIT license.
// More information can be found in the LICENSE file.
package rmnp
import (
"encoding/binary"
"fmt"
"hash/crc32"
"net"
"sync/atomic"
"time"
)
func checkError(msg string, err error) {
if err != nil {
fmt.Println(msg, "=>", err)
panic(err)
}
}
func antiPanic(callback func()) {
if err := recover(); err != nil {
atomic.AddUint64(&StatGoRoutinePanics, 1)
fmt.Println("panic:", err)
if callback != nil {
go func() {
defer antiPanic(nil)
callback()
}()
}
}
}
func addrHash(addr *net.UDPAddr) uint32 {
port := cnvUint32(uint32(addr.Port))
return crc32.ChecksumIEEE(append(addr.IP, port...))
}
func cnvUint32(i uint32) []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, i)
return b
}
func currentTime() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func greaterThanSequence(s1, s2 sequenceNumber) bool {
return (s1 > s2 && s1-s2 <= 32768) || (s1 < s2 && s2-s1 > 32768)
}
func greaterThanOrder(s1, s2 orderNumber) bool {
return (s1 > s2 && s1-s2 <= 127) || (s1 < s2 && s2-s1 > 127)
}
func differenceSequence(s1, s2 sequenceNumber) sequenceNumber {
if s1 >= s2 {
if s1-s2 <= 32768 {
return s1 - s2
}
return (65535 - s1) + s2
}
return differenceSequence(s2, s1)
}
func min(x, y int64) int64 {
if x < y {
return x
}
return y
}
func max(x, y int64) int64 {
if x > y {
return x
}
return y
}