chenshijun
2020-03-25 6b2fd0e872389ffcd6c1da20b778a76f37fd31ec
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package shmqueue
 
import (
    "context"
    "fmt"
    "time"
    "github.com/gen2brain/shm"
)
 
// NewBlock shm block with size
func NewBlock(size int, key int) ([]byte, int, error) {
    id, err := shm.Get(key, size, shm.IPC_CREAT|0666)
    fmt.Println("Get:", id, err)
    if err != nil || id == -1 {
        return nil, -1, err
    }
 
    data, err2 := shm.At(id, 0, 0)
    if err2 != nil {
        return nil, -1, err2
    }
 
    return data, id, nil
}
 
// AttachBlock attach exist shm
func AttachBlock(key int) ([]byte, int, error) {
    id, err := shm.Get(key, 0, 0)
    fmt.Println("Get:", id, err)
    if err != nil || id == -1 { //no exist
        return nil, -1, err
    }
 
    data, err2 := shm.At(id, 0, 0)
    if err2 != nil {
        return nil, -1, err2
    }
 
    return data, id, nil
}
 
// ReleaseBlock release shm block
func ReleaseBlock(data []byte) {
    if data != nil {
        shm.Dt(data)
    }
}
 
// CreateRawShm create raw shm block with size, only space, no padding, return data([]byte), id(int)
// context for quit
func CreateRawShm(ctx context.Context, size int, key int) ([]byte, int) {
    data, id, err := AttachBlock(key)
    fmt.Println("err:", err)
    if err != nil {
    loopB:
        for {
            select {
            case <-ctx.Done():
                return nil, -1
            default:
                if err == nil {
                    break loopB
                } else {
                    fmt.Println("createShm error:", err)
                }
                time.Sleep(time.Millisecond)
                data, id, err = NewBlock(size, key)
            }
        }
    }
    return data, id
}
 
//AttachRawShm don't create
func AttachRawShm(ctx context.Context, key int) ([]byte, int) {
    data, id, err := AttachBlock(key)
    if err != nil {
    loopB:
        for {
            select {
            case <-ctx.Done():
                return nil, -1
            default:
                if err == nil {
                    break loopB
                } else {
                    fmt.Println("createShm error:", err)
                }
                time.Sleep(time.Millisecond)
                data, id, err = AttachBlock(key)
            }
        }
    }
    return data, id
}
 
// CreatePaddingShm create padding shm block with size, return data-with-padding([]byte), id(int)
// context for quit, padding for fill raw shm block
func CreatePaddingShm(ctx context.Context, size int, key int, padding []byte) ([]byte, int) {
    data, id, err := NewBlock(size, key)
    if err != nil {
    loopB:
        for {
            select {
            case <-ctx.Done():
                return nil, -1
            default:
                if err == nil {
                    break loopB
                } else {
                    fmt.Println("createShm error:", err)
                }
                time.Sleep(time.Millisecond)
                data, id, err = NewBlock(size, key)
            }
        }
    }
 
    copy(data, padding)
    return data, id
}
 
// DetachShm destroy
func DetachShm(data []byte) {
    ReleaseBlock(data)
}
 
// Attach attach shmid get block
func Attach(id int) ([]byte, error) {
   return shm.At(id, 0, 0)
}
 
// Detach detach shm block
func Detach(d []byte) error {
   return shm.Dt(d)
}
 
// RemoveShmID Remove shmid
func RemoveShmID(id int) error {
   return shm.Rm(id)
}
 
// RemoveShmKey Remove shmkey
func RemoveShmKey(shmkey int) error {
    _, id, err := AttachBlock(shmkey)
    if err != nil {
        return err
    } else {
        return shm.Rm(id)
    }
}
 
// DetachShmKey detach shmkey
func DetachShmKey(shmkey int) error {
    d, _, err := AttachBlock(shmkey)
    if err != nil {
        return err
    } else {
        return Detach(d)
    }
}
 
// DetachAndRemoveShmKey detach and Remove shmkey
func DetachAndRemoveShmKey(shmkey int) error {
    d, id, err := AttachBlock(shmkey)
    if err != nil {
        return err
    } else {
        Detach(d)
        return shm.Rm(id)
    }
}