-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.go
65 lines (57 loc) · 837 Bytes
/
public.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
package songoku
import (
"errors"
"sync/atomic"
)
var pid int32
func init() {
pid = 0
pid = atomic.LoadInt32(&pid)
}
type MqttMsg struct {
Topic string
Content string
Pid int
Dup bool
Qos int
Retain bool
}
func encodeLen(x int) []byte {
l := make([]byte, 0)
var e int
for x > 0 {
e = x % 128
x /= 128
if x > 0 {
e = e | 128
}
l = append(l, byte(e))
}
return l
}
func deCodeLen(l []byte) (int, int, error) {
if len(l) == 0 {
return -1, -1, errors.New("data empty")
}
x := 0
m := 1
i := 0
for {
x += (int(l[i]) & 127) * m
m *= 128
if m > 128*128*128 {
return -1, -1, errors.New("The size cross border")
}
if l[i]&128 != 128 {
break
}
i++
}
return x, i + 1, nil
}
func GetPid() int {
if pid == 10000000 {
pid = 0
}
return int(atomic.AddInt32(&pid, 1))
}